Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process.spawn produces empty string for various commands (node, npm, ncu) #843

Closed
kleinfreund opened this issue May 4, 2019 · 6 comments

Comments

@kleinfreund
Copy link

kleinfreund commented May 4, 2019

  • Version: v12.1.0, v13.1.0, v13.12.0, v14.10.1, v15.6.0
  • Platform: Linux RAUMSTATION 5.0.0-13-generic 14-Ubuntu SMP Mon Apr 15 14:59:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: child process (?)

I switched from the PPA binary (https://github.com/nodesource/distributions/blob/master/README.md#debinstall) to the snap binary (https://snapcraft.io/node). This started to break the ncu -g command. Originally, I opened an issue with npm-check-updates, but it doesn’t seem to be an issue on their end (for reference: raineorshine/npm-check-updates#536).

Only with the snap binary do I get the following error:

/home/phil/.npm-global/lib/node_modules/npm-check-updates/lib/npm-check-updates.js:387
        throw err;
        ^

Error: Expected JSON from "npm ls". This could be due to npm instability.


    at parseJson (/home/phil/.npm-global/lib/node_modules/npm-check-updates/lib/package-managers/npm.js:38:15)
    at /home/phil/.npm-global/lib/node_modules/npm-check-updates/lib/package-managers/npm.js:144:30
    at processTicksAndRejections (internal/process/task_queues.js:89:5)

The command that is executed with child_process.spawn is npm ls --global --depth=0 --json which produces the correct JSON with both the PPA and the snap binary when executed directly from the terminal.

Steps to reproduce:

Create a reproduction.js file containing the following code and run node reproduction.js with node being a Node.js binary from a Snap source (reproduces with all versions from v12 to v14 at least).

const { spawnSync } = require('child_process')

// Both these spawn commands will yield nothing in their stdout.
// The latter should just contain a Node version tring such as `v14.10.1`.

// npm ls --global --depth=0 --json
const npmListOutput = String(spawnSync('npm', ['ls', '--global', '--depth=0', '--json']).stdout)
console.log({ npmListOutput });

// node --version
const nodeVersionOutput = String(spawnSync('node', ['--version']).stdout)
console.log({ nodeVersionOutput });

To switch around the Node binary on Ubuntu:

  1. Uninstall Node.

    sudo apt remove nodejs
    
  2. Install Node via Snap.

    sudo snap install node --channel=14/stable --classic
    
  3. Verify that Snap’s Node is used (i.e. on my system, the binary is reported to be in /snap/bin/node when running which node).

  4. Run npm-check-updates with the global flag.

    ncu -g
    
  5. Remove Node via Snap.

    sudo snap remove node
    
  6. Install Node via apt.

    sudo apt install nodejs
    
@pachulo
Copy link

pachulo commented Sep 27, 2019

We were bitten by this same problem, and we think that is related to this: https://forum.snapcraft.io/t/snapd-2-32-breaks-live-server-installer/4597

@kleinfreund
Copy link
Author

I’m closing this because there hasn’t been a response or interest in this issue (which is still happening).

@kleinfreund
Copy link
Author

For posterity, both the following scripts produce the empty string when their commands executed in a shell return a completely different (and expected) results:

const npm = require('child_process').spawnSync('npm', ['ls', '--global', '--depth=0', '--json', '--prefix=/home/phil/.npm-global'])
console.log(String(npm.stdout) === '');

The asynchronous variant never runs into either callbacks for stdout or stderr and completes with child process exited with code 0.

const npm = require('child_process').spawn('npm', ['ls', '--global', '--depth=0', '--json', '--prefix=/home/phil/.npm-global'])

npm.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
});

npm.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
});

npm.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
});

@kleinfreund kleinfreund reopened this Sep 12, 2020
@kleinfreund kleinfreund changed the title ncu -g fails with Snap binary: child_process.spawn produces empty string child_process.spawn produces empty string for various commands (node, npm, ncu) Jan 24, 2021
@kleinfreund
Copy link
Author

This is still broken in the Snap distribution of Node.js.

The following short program logs true even though the expression String(npm.stdout) should evaluate to a dependency graph of globally installed npm packages.

const npm = require('child_process').spawnSync('npm', ['ls', '--global', '--depth=0', '--json', '--prefix=/home/phil/.npm-global'])
console.log(String(npm.stdout) === '');

This issue makes the Snap distribution of Node.js unuseable for me.

@desmap
Copy link

desmap commented Feb 4, 2021

I just had the same issue with coc.vim which relies on node. When it`s installed via snap coc.vim cannot check node's version number: neoclide/coc.nvim#2870

@igsu
Copy link
Contributor

igsu commented Aug 10, 2021

We are not responsible for maintaining the Snap packages, to solve any issue or more information:

https://github.com/nodejs/snap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants