How to run NPM scripts with NodeJS plugin


#1

Snapcraft used to accept a list of commands for NPM to run, however in v3.0 npm-run has been removed.

How can I replicate the following in v3.0?

parts:
  frontend:
    plugin: nodejs
    npm-run: [build]
    stage: [dist]

I have been trying different variations of

parts:
  frontend:
    plugin: nodejs
    # nodejs-package-manager: npm
    source: frontend
    stage: [dist]
    override-build: |
      ../npm/bin/yarn run build

But with no success

Launching a VM.
Updating pull step for defaults (source changed)                                
Skipping pull frontend (already ran)
'mongoose' has dependencies that need to be staged: frontend
Skipping pull frontend (already ran)
Building frontend 
Yarn requires Node.js 4.0 or higher to be installed.
Failed to run 'override-build': Exit code was 1.

#2

you probably want to use node-engine: and make sure to get a new enough node version into your snap … there is also node-package-manager to point the plugin to yarn…

https://docs.snapcraft.io/the-nodejs-plugin/8514


#3

The default version (8.x) is high enough for my purpose, and yarn is the default package manager. Explicitly specifying them doesn’t make any difference though.

parts:
  frontend:
    plugin: nodejs
    # nodejs-package-manager: npm
    source: frontend
    stage: [dist]
    override-build: |
      export PATH=$PATH:../npm/bin
      yarn run build

Appending the missing folder to $PATH works and I can then run yarn/npm commands. However then it complains about the dist path I am trying to stage.

Staging frontend
Failed to copy '/root/parts/frontend/install/dist': no such file or directory

Why is it trying to stage dist from that path? Shouldn’t it be looking in /root/parts/frontend/build/dist (which is where the files it is looking for actually reside)?

So basically to sum things up, I have 2 questions:

  1. Why does the nodejs plugin not add the required node binaries to the $PATH variable so they can be used in override scripts? Is this a bug?
  2. Why is stage looking in install rather than build folder for its files? Is this correct or should I be doing it differently? I want to stage the files so that they can be picked up by another part.