Thursday, June 7, 2018

Npm global not being used?

Leave a Comment

So I've previously had some npm issues and followed some stackoverflow/github issues commands to attempt to fix them. Which worked... Kinda. Everything was fine until recently when I realised that my global npm packages are not actually being read/used.

So whenever I did a npm -g install xxx, or an update. It updates the global folder alright, but it's just not being used. So I went to dig a little and found this.

Where npm is reading/using from

usr/local/lib/node_modules 

Where the global is installing to

Users/Me/.npm-global/lib/node_modules 

So whenever I did a -g check for my modules and stuff, everything is good and updated, but when I actually try to run anything, the npm command uses the one in the older folders. So basically I can't update or install anything using -g. And nothing goes into the old folder which is the one being actually used.

How do I resolve this? Was some linking or profile got screwed?

3 Answers

Answers 1

It looks like this is about your npm prefix configuration. Global Prefix is the folder where npm will install global packages.

First I would run the following command to get the value of the global prefix (https://docs.npmjs.com/cli/prefix)

$ npm prefix -g 

To set it to a different value:

npm config set prefix /usr/local/lib/node_modules 

This is for the location of your global packages, now you need to check that your terminal PATH variable is checking this folder for binaries.

$ echo $PATH 

Command above will print a ":" separated list of folder location that your shell checks for binaries.

Your global npm prefix location should be part of that list, from your question I'd assume this folder /usr/local/lib/node_modules already is.

Answers 2

Below are the steps to change the home directory for global npm installations for currently logged in user:

  1. Make a directory for global installations:

    mkdir ~/.npm-global

  2. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'

  3. Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH

  4. Back on the command line, update your system variables:

    source ~/.profile

  5. Test: Download a package globally without using sudo.

    npm install -g <package_name>

Instead of steps 1-5, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global 

The above configuration would not work if you use sudo to install the npm modules as it will follow the configurations set for root user.

Answers 3

You need to respect the order of load.

Loading from node_modules Folders

If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.

If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then Node.js would look in the following locations, in this order:

  • /home/ry/projects/node_modules/bar.js
  • /home/ry/node_modules/bar.js
  • /home/node_modules/bar.js
  • /node_modules/bar.js

This allows programs to localize their dependencies, so that they do not clash.

It is possible to require specific files or sub modules distributed with a module by including a path suffix after the module name. For instance require('example-module/path/to/file') would resolve path/to/file relative to where example-module is located. The suffixed path follows the same module resolution semantics.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment