-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Run nvm from package.json script #2106
Comments
Unfortunately your expectation is wrong; nvm is not an executable, it’s a sourced shell function. Sourcing it in each npm run command is required, by its nature. I’m not sure why you had to unset those npm env vars tho. Additionally, nvm is not supported via homebrew; please brew uninstall it and reinstall it with the proper curl command from the readme. |
Does transforming it into an executable not make sense? It would prevent a lot of unexpected behaviour cases. (When searching the issues for About the nvm location: Now that I'm digging through this, I see that nvm is not installed through PS: Exactly the same NPM environment vars had to be unset from a colleague's machine. So this seems to be something systematic. |
theres not really a way to confirm - but you could rerun the install script and everything should be fine. what exactly happens when you dont reset those npm env vars? |
Also no, nvm wouldn’t be able to function as an executable - it has to be able to modify the current shell’s PATH. |
Without the unsets:
Sourcing nvm.sh errors on an incompatible env var being present, in this order:
|
The other two seem like perhaps a bug in nvm. |
Running from script: $ npm run watch
> [REDACTED]@0.1.0 watch /Users/guidobouman/[REDACTED]
> source ~/.nvm/nvm.sh && nvm use && webpack --config webpack.dev.js
nvm is not compatible with the "npm_config_prefix" environment variable: currently set to "/Users/guidobouman/.nvm/versions/node/v8.11.1"
Run `unset npm_config_prefix` to unset it. Running from terminal: $ source ~/.nvm/nvm.sh
[nothing]
$ nvm current
v8.11.1
$ nvm use
Found '/Users/guidobouman/Projects/Nedwin.Portal/.nvmrc' with version <10.15.1>
Now using node v10.15.1 (npm v6.4.1)
$ nvm current
v10.15.1 Running from script after local $ npm run watch
> [REDACTED]@0.1.0 watch /Users/guidobouman/[REDACTED]
> source ~/.nvm/nvm.sh && nvm use && webpack --config webpack.dev.js
nvm is not compatible with the "npm_config_prefix" environment variable: currently set to "/Users/guidobouman/.nvm/versions/node/v10.15.1"
Run `unset npm_config_prefix` to unset it. Looks like nvm sets it somehow. Also, the error happens before |
Hmm - is there any chance your homedir or $NVM_DIR is a symlink? |
I don't think so. My Running |
and the capitalization of |
|
Is there any chance you can make a small repo that this issue can be reproduced in? |
Let me try to do that sometime this week. |
Is there any update on this? I'm seeing the same issue. |
I still haven't seen a repro repo. |
Sorry for the UBER long delay, there's the repro repo: |
aha, interesting. it seems that an npm run-script sets the ( |
Hmmm, okay. So there's no fixing this, except for unsetting all those vars? Should this not be brought up with the npm team? I'd think this is something they would be interested in. To allow every npm command to run in the context it was meant to run. Or do you think differently? |
You misunderstand :-) I closed the issue in 6fa92c8 with a fix. In the next release, it will work. |
I missed the commit hash in the close message... 🤦♂ Thanks @ljharb! |
I was having the same issue and this commit fixed it for me. Thanks! Is there a date scheduled for the next release? |
Fyi this is what I ended up using as a preinstall script, it has the benefit of installing the global packages used by the users current version, and fails with a message missing nvm.sh if not found "preinstall": "test \"$(node -v)\" != \"$(cat .nvmrc)\" && { unset npm_package_scripts_preinstall npm_config_prefix npm_lifecycle_script && . ~/.nvm/nvm.sh && nvm install $(cat .nvmrc) --reinstall-packages-from=current --skip-default-packages && nvm use; } || echo", |
Added package.json cmd "preinstall": - To match the node version. - if not it will unset any incompatible env var being present. - then it should install .nvmrc version on file to update the npm version with compatible version. - finally "nvm use" should point to the version installed "14.17.0". References: - nvm-sh/nvm#2106 - https://github.com/nvm-sh/nvm
Hi just a curious wanderer here, I don't want to get nvm use working in package.json, but I noticed that it does not work for me. I do want to get OS: Win10 running WSL Ubuntu "packageManager": "yarn@3.1.1", nvm debug output:
|
@chav-aniket i have no idea what “JustFile” is, or what takes a pckageManager key. nvm manages node, only; yarn is something separate, and nvm has nothing to do with that. |
@ljharb Sorry for the confusion the package manager key is from my package.json. Basically is there any reason why an nvm command would not be usable through package.json? |
@chav-aniket nvm is a per-shell per-user tool. If your package.json command isn't using your normal shell environment, then nvm wouldn't be available in it. |
Ah I see, I'll look into the environment, thanks @ljharb ! |
My case seems a bit different however, it may help someone. ProblemI have node.js 14 installed via nvm. All of my projects run on node 14. Recently Next.js 13 was launched and it required node.js 16. Each time I had to start the project, I had to run 'nvm use 16' . SolutionAdd a .nvmrc file on the project root directory and prepend the dev command inside package.json script to use node.js 16. .nvmrc file
package.json file "scripts": {
"dev": "source ~/.nvm/nvm.sh && nvm use && next"
}, |
@bibhuticoder Nice one almost works for me, on ubuntu 20.4 I had to use:
|
I tried the above (on MacOS), and it appears to work, but it doesn't. If I add the 'node -v' like below, it will show a different version of node.
|
Operating system and version:
macOS Mojave 10.14.6
nvm debug
output:nvm ls
output:How did you install
nvm
? (e.g. install script in readme, Homebrew):Homebrew
What steps did you perform?
I added
nvm use
to apackage.json
script, to make sure commands are executed in the proper Node context.What happened?
I had to unset three NPM parameters and re-source
nvm.sh
to make nvm run from a package.json:What did you expect to happen?
That NVM would be found as a global executable.
Is there anything in any of your profile files (
.bashrc
,.bash_profile
,.zshrc
, etc) that modifies thePATH
?I run Oh my ZSH.
The text was updated successfully, but these errors were encountered: