Putting NPM package in a snap?

What’s the right way to get an NPM package into a snap directly from NPM?

From what I can see, this used to be possible in the past, but that’s no longer the case. I’ve seen quite a few examples like

parts:
  vtop:
    plugin: nodejs
    node-packages:
      - vtop

(from https://github.com/earnubs/vtop-snap/blob/master/snapcraft.yaml) out in the wild. These appear no longer work, since the nodejs plugin now requires a source directive (and no longer supports the node-packages directive).

Specifically, I’d like to put https://www.npmjs.com/package/jfq in a snap. I had a brief go at doing this by using the nodejs plugin and the github repo as a source, but I got an error

FileNotFoundError: [Errno 2] No such file or directory: '/build/snap-name/parts/jfq/install/bin/jfq.js'

which I’m not sure what to make of. Either way, it seems that doing the equivalent of npm install jfq during the snap build process would be preferable.

Thank you in advance for any help!

IIRC, the current practice is to specify a source directory that contains a package.json file. Any dependencies you specify there will be installed by the part.

Yup, that did it, thanks @jamesh . For anyone that’s interested, the package.json looked like:

{
    "name": "jfq-builder",
    "version": "1.0.0",
    "dependencies": {
        "jfq": "1.2.0"
    }
}

The name and version fields appear to be compulsory for snapcraft, even if they are not for node.

That got the module itself installed, though now I’m trying to see how I can get the associated command to make its way into my bin directory…

1 Like