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

npm outdated unable to be run programmatically from child_process.execSync #4188

Closed
jrtitus opened this issue Jun 16, 2023 · 4 comments
Closed

Comments

@jrtitus
Copy link

jrtitus commented Jun 16, 2023

Version

16.20.0, 18.6.0, 20.3.0

Platform

Linux --- 5.15.90.1-microsoft-standard-WSL2 nodejs/node#1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

child_process

What steps will reproduce the bug?

const { execSync } = require('node:child_process');
const out = execSync('npm outdated --omit=dev --json');

How often does it reproduce? Is there a required condition?

Always. This also occurs with a request of execSync('npm outdated') (without flags).

What is the expected behavior? Why is that the expected behavior?

A Buffer including the results of npm outdated.

The example I'm using should have this output:

{
  "octokit": {
    "current": "2.0.19",
    "wanted": "2.0.21",
    "latest": "2.0.21",
    "dependent": "---",
    "location": "/home/---/src/---/node_modules/octokit"
  }
}

What do you see instead?

Uncaught Error: Command failed: npm outdated --omit=dev --json
    at __node_internal_genericNodeError (node:internal/errors:857:15)
    at checkExecSyncError (node:child_process:861:11)
    at execSync (node:child_process:932:15) {
  status: 1,
  signal: null,
  output: [
    null,
    <Buffer 7b 0a 20 20 22 6f 63 74 6f 6b 69 74 22 3a 20 7b 0a 20 20 20 20 22 63 75 72 72 65 6e 74 22 3a 20 22 32 2e 30 2e 31 39 22 2c 0a 20 20 20 20 22 77 61 6e ... 138 more bytes>,
    <Buffer >
  ],
  pid: 6610,
  stdout: <Buffer 7b 0a 20 20 22 6f 63 74 6f 6b 69 74 22 3a 20 7b 0a 20 20 20 20 22 63 75 72 72 65 6e 74 22 3a 20 22 32 2e 30 2e 31 39 22 2c 0a 20 20 20 20 22 77 61 6e ... 138 more bytes>,
  stderr: <Buffer >
}

Additional information

  1. If you translate the stdout buffer from hex to ascii, it does have the correct data, but the call fails for some reason.
  2. Other commands such as execSync('npm i'), execSync('npm audit'), execSync('npm config list --json'), etc. will succeed.
@richardlau richardlau transferred this issue from nodejs/node Jun 16, 2023
@richardlau
Copy link
Member

cc @nodejs/npm

I believe this is working as designed. npm outdated exits with a non-zero exit code (status: 1 in the output) which causes execSync to throw an error.

Looks like there's some discussion about the npm behaviour in npm/rfcs#473.

@jrtitus
Copy link
Author

jrtitus commented Jun 16, 2023

@richardlau Thank you for the response.

At least now I understand how I can work around this. I didn't even consider that a non-zero exit code was expected when outdated packages are found because the command succeeds in retrieving information about them.

Copy link

github-actions bot commented May 7, 2024

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.

@github-actions github-actions bot added the stale label May 7, 2024
Copy link

github-actions bot commented Jun 7, 2024

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants