Error building with npm plugin and lxd or multipass

I’m trying to build from within a lxd container, using the npm plugin.

(I get the same error on multipass)

Here is my snap/snapcraft.yaml

name: polkadot-rpc-check
base: core20 
version: '0.1' 
summary: Nagios plugin to check polkadot rpc endpoint. 
description: |
  This is a nagios plugin to check a pokadot rpc endpoint.

grade: devel 
confinement: devmode 

apps:
  check-substraterpc:
    command: bin/check-substraterpc
    plugs:
      - network
parts:
  check-node:
    source: .
    plugin: npm
    npm-node-version: "17.7.2"

$ snapcraft --use-lxd --debug

npm notice 641B  snap/snapcraft.yaml                                     
npm notice === Tarball Details === 
npm notice name:          node-check                              
npm notice version:       1.0.0                                   
npm notice filename:      node-check-1.0.0.tgz                    
npm notice package size:  9.2 kB                                  
npm notice unpacked size: 28.8 kB                                 
npm notice shasum:        1bf28deebabf94c25d9a5801cf300c7f12e4e7c6
npm notice integrity:     sha512-fo/0TNozzSFaa[...]jzs/Q9Buocl+w==
npm notice total files:   48                                      
npm notice 
+ npm install -g --prefix /root/parts/check-node/install node-check-1.0.0.tgz
npm ERR! code 127
npm ERR! path /root/parts/check-node/install/lib/node_modules/node-check/node_modules/utf-8-validate
npm ERR! command failed
npm ERR! command sh -c node-gyp-build
npm ERR! sh: 1: node-gyp-build: Permission denied

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-03-19T15_32_36_630Z-debug-0.log
Failed to build 'check-node'.

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

I check some logs…

517 verbose argv "/root/parts/check-node/install/bin/node" "/root/parts/check-node/install/bin/npm" "install" "-g" "--prefix" "/root/parts/check-node/install" "node-check-1.0.0.tgz"
518 verbose node v17.7.2
519 verbose npm  v8.5.2
520 error code 127
521 error path /root/parts/check-node/install/lib/node_modules/node-check/node_modules/utf-8-validate
522 error command failed
523 error command sh -c node-gyp-build
524 error sh: 1: node-gyp-build: Permission denied
525 verbose exit 127
526 timing npm Completed in 3266ms
527 verbose unfinished npm timer reify 1647703956901
528 verbose unfinished npm timer reify:build 1647703959499
529 verbose unfinished npm timer build 1647703959499
530 verbose unfinished npm timer build:deps 1647703959499
531 verbose unfinished npm timer build:run:install 1647703959505
532 verbose unfinished npm timer build:run:install:node_modules/node-check/node_modules/bufferutil 1647703959505
533 verbose unfinished npm timer build:run:install:node_modules/node-check/node_modules/utf-8-validate 1647703959521
534 verbose code 127
535 error A complete log of this run can be found in:
535 error     /root/.npm/_logs/2022-03-19T15_32_36_630Z-debug-0.log
snapcraft-polkadot-rpc-check # exit
exit

… and I have no clue on how to continue.

Try

parts:
  check-node:
    source: .
    plugin: npm
    npm-node-version: "17.7.2"
    build-environment:
      - npm_config_unsafe_perm: 'true'
      - SUDO_UID: '0'
      - SUDO_GID: '0'
      - SUDO_USER: 'root'

The unsafe_perm flag is default with the npm plugin, I include it for simplicity incase other people are trying to use npm directly in override-build or similar.

The SUDO_* vars should hopefully help fix the specific issue.

I tried, but no luck.

$ snapcraft clean

$ snapcraft build

+ npm install -g --prefix /root/parts/check-node/install node-check-1.0.0.tgz
npm notice 
npm notice New patch version of npm available! 8.5.2 -> 8.5.5
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.5.5
npm notice Run npm install -g npm@8.5.5 to update!
npm notice 
npm ERR! code 127
npm ERR! path /root/parts/check-node/install/lib/node_modules/node-check/node_modules/utf-8-validate
npm ERR! command failed
npm ERR! command sh -c node-gyp-build
npm ERR! sh: 1: node-gyp-build: Permission denied

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-03-19T20_45_57_695Z-debug-0.log
Failed to build 'check-node'.

Recommended resolution:
Check the build logs and ensure the part's configuration and sources are correct.
Run the same command again with --debug to shell into the environment if you wish to introspect this failure.

This is the current (non functional) yaml.

name: polkadot-rpc-check
base: core20 
version: '0.1' 
summary: Nagios plugin to check polkadot rpc endpoint. 
description: |
  This is a nagios plugin to check a pokadot rpc endpoint.

grade: devel
confinement: devmode 

apps:
  check-substraterpc:
    command: bin/check-substraterpc
    plugs:
      - network
parts:
  check-node:
    source: .
    plugin: npm
    npm-node-version: "17.7.2"
    build-environment:
      - npm_config_unsafe_perm: 'true'
      - SUDO_UID: '0'
      - SUDO_GID: '0'
      - SUDO_USER: 'root'

More updates on this.

I have managed to get a charm built with “core18”, replacing the “npm” plugin with “nodejs”.

This is not what I want, but lets me build a charm with my simple js app.

So, the “npm” plugin fails on me and core20 is needed/wanted for my charm - so I would really like to understand what goes wrong above.