The nodejs plugin

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

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

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

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.

base: core18 | core

This plugin uses the following plugin-specific keywords:

  • nodejs-version (string; default: 8.12.0) The version of node.js you want the snap to run on. For example: nodejs-version: 8.12.0
  • nodejs-package-manager (string; default: yarn) The language package manager to use to drive installation of node packages. Can be either npm or yarn (default).
  • nodejs-yarn-version (string) The version of the yarn package manager to use to drive installation of node packages. Currently, this must start with a v, for example: nodejs-yarn-version: v1.19.1

For examples, search GitHub for projects already using the plugin.

Requires Snapcraft version 3.x.

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

During the build stage, the plugin unsets SUDO_UID and runs npm config set unsafe-perm true to get around the issue of npm refusing to retrieve git repositories and run scripts as root. If you use override-build to control the npm install, those will not be set, and any scripts will fail to run and git repositories will fail to be retrieved.

If you have to use override-build, make sure that you unset SUDO_UID and run npm config set unsafe-perm true.

Please add a note that you probably have to run snapcraft clean after you change the Node.js-version, if you already ran snapcraft before.

Otherwise the node modules in parts/…/install/lib/node_modules may not fit for the newer Node.js version, but won’t be updated on the other hand. So the build will fail.

In my case snapcraft aborted with following error:

parts/.../install/lib/node_modules/npm/node_modules/npmlog/log.js:57
log.progressEnabled = log.gauge.isEnabled()
                                ^

TypeError: log.gauge.isEnabled is not a function

nodejs-yarn-version must start with ‘v’. E.g. nodejs-yarn-version: v1.19.1

1 Like

Thanks for letting us know - I’ve updated the doc!

1 Like