Repository navigation
Cannot exec or spawn node, npm or yarn [node snap] #37982
Description
Activity
I mentioned this issue in the snap forum, so we might receive some special assistance from snap experts: https://forum.snapcraft.io/t/node-snap-issues-with-exec-of-npm-node-yarn/23635
Can you try to run the following commands in your terminal:
/bin/sh -c 'node -v' /bin/sh -c 'npm --version' /bin/sh -c 'yarn --version'
Sure, here you are:
$ /bin/sh -c 'node -v' v14.16.0 $ /bin/sh -c 'npm --version' 7.7.5 $ /bin/sh -c 'yarn --version' 1.22.5- addedchild_processIssues and PRs related to the child_process subsystem.Issues and PRs related to the child_process subsystem.linuxIssues and PRs related to the Linux platform.Issues and PRs related to the Linux platform.
on Mar 31, 2021 microsoft/vscode-vsce#341 was recently closed, apparently owing to this or a similar problem with child processes. Mentioning here.
Hi! We installed Node.js via snap store and tried to use
execandspawnfunctions in the VS Code extension environment. In the test extension we could also reproduce the same incorrect behavior as described in this issue. The functions for spawning child processes skip the execution of npm and CLI commands (based on Node.js scripts) and return just an empty result or an error.I am getting same error when doing
spawn('yarn build'). code: 'ENOENT'. And getting same error on running any node binaries (webpack, npm etc). System commands (like curl, zip) are working fine.I tried doing
which yarnandwhich node, yarn is picked from some tmp file, and node is also running from tmp yarn folder.When I run the script directly (instead of via yarn), it returns the node inside the nvm directory path, but still not working.
Interestingly
which yarnandwhich nodeare working fine.I looked into this.
EDIT:
Correction:node cannot run
snapdue to sandbox limitations. This blocks node from executing some snap programs as children.on the other hand, running node in the true path (in my case /snap/node/6694/bin/node currently) will work.
causing things like a npm version check to work like this (as long as snap is installed, in which case "which" command is also present):
execSync(process.execPath + ' $(which npm) --version').toString().trim()(returns the correct 8.19.2 in my case)otherwise the command
execSync('npm --version')will return an empty string, because "snap" just terminates itself before printing anything.================== Edit End =========
as noticed, node cannot spawn a child of any given snap program.
This is caused by the "symlink" structure of the bin files in /snap/binll /snap/binreveals that all of those link to /usr/bin/snap in one way or another. I have no idea how on earth the bash knows how to execute them. I guess there is an env variable which does contain this info, mapping the execution of the snap bin tosnap run [bin-name]but this means, whenever a
which [snap-program-name]resolves to/snap/bin/[prog-name]it must be runsnap run [name]instead of calling it from terminal directly.
The bash seems to notice the weird symlink, and knows what to do. But node exec does not. (understandable, i have no idea either xD)
Anyway. There should be a solution to this.Reacted by Gustavo HenkeReacted by Fabio Colella and Toby Worth@Bloodiko great work looking into this! 😊
In the meanwhile the following workaround should help (works on Ubuntu 22.04):
for cmd in node npm yarn; do mkdir -p ~/bin cat > "$HOME/bin/$cmd" << EOF #!/bin/bash # https://github.com/nodejs/node/issues/37982 /snap/bin/$cmd "\$@" > >(cat) 2> >(cat >&2) EOF chmod +x "$HOME/bin/$cmd" done
Reacted by Scott TalbertI'll go ahead and close this because it's a snap sandbox restriction, not an issue with node itself. If the restriction is a problem for you, you should report it to the maintainers of the snap package.
v14.16.0Linux fabio-XPS-15-7590 5.4.0-70-generic #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 x86_64 x86_64 x86_64 GNU/Linuxchild_processWhat steps will reproduce the bug?
Install node snap:
sudo snap install node --classicOptional: fix permission issues https://npm.github.io/installation-setup-docs/installing/a-note-on-permissions.html
Then run the following:
How often does it reproduce? Is there a required condition?
It always happens to me. I think a requirement to be using node from the snap.
What is the expected behaviour?
What do you see instead?
The output of node and npm commands is empty, while yarn fails.
However, running each of those commands from the terminal works correctly:
Additional information
Running a command like
which npmreturns the correct binary path both from the terminal and from the script. I also attempted to write the full path in the script, instead of the command, (e.g./home/fabio/.npm-global/bin/npm) but nothing changes.The node binary is provided by the node snap. However, I could not find clear issues in the logs. Also npm and yarn are provided in the snap, but I installed them globally under
/home/fabio/.npm-global.✅ I'm willing to help with debugging and getting this solved. 👨💻