JetBrains Space Help

Node.js, npm


  • You have a Node.js project with package.json in the project root.

  • (Optional) The project has unit tests.

  • If you want to publish artifacts to Space Packages, make sure the project has an npm registry.

Eligible images

Build, run tests, and publish in a Node.js project

Currently, Automation does not provide any API for working with npm. So, the only way to build, test, and publish Node.js projects is to use the npm tool in shell scripts.

The content of .space.kts might look like follows:

job("Run npm test and publish") { container(displayName = "Run publish script", image = "node:14-alpine") { env["REGISTRY"] = "" shellScript { interpreter = "/bin/sh" content = """ echo Install npm dependencies... npm ci echo Run build if it exists in package.json... npm run build --if-present echo Run tests... npm run test echo Run publishing... chmod +x ./ ./ """ } } }

In more details:

  • env["REGISTRY"]: specifies an npm registry.

  • npm ci: installs npm dependencies.

  • npm run build: runs build step if it exists.

  • npm run test: runs tests.

  • ./ runs the publishing script. See details below.

The script authenticates in the registry using the .npmrc file and then publishes the npm package:

#!/bin/sh echo "Configure npm..." mkdir package && cd package echo "registry = $REGISTRY" >> ~/.npmrc AUTH=$(echo -ne "$JB_SPACE_CLIENT_ID:$JB_SPACE_CLIENT_SECRET" | base64 | tr -d \\n) echo "_auth = $AUTH" >> ~/.npmrc echo "email =" >> ~/.npmrc echo "always-auth = true" >> ~/.npmrc echo "Publish package..." VERSION="0.0.$JB_SPACE_EXECUTION_NUMBER" npm config set init.version $VERSION npm init -y npm publish --registry $REGISTRY

In more details:

  • registry = $REGISTRY: specifies registry URL using the environment variable set in .space.kts.

  • $JB_SPACE_CLIENT_ID and $JB_SPACE_CLIENT_SECRET: environment variables that authenticate the Automation service in Space Packages.

  • VERSION="0.0.$JB_SPACE_EXECUTION_NUMBER": uses the $JB_SPACE_EXECUTION_NUMBER to specify the package version based on the script run number.

Last modified: 08 April 2024