2

I wanted to do npm install -g. As it didn't work due to permissions I took the approach suggested from the accepted answer in https://stackoverflow.com/questions/47252451/permission-denied-when-installing-npm-modules-in-osx

Change the owner of npm's directories to the name of the current user (your username):

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

then I could run my npm install -g. But this is not where the story ends. I wanted to install another package with npm. (npm install identicon).

I received the following error

npm does not support Node.js v12.0.0 You should probably upgrade to a newer version of node as we can't make any promises that npm will work with this version. You can find the latest version at https://nodejs.org/ .... 

And now I receive the same error over and over again, whenever I use a npm command.

On SO someone else had this issue https://stackoverflow.com/questions/56805007/npm-warn-npm-does-not-support-node-js-v12-4-0?noredirect=1&lq=1 and the accepted answer suggested

Fix /usr/local permissions:

sudo chown -R id -un:id -gn /usr/local

Clean npm cache and uninstall it:

npm cache clean -f npm -g uninstall npm

Reinstall node (with npm)

brew reinstall node

Upgrade npm:

npm -g i npm

But the very first command failed:

chown: /usr/local: Operation not permitted 

In case this helps:

/usr/local root# ls -la total 0 -rw-r--r-- 1 myuser staff 0 Sep 20 2019 .com.apple.installer.keep drwxrwxr-x 2 myuser staff 64 Apr 24 2019 Caskroom drwxrwxr-x 103 myuser staff 3296 Nov 17 17:31 Cellar drwxrwxr-x 3 myuser staff 96 Nov 17 17:25 Frameworks drwxrwxr-x 22 myuser staff 704 Jan 14 05:49 Homebrew drwxrwxr-x 409 myuser staff 13088 Jan 14 05:49 bin drwxrwxr-x 10 myuser staff 320 Nov 17 17:23 etc drwxrwxr-x 192 myuser staff 6144 Nov 17 17:33 include drwxrwxr-x 620 myuser staff 19840 Nov 17 17:33 lib drwxr-xr-x 5 myuser staff 160 Mar 24 2019 man drwxrwxr-x 124 myuser staff 3968 Nov 17 17:34 opt drwxr-xr-x 3 myuser staff 96 Mar 24 2019 remotedesktop drwxrwxr-x 12 myuser staff 384 Nov 17 17:27 sbin drwxrwxr-x 34 myuser staff 1088 Nov 17 17:34 share drwxr-xr-x 4 myuser staff 128 Mar 24 2019 texlive drwxrwxr-x 4 myuser staff 128 May 16 2021 var 

I want to use npm again and have my permissions fixed. Maybe someone can point out good ressources for beginners, which can guide me through the process, when I experience something similar. Thank you.

My macbookpro: macOS Catalina (10.15.7)

11
  • sudo chown -R $(id -un):$(id -gn) /usr/local/* should work. But please run ls -l /usr/local/bin first and add the result to the quesiton (instead of the listing of /var/db/sudo which doesn't matter here). Commented Jan 14, 2022 at 7:04
  • the result of ls -l /usr/local/bin is couple hundred entries long, is this really right (feels wrong). Do you want to see a specific entry or a couple of entries? Commented Jan 14, 2022 at 7:14
  • If it is a couple of hundred, then by all means add them or (or put them on pastebin and share a link). there may be more things wrong then. But the intersting ones would be bin, share, lib and lib/node_modules. Commented Jan 14, 2022 at 7:18
  • used pastebin, to many characters for the body. Thank you for your help. Commented Jan 14, 2022 at 7:35
  • Ah, my bad. I wanted to see the content of /usr/local, not /usr/local/bin. Sorry for that, can you provide this (should be just a few lines)? Commented Jan 14, 2022 at 8:09

1 Answer 1

1

You can use [nvm][1] to install node for your user in ~/ to avoid using the system node, which requires permissions. nvm allows you to install and switch between multiple versions of node.

As stated in the readme:

When using nvm you do not need sudo to globally install a module with npm -g, so instead of doing sudo npm install -g npm, do npm i -g npm

1. Prerequisites

Before you start, uninstall node if you have it installed with homebrew:

brew uninstall --ignore-dependencies node brew uninstall --force node 

2. install nvm

Option 1: Using homebrew

Install nvm:

brew update brew install nvm 

Create the nvm directory:

mkdir ~/.nvm 

Add the following to ~/.zshrc:

export NVM_DIR="${HOME}/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm 

Option 2 (preferred): manual installation

To install nvm manually, download and run the install script and run it using either of the following cURL or Wget commands:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 

If that doesn’t work, you can manually download the install script and run it.

This creates a directory for nvm in $HOME:

mkdir ~/.nvm 

And adds source lines from the snippet below to~/.zshrc which loads nvm in every new terminal shell:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm 

3. Load nvm

You can either reset your current shell, start a new one, or source the .zshrc changes:

source ~/.zshrc 

To verify that nvm has been installed, do:

command -v nvm 

which should output nvm if the installation was successful.

4. Install node using nvm

Install node 14 using nvm and set it as the default version:

nvm install 14 

Make node 14 the default version:

nvm alias default 14 

Set default as the current version to use:

nvm use default 

Upgrade npm installed for node 14 to the latest version:

npm install -g npm@latest 
8
  • how do I know if I installed it with homebrew? Can I look this up? Commented Jan 14, 2022 at 15:40
  • @YvesBoutellier you can run brew list and see if it comes up in the output Commented Jan 14, 2022 at 16:45
  • thanks! it was indeed installed with brew. I could install it manually. Why do you recommend node version 14? Commented Jan 17, 2022 at 5:06
  • I encountered a new error. Whenever I restart my computer, the nvm is not installed anymore. And node is the old version 12.0.0. The file .nvm is still there. When I manually run the installation script . ~/.nvm/nvm.sh and use nvm install node it works again. How can I make my changes persistent? Commented Jan 17, 2022 at 6:17
  • 1
    yes this worked: "export NVM_DIR=~/.nvm and source $NVM_DIR/nvm.sh to your ~/.zshrc file", now it's persistent Commented Jan 20, 2022 at 4:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.