Node snap permission issues when installing global packages

I was recently installing a global package with the node snap and the installation had issues trying to create nonexistent directories.

sudo npm install -g expo-cli --scripts-prepend-node-path=/snap/bin/node
npm WARN invalid config scripts-prepend-node-path="/snap/bin/node"
npm WARN deprecated @hapi/joi@17.1.1: Switch to 'npm install joi'
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm WARN deprecated core-js@2.6.12: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
npm WARN deprecated joi@11.4.0: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated topo@2.0.2: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated hoek@4.2.1: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated @hapi/formula@2.0.0: Moved to 'npm install @sideway/formula'
npm WARN deprecated @hapi/address@4.1.0: Moved to 'npm install @sideway/address'
npm WARN deprecated @hapi/pinpoint@2.0.0: Moved to 'npm install @sideway/pinpoint'
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
/usr/local/bin/expo -> /usr/local/lib/node_modules/expo-cli/bin/expo.js
/usr/local/bin/expo-cli -> /usr/local/lib/node_modules/expo-cli/bin/expo.js
npm WARN lifecycle The node binary used for scripts is /snap/bin/node but npm is using /snap/node/3790/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> core-js@2.6.12 postinstall /usr/local/lib/node_modules/expo-cli/node_modules/babel-runtime/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

2021/03/01 00:35:18.959395 cmd_run.go:994: WARNING: cannot create user data directory: cannot create "/nonexistent/snap/node/3790": mkdir /nonexistent: permission denied
cannot create user data directory: /nonexistent/snap/node/3790: Permission denied
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (node_modules/expo-cli/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/expo-cli/node_modules/watchpack-chokidar2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/expo-cli/node_modules/webpack-dev-server/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN @pmmmwh/react-refresh-webpack-plugin@0.3.3 requires a peer of react-refresh@^0.8.2 but none is installed. You must install peer dependencies yourself.
npm WARN ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! core-js@2.6.12 postinstall: `node -e "try{require('./postinstall')}catch(e){}"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the core-js@2.6.12 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-02-28T22_35_19_895Z-debug.log

The script that fails should be this. However, I could not debug the issue further as no error happens when running the script directly, so I’m thinking it might be connected to how node handles package installations (through npm) in a snap environment.

I installed node as such: sudo snap install node --classic --channel=14

Can anyone suggest more directions for investigation? :blush:

Update

Changing the installation directory with this procedure, fixed the issue, so it seems related to that, but still I can’t understand the permission denied complaints:

2021/03/01 00:35:18.959395 cmd_run.go:994: WARNING: cannot create user data directory: cannot create "/nonexistent/snap/node/3790": mkdir /nonexistent: permission denied
cannot create user data directory: /nonexistent/snap/node/3790: Permission denied

The debug file didn’t say much more:

47197 silly postinstall core-js@2.6.12
47198 info lifecycle core-js@2.6.12~postinstall: core-js@2.6.12
47199 warn lifecycle The node binary used for scripts is /snap/bin/node but npm is using /snap/node/3790/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
47200 verbose lifecycle core-js@2.6.12~postinstall: unsafe-perm in lifecycle false
47201 verbose lifecycle core-js@2.6.12~postinstall: PATH: /snap/node/3790/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/usr/local/lib/node_modules/expo-cli/node_modules/babel-runtime/node_modules/core-js/node_modules/.bin:/usr/local/lib/node_modules/expo-cli/node_modules/babel-runtime/node_modules/.bin:/usr/local/lib/node_modules/expo-cli/node_modules/.bin:/usr/local/lib/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
47202 verbose lifecycle core-js@2.6.12~postinstall: CWD: /usr/local/lib/node_modules/expo-cli/node_modules/babel-runtime/node_modules/core-js
47203 silly lifecycle core-js@2.6.12~postinstall: Args: [ '-c', `node -e "try{require('./postinstall')}catch(e){}"` ]
47204 silly lifecycle core-js@2.6.12~postinstall: Returned: code: 1  signal: null
47205 info lifecycle core-js@2.6.12~postinstall: Failed to exec postinstall script
47206 timing action:postinstall Completed in 123ms
47207 verbose unlock done using /root/.npm/_locks/staging-3a08f0df5026584d.lock for /usr/local/lib/node_modules/.staging
47208 timing stage:rollbackFailedOptional Completed in 763ms
47209 timing stage:runTopLevelLifecycles Completed in 36484ms
47210 warn optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.1 (node_modules/expo-cli/node_modules/chokidar/node_modules/fsevents):
47211 warn notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
47212 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Valid OS:    darwin
47212 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Valid Arch:  any
47212 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Actual OS:   linux
47212 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Actual Arch: x64
47213 warn optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/expo-cli/node_modules/watchpack-chokidar2/node_modules/chokidar/node_modules/fsevents):
47214 warn notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
47215 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Valid OS:    darwin
47215 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Valid Arch:  any
47215 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Actual OS:   linux
47215 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Actual Arch: x64
47216 warn optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/expo-cli/node_modules/webpack-dev-server/node_modules/chokidar/node_modules/fsevents):
47217 warn notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
47218 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Valid OS:    darwin
47218 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Valid Arch:  any
47218 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Actual OS:   linux
47218 verbose notsup SKIPPING OPTIONAL DEPENDENCY: Actual Arch: x64
47219 warn @pmmmwh/react-refresh-webpack-plugin@0.3.3 requires a peer of react-refresh@^0.8.2 but none is installed. You must install peer dependencies yourself.
47220 warn ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
47221 verbose stack Error: core-js@2.6.12 postinstall: `node -e "try{require('./postinstall')}catch(e){}"`
47221 verbose stack Exit status 1
47221 verbose stack     at EventEmitter.<anonymous> (/snap/node/3790/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
47221 verbose stack     at EventEmitter.emit (events.js:315:20)
47221 verbose stack     at ChildProcess.<anonymous> (/snap/node/3790/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
47221 verbose stack     at ChildProcess.emit (events.js:315:20)
47221 verbose stack     at maybeClose (internal/child_process.js:1048:16)
47221 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
47222 verbose pkgid core-js@2.6.12
47223 verbose cwd /home/fabio/Projects/aalto/fullstack_open_2020_part10
47224 verbose Linux 5.4.0-66-generic
47225 verbose argv "/snap/node/3790/bin/node" "/snap/node/3790/bin/npm" "install" "-g" "expo-cli" "--scripts-prepend-node-path=/snap/bin/node"
47226 verbose node v14.16.0
47227 verbose npm  v6.14.11
47228 error code ELIFECYCLE
47229 error errno 1
47230 error core-js@2.6.12 postinstall: `node -e "try{require('./postinstall')}catch(e){}"`
47230 error Exit status 1
47231 error Failed at the core-js@2.6.12 postinstall script.
47231 error This is probably not a problem with npm. There is likely additional logging output above.
47232 verbose exit [ 1, true ]

Hi Guys,

Proceed to create the path /nonexistent/snap/node/3790 manually and grant write access to the path.

The issue will definitely be fixed, it occurred because the path doesn’t exist or the NPM doen’t have access to it

mkdir /nonexistent/snap/node/
cd
chmod 777 /nonexistent/snap/node/

I could workaround the issue by setting a local path for npm as described in Option 2 of this page.

However, this should ideally be something that the node snap handles by itself. Also, it’s still not clear to me what is creating such issue.