Nvm: Run nvm from package.json script

Created on 8 Oct 2019  路  22Comments  路  Source: nvm-sh/nvm

  • 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
    
bugs

Most helpful comment

Let me try to do that sometime this week.

All 22 comments

Unfortunately your expectation is wrong; nvm is not an executable, it鈥檚 a sourced shell function. Sourcing it in each npm run command is required, by its nature.

I鈥檓 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 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.

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鈥檛 be able to function as an executable - it has to be able to modify the current shell鈥檚 PATH.

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

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.

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.

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

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.

and the capitalization of whoami and echo $HOME matches?

username & /Users/username respectively.

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:

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

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)

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 https://github.com/nvm-sh/nvm/commit/6fa92c82caef03081de0adca99f6256e5fd209ca with a fix. In the next release, it will work.

I missed the commit hash in the close message... 馃う鈥嶁檪 Thanks @ljharb!

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?

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",
Was this page helpful?
0 / 5 - 0 ratings