Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Npm global not being used?

Tags:

node.js

npm

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?

like image 865
Kelvin Zhao Avatar asked May 25 '18 12:05

Kelvin Zhao


People also ask

Why is npm not working?

If your npm is broken: On Mac or Linux, reinstall npm. Windows: If you're on Windows and you have a broken installation, the easiest thing to do is to reinstall node from the official installer (see this note about installing the latest stable version).


2 Answers

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.

like image 179
maxletou Avatar answered Nov 29 '22 09:11

maxletou


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.

like image 44
Abhishek Singh Avatar answered Nov 29 '22 07:11

Abhishek Singh