Nvm: NVM not adding node_modules to path

Created on 25 Mar 2019  路  16Comments  路  Source: nvm-sh/nvm

  • Operating system and version: macOS 10.14.2

  • nvm debug output:


    nvm --version: v0.34.0
    $TERM_PROGRAM: iTerm.app
    $SHELL: /bin/bash
    $SHLVL: 1
    $HOME: /Users/jdelman
    $NVM_DIR: '$HOME/.nvm'
    $PATH: $NVM_DIR/versions/node/v10.15.3/bin:~/snaps/env/bin:~/snaps/snaps-resources/scripts/docker:$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
    $PREFIX: ''
    $NPM_CONFIG_PREFIX: ''
    $NVM_NODEJS_ORG_MIRROR: ''
    $NVM_IOJS_ORG_MIRROR: ''
    shell version: 'GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)'
    uname -a: 'Darwin 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64'
    OS version: Mac 10.14.2 18C54
    curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.4 zlib/1.2.11 nghttp2/1.24.1
    wget: /usr/local/bin/wget, GNU Wget 1.19.1 built on darwin16.6.0.
    git: /usr/local/bin/git, git version 2.14.1
    grep: /usr/bin/grep, 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: /bin/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.3
    which node: $NVM_DIR/versions/node/v10.15.3/bin/node
    which iojs:
    which npm: $NVM_DIR/versions/node/v10.15.3/bin/npm
    npm config get prefix: $NVM_DIR/versions/node/v10.15.3
    npm root -g: $NVM_DIR/versions/node/v10.15.3/lib/node_modules
    

  • nvm ls output:


        v6.11.1
         v8.1.4
         v8.6.0
        v8.11.3
        v9.11.1
    ->     v10.15.3
         system
    default -> 10 (-> v10.15.3)
    node -> stable (-> v10.15.3) (default)
    stable -> 10.15 (-> v10.15.3) (default)
    iojs -> N/A (default)
    unstable -> N/A (default)
    lts/* -> lts/dubnium (-> v10.15.3)
    lts/argon -> v4.9.1 (-> N/A)
    lts/boron -> v6.17.0 (-> N/A)
    lts/carbon -> v8.15.1 (-> N/A)
    lts/dubnium -> v10.15.3
    

  • How did you install nvm? (e.g. install script in readme, Homebrew):
    Install script in README

  • What steps did you perform?
    Ran npm link to link a package to nvm-scoped "global" node_modules folder, but can't require that package elsewhere.

The node_modules path does not show up in module.paths:

[ '/Users/jdelman/repl/node_modules',
  '/Users/jdelman/node_modules',
  '/Users/node_modules',
  '/node_modules',
  '/Users/jdelman/.node_modules',
  '/Users/jdelman/.node_libraries',
  '/Users/jdelman/.nvm/versions/node/v10.15.3/lib/node' ]

I would expect that last path to end /node_modules rather than /node - that folder doesn't even exist:

ls: /Users/jdelman/.nvm/versions/node/v10.15.3/lib/node: No such file or directory

Most helpful comment

@Gerst20051 @ljharb

Because https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders node will resolve:

1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node
4. $NODE_PATH

Then I add a NODE_PATH to make this path a possible path to lookup modules.

export NODE_PATH="$(npm config get prefix)/lib/node_modules"

Don't know if there are any side effects though.

All 16 comments

Global modules are not requireable. Using npm link requires also running npm link foo in the project you want to use it in.

@ljharb Thanks - did this policy change at some point? I have nvm installed on another machine and it works fine.

Many many years ago, yes - nvm shouldn鈥檛 have ever made that possible to begin with, and i removed it awhile back.

Ok, thanks. Closing.

So I guess the globally installed npm packages are not usable via require but only via cli for the symlinks to the node_modules folder in the bin folder ~/.nvm/versions/node/v8.16.1/bin

I was also wondering why module.paths has ~/.nvm/versions/node/v8.16.1/lib/node instead of ~/.nvm/versions/node/v8.16.1/lib/node_modules.

@Gerst20051 yes, that's correct. global things are not requireable; requireable things are dependencies and thus must be locally installed.

Do you have any insight into why ~/.nvm/versions/node/v8.16.1/lib/node is in the module.paths when it's not a valid directory? I'm curious about what purpose it serves? @ljharb

I don't; that's something node itself does. module.paths isn't generally used by anyone for anything, though.

@Gerst20051 @ljharb

Because https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders node will resolve:

1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node
4. $NODE_PATH

Then I add a NODE_PATH to make this path a possible path to lookup modules.

export NODE_PATH="$(npm config get prefix)/lib/node_modules"

Don't know if there are any side effects though.

@icese7en yes, but requiring global packages is one of the biggest antipatterns that can exist in node. If you require it, it鈥檚 a dependency, so it should be locally installed and in a package.json.

@icese7en yes, but requiring global packages is one of the biggest antipatterns that can exist in node. If you require it, it鈥檚 a dependency, so it should be locally installed and in a package.json.

yes, I only use this feature for global eslint in the webstorm for the projects which don't use eslint. not for requiring global packages.

You shouldn鈥檛 have a global eslint anyways :-)

If a project doesn鈥檛 have eslint locally, it shouldn鈥檛 be linted.

I hate to necro this, but I disagree with the statement:

nvm shouldn鈥檛 have ever made that possible to begin with

This is actually pretty valuable when playing in REPL. Being able to require something to play around is pretty valuable with me, especially when i don't want to mess with a project. For example, I'm wanting to test creating some JWT tokens for a project that doesn't even use Node. I don't want to initialize a bunch of stuff and mess with gitignoring, just to run some JS. Is there any workaround for automatically adding the global node_modules path for whatever version of node i'm running?

@LordZardeck that it's occasionally useful doesn't mean it's a good idea for nvm to do something that contradicts the way node itself works.

Separately, I'd say if you want to require something to play around, you should be installing it locally regardless. If you don't want to mess with a project, luckily any project worth using is using version control, so there's no danger of doing that accidentally :-)

I hate to necro this, but I disagree with the statement:

I love to necro... it has the greatest context possible.

nvm shouldn鈥檛 have ever made that possible to begin with

This is actually pretty valuable when playing in REPL. Being able to require something to play around is pretty valuable with me, especially when i don't want to mess with a project. For example, I'm wanting to test creating some JWT tokens for a project that doesn't even use Node. I don't want to initialize a bunch of stuff and mess with gitignoring, just to run some JS. Is there any workaround for automatically adding the global node_modules path for whatever version of node i'm running?

Then use a repl tool that deal with this:

@icese7en yes, but requiring global packages is one of the biggest antipatterns that can exist in node. If you require it, it鈥檚 a dependency, so it should be locally installed and in a package.json.

yes, I only use this feature for global eslint in the webstorm for the projects which don't use eslint. not for requiring global packages.

Get a better editor:

  • vscode + vscode-remote
Was this page helpful?
0 / 5 - 0 ratings