The npm plugin

The npm plugin is useful when working with Node.js and npm JavaScript based parts.

This plugin can only be used with a base of core22 and core20. For core18 and core, use the nodejs plugin instead.

The plugin uses node to install dependencies from package.json. It also sets up binaries defined in package.json by adding them to PATH.

This plugin uses the common plugin keywords as well as those for “sources”. For more information, see Snapcraft parts metadata.

Plugin-specific features and syntax are dependent on which base is being used, as outlined below:

See Node applications for a simple example, or search GitHub for projects already using the plugin.

ⓘ This is a snapcraft plugin. See Snapcraft plugins and Supported plugins for further details on how plugins are used.

base: core22

This plugin uses the following plugin-specific keywords:

  • npm-include-node (bool, default: false) If true, download and include the node binary and its dependencies. If npm-include-node is true, then npm-node-version must be defined.

  • npm-node-version (string) The version of node.js you want the snap to run on and includes npm, as would be downloaded from (https://nodejs.org). If not set, node.js must be provided another way, such as creating a node-deps part that provides node using build and staging dependencies or manually. Search GitHub for examples.

Requires Snapcraft version 7.0+.

base: core20

This plugin uses the following required plugin-specific keyword:

  • npm-node-version (string) The version of node.js you want the snap to run on and includes npm, as would be downloaded from (https://nodejs.org).

Requires Snapcraft version 4.0+.

nodejs-version should be npm-node-version

Else you get: Additional properties are not allowed ('nodejs-version', were unexpected)

2 Likes

The difference there, @bart, is between using core18 vs core20. Plugins were all updated for core20 and the options made more consistently named between plugins.

Edit: oops, I forgot this plugin only works with core20 :woman_facepalming:

I’m starting use the npm plugin with base: core20 environment & i’d ask about the node.js versions actually supported and how the default one is handled when no npm-node-version is specified … Actually with the node 16.9.1 version i’ve experimented the following error:

1478 verbose stack Error: command failed
1478 verbose stack     at ChildProcess.<anonymous> (/root/parts/node-service/install/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/index.js:64:27)
1478 verbose stack     at ChildProcess.emit (node:events:394:28)
1478 verbose stack     at maybeClose (node:internal/child_process:1064:16)
1478 verbose stack     at Socket.<anonymous> (node:internal/child_process:450:11)
1478 verbose stack     at Socket.emit (node:events:394:28)
1478 verbose stack     at Pipe.<anonymous> (node:net:672:12)
1479 verbose pkgid core-js@3.6.5
1480 verbose cwd /root/parts/node-service/build
1481 verbose Linux 5.4.0-53-generic
1482 verbose argv "/root/parts/node-service/install/bin/node" "/root/parts/node-service/install/bin/npm" "install" "-g" "--prefix" "/root/parts/node-service/install" "logicat-iot-2.7.0.tgz"
1483 verbose node v16.9.1
1484 verbose npm  v7.21.1
1485 error code 127
1486 error path /root/parts/node-service/install/lib/node_modules/logicat-iot/node_modules/core-js
1487 error command failed
1488 error command sh -c node -e "try{require('./postinstall')}catch(e){}"
1489 error sh: 1: node: Permission denied
1490 verbose exit 127

That i think it depends from the npm version in use? Every time i’m trying to add a package i’m running into trouble with the npm plugin and latest node.js versions eg. 16.10.0 …

here the repo:

1 Like

I get the same error “Permission denied”.

Seems like people have some luck with

npm config set user 0
npm config set unsafe-perm true

But I’m not sure. Is this plugin broken?

I’ve just tried this with our Node apps quickstart guide (which I’ve also updated to use core20 in the process), and everything is working: Node apps.

Hey,

tried this plugin today. But was a bit confused why node is downloaded as part of the build step?

I have to generate some files using npm before running npm install. But this fails with npm not found because node seems to get installed during the build step, so an override would not work:

  override-build: |
  npm install
  npm run build-latest
  snapcraftctl build