How to resolve NVM is not compatible with the npm config prefix option issue

The npm prefix option is used to set the location of globally installed packages.

When the prefix is set, then the package you install with the npm install -g command will be saved under that location.

To see if the prefix option is set, you can run the npm config get prefix or npm prefix -g command:

$ npm config get prefix
/Users/nsebhastian/.npm-global
# Or
$ npm prefix -g
/Users/nsebhastian/.npm-global

When you use NVM to manage multiple Node versions on your computer, NVM will produce a warning message when you have a prefix configuration set.

The warning message is as follows:

$ nvm use 16.13.0
nvm is not compatible with the npm config "prefix" option: 
currently set to "/usr/local"
Run `npm config delete prefix` 
or `nvm use --delete-prefix v16.13.0` to unset it.

NVM is a program designed to install multiple Node and npm versions on your computer.

When you run a global install command, the package should be installed under the active Node version folder.

For example, if you use Node v.16.13.0, then the prefix path should be similar to this:

/Users/nsebhastian/.nvm/versions/node/v16.13.0

If you switch the Node version with the nvm use command, then the prefix will also change as follows:

$ npm prefix -g
/Users/nsebhastian/.nvm/versions/node/v16.13.0
$ nvm use 10.24.1
Now using node v10.24.1 (npm v6.14.12)
$ npm prefix -g
/Users/nsebhastian/.nvm/versions/node/v10.24.1

When you set a prefix configuration, then running npm install -g will put the package inside the prefix location instead of the NVM node/ folders.

The prefix config will block NVM from running global installs properly. But local installation works fine.

To resolve this issue, remove the config using either npm config delete prefix or nvm use --delete-prefix <version> command.

Once you run the command, the prefix config should be removed from your .npmrc file. NVM will use the Node version prefix path again.

If you still see the message appears when you run the nvm use command, then you may have the prefix option set in more than one .npmrc file. You need to solve this issue manually.

Run the following command to find the locations of your .npmrc files:

npm config ls -l | grep config

The output will be similar to this:

$ npm config ls -l | grep config  
; "default" config from default values
globalconfig = "/Users/nsebhastian/.nvm/versions/node/v16.13.0/etc/npmrc" 
userconfig = "/Users/nsebhastian/.npmrc" 
; "user" config from /Users/nsebhastian/.npmrc
; "cli" config from command line options

Pay attention to the globalconfig and the userconfig output. Check that both config files don’t have the prefix option set as shown below:

registry=https://registry.npmjs.org/
prefix=~/.npm-global

Once all prefix configs are removed, the NVM prefix message should disappear.

In the latest NVM version, the warning message has changed to show the .npmrc file location that causes the issue:

$ nvm use v16.13.0                
Your user’s .npmrc file (${HOME}/.npmrc)
has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.
Run `nvm use --delete-prefix v16.13.0` to unset it

As you can see, the latest version warning message is more useful than the previous one.

Both can be resolved by removing the prefix option.

Handling the EACCES permission error with NVM prefix

Most of the time, the prefix option is set to avoid permission issues blocking the npm install command.

When using NVM, the prefix is not needed because NVM installs a global package in a folder that doesn’t require superuser permissions.

But if you can’t run the npm install -g command after removing the prefix because of permission issues, set the prefix to the current node version as shown below:

npm config set prefix $NVM_DIR/versions/node/<version>

# Example:
# npm config set prefix $NVM_DIR/versions/node/v16.13.0

Please note that when you run the nvm use command to change the version, you need to run the set prefix command again.

And that’s how you resolve the NVM is not compatible with the npm config prefix option issue. Good work! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.