Fix NodeJS wrong path `/usr/local/lib` vs `/usr/share/nodejs/npm` under Ubuntu 21.04?

Viewed 114

I have a rather fresh Ubuntu-MATE 21.04 setup, not having done much with npm and nodejs yet. Installed it through

sudo apt-get -y install nodejs
sudo apt-get install -q -y gcc g++ make npm yarn

npm --version
7.5.2

What I wanted (for some reason) to only slightly upgrade to 7.5.6

$> npm install -g npm@7.5.6

    ...
    npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'
    ...

And now, this is interesting. There is nothing node/npm-related in /usr/local/lib/, it is all inside /usr/share/nodejs:

$>  ll /usr/local/lib/
    drwxrwsr-x 4 root staff 4,0K 2021-06-28 11:48  python2.7
    drwxr-xr-x 3 root root  4,0K 2021-06-28 11:30  python3.9
$> which npm
    /usr/bin/npm
$> ll /usr/bin/ | grep npm
    lrwxrwxrwx 1 root root   npm -> ../share/nodejs/npm/bin/npm-cli.js
    lrwxrwxrwx 1 root root   npx -> ../share/nodejs/npm/bin/npx-cli.js
$> ll /usr/share/nodejs/npm
    drwxr-xr-x  3 root root   bin
    drwxr-xr-x  5 root root   lib
    lrwxrwxrwx  1 root root   man -> ../../man
    drwxr-xr-x 77 root root   node_modules
    -rw-r--r--  1 root root   npmrc
    -rw-r--r--  1 root root   package.json

So, what is the right thing to do now? How to talk npm out of that wrong /usr/local/lib/ folder?

I am also aware of the discussion, that nodejs and npm should typically installed with normal user rights und thus any npm install -g ... without sudo as well. I would like to conform to that, too.

0 Answers
Related