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

Run nvm from package.json script #2106

Closed
guidobouman opened this issue Oct 8, 2019 · 30 comments
Closed

Run nvm from package.json script #2106

guidobouman opened this issue Oct 8, 2019 · 30 comments
Labels
bugs Oh no, something's broken :-(

Comments

@guidobouman
Copy link
Contributor

guidobouman commented Oct 8, 2019

  • Operating system and version:
    macOS Mojave 10.14.6

  • nvm debug output:

    nvm --version: v0.33.11
    $TERM_PROGRAM: vscode
    $SHELL: /bin/zsh
    $SHLVL: 1
    $HOME: /Users/guidobouman
    $NVM_DIR: '$HOME/.nvm'
    $PATH: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Applications/Wireshark.app/Contents/MacOS:$NVM_DIR/versions/node/v10.15.1/bin:$HOME/Projects/VS.Terminal.Scripts/bash
    $PREFIX: ''
    $NPM_CONFIG_PREFIX: ''
    $NVM_NODEJS_ORG_MIRROR: ''
    $NVM_IOJS_ORG_MIRROR: ''
    shell version: 'zsh 5.3 (x86_64-apple-darwin18.0)'
    uname -a: 'Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64'
    OS version: Mac 10.14.6 18G103
    curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.5 zlib/1.2.11 nghttp2/1.24.1
    wget: not found
    git: /usr/local/bin/git, git version 2.18.0
    grep: grep: aliased to grep  --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}), grep (BSD grep) 2.5.1-FreeBSD
    awk: /usr/bin/awk, awk version 20070501
    sed: illegal option -- -
    usage: sed script [-Ealn] [-i extension] [file ...]
          sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
    sed: /usr/bin/sed, 
    cut: illegal option -- -
    usage: cut -b list [-n] [file ...]
          cut -c list [file ...]
          cut -f list [-s] [-d delim] [file ...]
    cut: /usr/bin/cut, 
    basename: illegal option -- -
    usage: basename string [suffix]
          basename [-a] [-s suffix] string [...]
    basename: /usr/bin/basename, 
    rm: illegal option -- -
    usage: rm [-f | -i] [-dPRrvW] file ...
          unlink file
    rm: /bin/rm, 
    mkdir: illegal option -- -
    usage: mkdir [-pv] [-m mode] directory ...
    mkdir: mkdir: aliased to nocorrect mkdir (nocorrect mkdir), 
    xargs: illegal option -- -
    usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
                [-L number] [-n number [-x]] [-P maxprocs] [-s size]
                [utility [argument ...]]
    xargs: /usr/bin/xargs, 
    nvm current: v10.15.1
    which node: $NVM_DIR/versions/node/v10.15.1/bin/node
    which iojs: iojs not found
    which npm: $NVM_DIR/versions/node/v10.15.1/bin/npm
    npm config get prefix: $NVM_DIR/versions/node/v10.15.1
    npm root -g: $NVM_DIR/versions/node/v10.15.1/lib/node_modules
  • nvm ls output:

            v8.11.1
          v10.13.0
          v10.14.1
          v10.14.2
    ->     v10.15.1
          v10.15.3
    default -> 8.11.1 (-> v8.11.1)
    node -> stable (-> v10.15.3) (default)
    stable -> 10.15 (-> v10.15.3) (default)
    iojs -> N/A (default)
    lts/* -> lts/dubnium (-> N/A)
    lts/argon -> v4.9.1 (-> N/A)
    lts/boron -> v6.17.1 (-> N/A)
    lts/carbon -> v8.16.1 (-> N/A)
    lts/dubnium -> v10.16.3 (-> N/A)
  • How did you install nvm? (e.g. install script in readme, Homebrew):
    Homebrew

  • What steps did you perform?
    I added nvm use to a package.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:

    {
      "scripts": {
        "nvm": "unset npm_package_scripts_nvm && unset npm_config_prefix && unset npm_lifecycle_script  && source ~/.nvm/nvm.sh && nvm use",
      },
    }
  • 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 the PATH?
    I run Oh my ZSH.

    export PATH=$PATH:$HOME/Projects/VS.Terminal.Scripts/bash
@ljharb
Copy link
Member

ljharb commented Oct 8, 2019

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.

@guidobouman
Copy link
Contributor Author

guidobouman commented Oct 9, 2019

Does transforming it into an executable not make sense? It would prevent a lot of unexpected behaviour cases. (When searching the issues for sourced shell function, I see a lot of repetition.)

About the nvm location: Now that I'm digging through this, I see that nvm is not installed through brew, that must have been my previous machine. Is there a quick way to confirm it was actually installed through the curl script?

PS: Exactly the same NPM environment vars had to be unset from a colleague's machine. So this seems to be something systematic.

@ljharb
Copy link
Member

ljharb commented Oct 9, 2019

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?

@ljharb
Copy link
Member

ljharb commented Oct 9, 2019

Also no, nvm wouldn’t be able to function as an executable - it has to be able to modify the current shell’s PATH.

@guidobouman
Copy link
Contributor Author

Without the unsets:

{
  "scripts": {
    "nvm": "source ~/.nvm/nvm.sh && nvm use",
  },
}

Sourcing nvm.sh errors on an incompatible env var being present, in this order:

  • npm_config_prefix
  • npm_lifecycle_script
  • npm_package_scripts_nvm

@ljharb
Copy link
Member

ljharb commented Oct 10, 2019

npm_config_prefix definitely shouldn't be set; but i'm not sure why npm run would set that either. if you run that command in your normal shell, is it a problem?

The other two seem like perhaps a bug in nvm.

@guidobouman
Copy link
Contributor Author

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 nvm use:

$ 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 nvm use gets to do its thing.

@ljharb
Copy link
Member

ljharb commented Oct 11, 2019

Hmm - is there any chance your homedir or $NVM_DIR is a symlink?

@guidobouman
Copy link
Contributor Author

guidobouman commented Oct 14, 2019

I don't think so. My Desktop & Documents folders are stored in iCloud. But that does not have any effect on my home or $NVM_DIR (/Users/[username]/.nvm) directories.

Running ls -al does not show any links.

@ljharb
Copy link
Member

ljharb commented Oct 16, 2019

and the capitalization of whoami and echo $HOME matches?

@guidobouman
Copy link
Contributor Author

username & /Users/username respectively.

@ljharb
Copy link
Member

ljharb commented Oct 18, 2019

Is there any chance you can make a small repo that this issue can be reproduced in?

@guidobouman
Copy link
Contributor Author

Let me try to do that sometime this week.

@ljharb ljharb added the needs followup We need some info or action from whoever filed this issue/PR. label Oct 29, 2019
@dgranahan
Copy link

Is there any update on this? I'm seeing the same issue.

@ljharb
Copy link
Member

ljharb commented Nov 26, 2019

I still haven't seen a repro repo.

@guidobouman
Copy link
Contributor Author

Sorry for the UBER long delay, there's the repro repo:

https://github.com/guidobouman/nvm-repro

@ljharb
Copy link
Member

ljharb commented Dec 16, 2019

aha, interesting. it seems that an npm run-script sets the npm_config_prefix env variable.

(nvm debug always exits with 42, so your npm test will always fail, but that's unrelated)

@ljharb ljharb closed this as completed in 6fa92c8 Dec 16, 2019
@ljharb ljharb added bugs Oh no, something's broken :-( and removed needs followup We need some info or action from whoever filed this issue/PR. labels Dec 16, 2019
@guidobouman
Copy link
Contributor Author

guidobouman commented Dec 16, 2019

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?

@ljharb
Copy link
Member

ljharb commented Dec 16, 2019

You misunderstand :-) I closed the issue in 6fa92c8 with a fix. In the next release, it will work.

@guidobouman
Copy link
Contributor Author

guidobouman commented Dec 16, 2019

I missed the commit hash in the close message... 🤦‍♂ Thanks @ljharb!

@andycochrane
Copy link

You misunderstand :-) I closed the issue in 6fa92c8 with a fix. In the next release, it will work.

I was having the same issue and this commit fixed it for me. Thanks! Is there a date scheduled for the next release?

@AskApache
Copy link

AskApache commented Dec 20, 2019

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",

aimeelramirez added a commit to AnalogStudiosRI/www.analogstudios.net that referenced this issue Dec 14, 2021
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
@chav-aniket
Copy link

chav-aniket commented Feb 28, 2022

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 nvm use working in my JustFile however. So yeah I thought if I can get it working in package.json then maybe I can get it to work in my JustFile. Is there any reason for why it may not be working for me using yarn? Could yarn3 be the issue? Thank you for any help!

OS: Win10 running WSL Ubuntu

"packageManager": "yarn@3.1.1",

nvm debug output:

$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.8 (x86_64-ubuntu-linux-gnu)'
uname -a: 'Linux 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux'
checksum binary: 'sha256sum'
OS version: Ubuntu 20.04.4 LTS
curl: /usr/bin/curl, curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3
wget: /usr/bin/wget, GNU Wget 1.20.3 built on linux-gnu.
git: /usr/bin/git, git version 2.25.1
ls: cannot access 'grep:': No such file or directory
grep: grep: aliased to grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}), grep (GNU grep) 3.4
awk: /usr/bin/awk, GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)
sed: /usr/bin/sed, sed (GNU sed) 4.7
cut: /usr/bin/cut, cut (GNU coreutils) 8.30
basename: /usr/bin/basename, basename (GNU coreutils) 8.30
rm: /usr/bin/rm, rm (GNU coreutils) 8.30
mkdir: /usr/bin/mkdir, mkdir (GNU coreutils) 8.30
xargs: /usr/bin/xargs, xargs (GNU findutils) 4.7.0
nvm current: v16.13.2
which node: ${NVM_DIR}/versions/node/v16.13.2/bin/node
which iojs: iojs not found
which npm: ${NVM_DIR}/versions/node/v16.13.2/bin/npm
npm config get prefix: ${NVM_DIR}/versions/node/v16.13.2
npm root -g: ${NVM_DIR}/versions/node/v16.13.2/lib/node_modules

@ljharb
Copy link
Member

ljharb commented Feb 28, 2022

@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.

@chav-aniket
Copy link

chav-aniket commented Feb 28, 2022

@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?

@ljharb
Copy link
Member

ljharb commented Feb 28, 2022

@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. npm run certainly works for nvm; i'm not sure if yarn does but I'd assume it does.

@chav-aniket
Copy link

Ah I see, I'll look into the environment, thanks @ljharb !

@bibhuticoder
Copy link

bibhuticoder commented Nov 4, 2022

My case seems a bit different however, it may help someone.

Problem

I 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' .

Solution

Add a .nvmrc file on the project root directory and prepend the dev command inside package.json script to use node.js 16.

.nvmrc file

v16.15.1

package.json file

  "scripts": {
    "dev": "source ~/.nvm/nvm.sh && nvm use && next"
  },

@Recodify
Copy link

Recodify commented Nov 4, 2022

@bibhuticoder Nice one almost works for me, on ubuntu 20.4 I had to use:

  "scripts": {
    "dev": ". ~/.nvm/nvm.sh && nvm use && next"
  },

@kbazzani
Copy link

kbazzani commented Sep 30, 2023

"scripts": {
    "dev": "source ~/.nvm/nvm.sh && nvm use && next"
  },

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.

  "scripts": {
    "dev": "source ~/.nvm/nvm.sh && nvm use && node -v && next"
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugs Oh no, something's broken :-(
Projects
None yet
Development

No branches or pull requests

9 participants