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 commands not working when called from inside Node #243

Closed
wbt opened this issue Dec 9, 2021 · 3 comments
Closed

NPM commands not working when called from inside Node #243

wbt opened this issue Dec 9, 2021 · 3 comments

Comments

@wbt
Copy link

wbt commented Dec 9, 2021

This issue was discovered here.

When I run node -v on the command line, I get 7.24.0.
When I run npm show --json eslint-config-xo@latest peerDependencies directly on the command line, I get the expected:

{
"eslint": ">=7.20.0"
}

Instead, suppose I make the following Javascript file as spawntest.js:

//One of the two next lines should be uncommented; doesn't really matter which.
//const spawnSync = require("cross-spawn").sync;
const spawnSync = require("child_process").spawnSync;
const npmProcess = spawnSync(
    "npm",
    //['-v'], //Using this instead of the next line gives the same result. 
    ["show", "--json", "eslint-config-xo@latest", "peerDependencies"],
    { encoding: "utf8" }
);
const error = npmProcess.error;
if (error && error.code === "ENOENT") {
    return null;
}
const fetchedText = npmProcess.stdout.trim();
console.log('fetchedText is "' + fetchedText + '"');

The result is:

fetchedText is "Sorry, there's a problem with nodist. Couldn't resolve npm version spec 7.24.0 : Couldn't find any matching version"

The init procedure in eslint tries to parse that as JSON, and understandably fails.

This seems like it might be an issue in nodist, so I'm filing a report here.

@wbt
Copy link
Author

wbt commented Dec 10, 2021

The error is getting thrown from this line and bubbling up through this function.

Apparently it's looking for 7.24.0 in the list of npm versions which are the names of children directories under os.Getenv("NODIST_PREFIX")+"/npmv" and on my machine, 7.24.0 isn't one of them. However, when I try to run 'nodist npm local 7.24.0' per the documentation, I get the following error:

{"message":"Bad credentials","documentation_url":"https://docs.github.com/rest"}.
Sorry.

So I now suspect something related to how Nodist gets npm distributions.

@aladdin-add
Copy link

maybe it's a known issue, as seen in the Known issues:

native modules: Since the node version changes at the mercy of the shim executable, based on env vars, target directory and the global setting, and availability of node versions, it is possible that locally or globally installed node modules that depend on a specific version of node (usually native modules and downloaders) stop working. npm rebuild makes things work again in these cases.

@wbt
Copy link
Author

wbt commented Dec 10, 2021

As it turns out, this is a duplicate of #233 and #235, but it sure didn't look like that at the start.

@wbt wbt closed this as completed Dec 10, 2021
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

2 participants