105

I am trying install typescript with command npm install -g typescript, and it returns this error:

    npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
    npm ERR!     at Error (native)
    npm ERR!  { Error: EACCES: permission denied, access '/usr/lib/node_modules'
    npm ERR!     at Error (native)
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'access',
    npm ERR!   path: '/usr/lib/node_modules' }
    npm ERR! 
    npm ERR! Please try running this command again as root/Administrator.
    npm ERR! Linux 4.4.0-93-generic
    npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "typescript"
    npm ERR! node v6.11.2
    npm ERR! npm  v3.10.10
    npm ERR! path npm-debug.log.1024969454
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall open
    
    npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.1024969454'
    npm ERR!     at Error (native)
    npm ERR!  { Error: EACCES: permission denied, open 'npm-debug.log.1024969454'
    npm ERR!     at Error (native)
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'open',
    npm ERR!   path: 'npm-debug.log.1024969454' }
    npm ERR! 
    npm ERR! Please try running this command again as root/Administrator.
    
    npm ERR! Please include the following file with any support request:
    npm ERR!     /npm-debug.log
    jramirez@jramirez:/$ ^C
    jramirez@jramirez:/$ npm install typescript
    npm WARN checkPermissions Missing write access to /
    /
    └── typescript@2.5.2 
    
    npm WARN enoent ENOENT: no such file or directory, open '/package.json'
    npm WARN !invalid#1 No description
    npm WARN !invalid#1 No repository field.
    npm WARN !invalid#1 No README data
    npm WARN !invalid#1 No license field.
    npm ERR! Linux 4.4.0-93-generic
    npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "typescript"
    npm ERR! node v6.11.2
    npm ERR! npm  v3.10.10
    npm ERR! path /
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall access
    
    npm ERR! Error: EACCES: permission denied, access '/'
    npm ERR!     at Error (native)
    npm ERR!  { Error: EACCES: permission denied, access '/'
    npm ERR!     at Error (native) errno: -13, code: 'EACCES', syscall: 'access', path: '/' }
    npm ERR! 
    npm ERR! Please try running this command again as root/Administrator.
    npm ERR! Linux 4.4.0-93-generic
    npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "typescript"
    npm ERR! node v6.11.2
    npm ERR! npm  v3.10.10
    npm ERR! path npm-debug.log.2387664261
    npm ERR! code EACCES
    npm ERR! errno -13
    npm ERR! syscall open
    
    npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.2387664261'
    npm ERR!     at Error (native)
    npm ERR!  { Error: EACCES: permission denied, open 'npm-debug.log.2387664261'
    npm ERR!     at Error (native)
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'open',
    npm ERR!   path: 'npm-debug.log.2387664261' }
    npm ERR! 
    npm ERR! Please try running this command again as root/Administrator.
    
    npm ERR! Please include the following file with any support request:
    npm ERR!     /npm-debug.log

Additionally, I run npm install typescript (without -g), but it doesn't work. Does there exist any other way to install typescript? My OS is Linux Ubuntu 16.04. I seldom use node and don't know like to fix this issue.

My question is: exactly which directories need that permission?

Peter Huber
  • 3,052
  • 2
  • 30
  • 42
jjoselon
  • 2,641
  • 4
  • 23
  • 37

8 Answers8

344

It's not recommended to use sudo with npm install, follow the steps from npmjs official docs instead.

Make a directory for global installations:

mkdir ~/.npm-global

Configure npm to use the new directory path:

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

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

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

Back on the command line, update your system variables:

source ~/.profile

Test: Download a package globally without using sudo.

npm install -g typescript

Source: https://docs.npmjs.com/getting-started/fixing-npm-permissions

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Euler Ribeiro Sudbrack
  • 3,631
  • 2
  • 8
  • 10
  • 6
    shouldn't step 3 `export PATH="~/.npm-global/bin:$PATH"` be put in `.bash_aliases` (included from `.bashrc`, or directly there...) for persistence? – Frank N Jul 20 '18 at 13:35
  • ThanksYOU!! I've been trying to get expo-cli installed using npm WSL for ages. Great answer. – damask Jul 22 '19 at 06:59
  • 3
    if you are using Z shell (Zsh) then add `export PATH=~/.npm-global/bin:$PATH` to your `~/.zshrc` and then source the file – Hom Bahrani Oct 06 '19 at 10:23
  • Just a note, some people probably already have typescript (or other global packages) installed and they just upgraded to Catalina. You don't need to reinstall those packages, just configure zsh by running through all the steps but make the source `.zprofile` – BrewMate Jan 26 '20 at 17:06
  • Why creating a new directory solved the issue, isnt it a permission issue ? – user204069 Oct 23 '20 at 23:56
  • How do I delete `.npm-global`? it keeps coming back after `npm install -g npm@8` `rm- rf .npm-global` deletes it and install brings it back – dn1 Jul 19 '22 at 23:26
  • Not work it my linux server with NVM. npm config set prefix /home/myfolder/.nvm/versions/node/v16.18.1/bin/npm and so on, but not working. Any ideas – bandungeuy Dec 06 '22 at 08:13
19

When installing global packages in ubuntu, you need special write permissions as you are writing to the usr/bin folder. It is for security reasons.

So, everytime you install a global package, use:

sudo npm install -g [package-name]

for your specific case:

sudo npm install -g typescript

When you try it without -g all that you are doing is installing the package locally in whatever folder you are in:

so: npm install typescript will create a node_modules folder in the folder you are in.

Juan
  • 6,125
  • 3
  • 30
  • 31
  • 1
    Would it be terribly wrong to go for a `chmod g+rw` on `/usr/lib/node_modules/` instead? – Frank N Jul 14 '18 at 10:34
  • 28
    You should not use sudo with NPM install. – Abdalla Arbab Aug 16 '18 at 08:35
  • The notion that sudo should not be used with NPM is wrong and doesn't align with the paradigm of installing global binaries in locations specified by the UNIX [Filesystem Hierarchy Standard](http://www.pathname.com/fhs/pub/fhs-2.3.html#USRLIBLIBRARIESFORPROGRAMMINGANDPA). – wheeler Apr 02 '19 at 14:13
9

I have same issue with webpack server installation on globally, Use steps Solved my issue, my be work for you.

Steps mention above There: Back-up your computer before you start.

Make a directory for global installations:

1. mkdir ~/.npm-global

Configure npm to use the new directory path:

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

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

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

Back on the command line, update your system variables:

4. source ~/.profile

Test: Download a package globally without using sudo.

npm install -g jshint

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

NPM_CONFIG_PREFIX=~/.npm-global

OrderAndChaos
  • 3,547
  • 2
  • 30
  • 57
Vishal Patel
  • 1,715
  • 16
  • 26
3

Not very sure if this could be the right solution to this problem, but I hope this could help others with similar situation that already tried the following:

  • Applied the recommendation on the npm website
  • Checked for the right permissions for the node_modules folder and found nothing wrong
  • Used sudo npm install <package> (solution which I do not recommend anyway) with or without -g and had no expected result

The solution to this problem pointed out when I noticed that while I was trying to install the axios package with npm, the directory containing the entire project was opened in Visual Studio Code. So I closed it and retried installation again. Surprise, the installation worked like a charm.

For trying to replicate same problem I was using Ubuntu 18.04 LTS on WSL (maybe this has something to do with Windows permissions)

This could be the solution for this question also: Error: EACCES: permission denied, access '/usr/local/lib/node_modules' react

Cosmin
  • 31
  • 1
1

1) Go to the directory “/usr/local/lib/node_modules

2) Click on the folder and right click Get info

3) Click the lock at the bottom right

4) Key in your window password to unlock

5) Check both the value of Wheel and Everyone to Read & Write

6) Go back to your terminal and try to install the Typescript again

  • `wheel` is a BSD/RedHat thing, not in Debian based systems like Ubuntu https://unix.stackexchange.com/questions/4460/why-is-debian-not-creating-the-wheel-group-by-default – Novaterata Sep 12 '19 at 12:39
1

It is recommended to use nvm to manage your node versions and npm suggests this to be the recommended solution of access errors: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm

Zaffer
  • 1,290
  • 13
  • 32
0

According to the npm documentation - https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally :

If you see an EACCES error when you try to install a package globally, you can either:

  • Reinstall npm with a node version manager (recommended), or
  • Manually change npm’s default directory

I followed recommended way - installed nvm. its resolved my issue

desertnaut
  • 57,590
  • 26
  • 140
  • 166
vinzbruce
  • 11
  • 1
-3

this worked for me.

sudo npm install -g coffee-script

then

sudo npm install -g typescript
Fthi.a.Abadi
  • 324
  • 2
  • 8
  • 5
    Try to avoid using sudo with npm install which can be dangerous in terms of security. Follow the steps provided in npmjs official docs. https://docs.npmjs.com/getting-started/fixing-npm-permissions – Mihai Oct 16 '18 at 11:49