Npm plugin in core20 fails to build

I have a snapcraft.yaml that contains a section like this:

  ui:
    plugin: npm
    source: https://github.com/lemmynet/lemmy-ui.git
    npm-node-version: 14.13.0

Which results in this error:

Building ui
+ [ ! -f /root/parts/ui/install/bin/node ]
+ curl -s https://nodejs.org/dist/v14.13.0/node-v14.13.0-linux-x64.tar.gz
+ tar xzf - -C /root/parts/ui/install/ --strip-components=1
+ npm pack .
+ tail -1
npm ERR! package.json requires a valid "version" field

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-10-06T18_18_38_075Z-debug.log
+ npm install -g --prefix /root/parts/ui/install
npm ERR! Can't install /root/parts/ui/build: Missing package version

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-10-06T18_18_38_555Z-debug.log
Failed to build 'ui'.

Recommended resolution:
Check the build logs and ensure the part's configuration and sources are correct.

I also tried building the step manually:

  ui:
    plugin: dump
    source: https://github.com/lemmynet/lemmy-ui.git
    build-snaps:
      - node/14/stable
    stage-snaps:
      - node/14/stable
    override-build: |
      set -eux
      snapcraftctl build
      node.yarn install --pure-lockfile
      node.yarn
      node.yarn build:prod

This resulted in the following error:

Building ui
+ snapcraftctl build
+ cp --archive --link --no-dereference . /root/parts/ui/install
cp: cannot create hard link '/root/parts/ui/install/./LICENSE' to './LICENSE': File exists
cp: cannot create hard link '/root/parts/ui/install/./package.json' to './package.json': File exists
cp: cannot create hard link '/root/parts/ui/install/./README.md' to './README.md': File exists
Failed to build 'ui'.

Recommended resolution:
Check the build logs and ensure the part's configuration and sources are correct.
+ node.yarn install --pure-lockfile

For reference, I’m trying to do the equivalent of what’s in this Dockerfile:

What is the recommended way of handling yarn installs like this with core20?

For the first case, “npm pack” requires a version field. Most node packages do have a version field AFAIK.

Use the “nil” plugin. “dump” plugin is for copying.

Then make sure the required artifacts get installed to $SNAPCRAFT_PART_INSTALL.

@cjp256: Thanks for the response. Here’s the package.json in question:

There’s no version field there, and it works fine with yarn. Does core20 support yarn projects?

There is no yarn plugin today for core20, though would be easy to add.

Snapcraft relies on “npm pack” or “yarn pack” (prior to core20) to package up the install and extract it to $SNAPCRAFT_PART_INSTALL. However, both yarn and npm require a version to do so. Maybe you can insert the version into the package.json prior to building?