i can`t use npm install -g serve [duplicate] - node.js

What might be causing the error Error: EACCES: permission denied, access '/usr/local/lib/node_modules'?
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/macbookmd101/.npm/_logs/2018-02-21T16_26_08_421Z-debug.log

Change your file permissions... Like this
First check who owns the directory
ls -la /usr/local/lib/node_modules
it is denying access because the node_module folder is owned by root
drwxr-xr-x 3 root wheel 102 Jun 24 23:24 node_modules
so this needs to be changed by changing root to your user but first run command below to check your current user How do I get the name of the active user via the command line in OS X?
id -un OR whoami
Then change owner
sudo chown -R [owner]:[owner] /usr/local/lib/node_modules
OR
sudo chown -R ownerName: /usr/local/lib/node_modules
OR
sudo chown -R $USER /usr/local/lib/node_modules

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use a hidden directory in your home directory.
Back up your computer.
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create the ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
To test your new configuration, install a package globally without using sudo.
Source: the NPM website.

I tried the solution of the answer given by #okanda but it didn't work for me.
However it worked perfectly when I did it for several folders like mentioned in this thread: https://github.com/angular/angular-cli/issues/9676#issuecomment-464857493
sudo chown -R $USER /usr/local/lib/node_modules/
sudo chown -R $USER /usr/local/bin/
sudo chown -R $USER /usr/local/share/

All you need to do is to add USER to the owner of /local/lib
sudo chown -R $USER /usr/local/lib
EDIT :
To target precisely and only the node_modules folder, try using this command before using the previous one :
sudo chown -R $USER /usr/local/lib/node_modules

try appending sudo before whatever command you are trying.
like this : sudo npm install
Using sudo with a command in Linux/UNIX generally elevates your permissions to superuser levels. In Windows, the superuser account is usually called 'Administrator.' In Linux/Unix the superuser account is generally named 'root'.
The root user has permission to access, modify or delete almost any file on your computer. Normal user accounts can access, modify or delete many fewer files. The restrictions on a normal account protect your computer from unauthorized or harmful programs or users. Some processes require you to perform actions on files or folders you don't normally have permissions to access. Installing a program that everyone can access is one of these actions.
In your case, running the installation command with sudo gives you the permissions of the superuser, and allows you to modify files that your normal user doesn't have permission to modify.

You can install npm through Node version manager or a Node installer. In the docs it states:
We do not recommend using a Node installer, since the Node
installation process installs npm in a directory with local
permissions and can cause permissions errors when you run npm packages
globally.
NPM actually recommends using a Node Version Manager to avoid these errors.
Since you have the permission error, you probably installed npm through a Node installer and now you need to reinstalled it with a nvm (node version manager).
Luckily, this is very simple. You do not even need to remove your current version of npm or Node.js.
All you need to do is
Install nvm.
For OSX or Linux Node use:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<VERSION>/install.sh | bash
where <VERSION> should be replaced with the latest version
This creates a nvm folder in your home directory.
Then
Install npm and node.js through nvm. To do so, just call
nvm install node
("node" is an alias for the latest version)
Now you can install your package globally without using sudo or changing the owner of node_modules in usr folder.

If you are facing this issue on you Mac. Follow these steps
First checking who is owner of this file by using below command
ls -la /usr/local/lib/node_modules
you will find some file like below one of them is below
drwxr-xr-x 3 root wheel 768 May 29 02:21 node_modules
have you notice that above file is own by root, for make changes inside for you need to change owner ship of path.
you can use check who is current user by this command
id -un (in my case user is yamsol)
and then you can change by calling this command (just replace your user name with ownerName)
sudo chown -R ownerName: /usr/local/lib/node_modules
in my case as you know user is "yamsol" i will call this command in this way
sudo chown -R yamsol: /usr/local/lib/node_modules
thats it.

It looks like you're running into permission issues. If you are installing npm-packages then it might possible that you are getting an EACCES error when trying to install a package globally. This means you do not have permission to write to the directories npm uses to store global packages and commands.
Try running commands: sudo chmod u+x -R 775 ~/.npm and sudo chown $USER -R ~/.npm or you can just run any npm command with sudo, that should get resolve your issue.
If you are installing an npm-package locally, then you should be in your local project directory and can try running sudo npm install <pkg-name> command to install required package. the purpose of using sudo is that it will change your owner permissions so you can make your current user authorized to run npm commands.
I'd recommend you to take a look at https://docs.npmjs.com/getting-started/fixing-npm-permissions

While 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, it will be:
sudo npm install -g typescript

Be careful with all responses that change the owner of all directories under /usr/local
Basically, don't mess the Linux system!!!
Using sudo for your local stuff is a really bad recommendation as well.
The original link from www.competa.com is broken, so this is the original approach from there:
npm config set prefix ~/.npm
# open your .bashrc (Linux) or .bash_profile (Mac) file for editing:
nano ~/.bashrc # for Linux
# or...
nano ~/.bash_profile # for Mac if you haven't created a .bashrc file
# add these lines:
export PATH="$PATH:$HOME/npm/bin"
export NODE_PATH="$NODE_PATH:$HOME/npm/lib/node_modules"
# save the file and then enter this command or logout and login to make the changes take effect:
. ~/.bashrc
# or...
. ~/.bash_profile
Option B: Use a version manager like NVM

If it is still not working after giving permissions try running these commands:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
and finally test with this command
npm install -g jshint
This does not work for Windows.

I was trying to install react expo and apart from sudo I had to add --unsafe-perm
like this. This resolves my Issue
sudo npm install -g expo-cli --unsafe-perm

Review this carefully:
https://ionicframework.com/docs/developing/tips#resolving-permission-errors
sudo chown -R $(whoami) /usr/local/{lib/node_modules,bin,share}
sudo chown -R $(whoami) ~/.npm ~/.npmrc

For nvm users
sudo chown -R $USER /home/bereket/.nvm/versions/node/v8.9.1/lib/node_modules
and
sudo chown -R $USER /usr/local/lib/node_modules/
replace v8.9.1 with your node version you are using.

Encountered in CentOS 8 stream
Solution
(1/4) Creating node_modules folder
$ sudo mkdir /usr/local/lib/node_modules
(2/4) Own with Current User
$ sudo chown -R $USER /usr/local/lib/node_modules/
(3/4) Own the bin folder
$ sudo chown -R $USER /usr/local/bin/
(4/4) Own the share folder
$ sudo chown -R $USER /usr/local/share/

Seems like you tried to install a npm package globally rather than locally, as the man npm install describes:
The -g or --global argument will cause npm to install the package globally rather than locally.
Generally, when you are setting up a npm project (among many others that you could have), it's not a good idea to install packages on Node.js global modules (/usr/local/lib/node_modules), as your the debug log suggested.
Instead of using -g, use --save, which will automatically save the package as a dependency for your package.json file:
Like this:
$ npm install express-generator --save
$ cat package.json
{
"name": "first_app_generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ivanleoncz",
"license": "MIT",
"dependencies": {
"express-generator": "^4.16.0"
}
}
But as the other answers mentioned, if you're going to use -g, you have to use sudo (if your user has sudo privileges: see /etc/sudoers) when performing npm install express-generator -g, but indeed, it's not a good idea, possibly causing permission problems.
NOTICE
There are instructions for installing express-generator with -g option, in order to have the script express-cli.js available on the system path, but you can use the locally installed script as well, located at the node_modules if your npm project:
$ ./node_modules/express-generator/bin/express-cli.js --view=pug myapp
If a message like /usr/bin/env: ‘node’: No such file or directory shows up, install nodejs-legacy (Debian/Ubuntu)
IMHO, using -g (also using sudo) is like hic sunt dracones, if you are unsure of the consequences.
For further information:
Creating NPM Package (npm init)
Express.js Generator

I was able to fix the issue using the following in mac.
sudo npm install -g #aws-amplify/cli --unsafe-perm=true

This occurred as a result of npm not being able to access your global node_modules directory locally, running
sudo chown -R Name: /usr/local/lib/node_modules
e.g.
sudo chown -R developerayo: /usr/local/lib/node_modules
fixes the issue, now you can run the command you ran again.

For those of you still unable to fix the problem after using the above mentioned solutions. Try this
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
That should do the trick, cheers!

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

Use this command to install npm as the sudo user:
sudo npm install -g create-react-app
instead of
npm install -g create-react-a pp.

Below command worked for me:
sudo npm install -g appium --unsafe-perm=true --allow-root

Simply you can change the owner or just use sudo before you command like this
sudo chown -R [owner]:[owner] /usr/local/lib/node_modules (change owner)
or
sudo npm install -g json-server
that's it.

For linux / ubuntu if the command
npm install -g <package_name>
npm WARN deprecated superagent#4.1.0: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released.
npm ERR! path ../lib/node_modules/<package_name>/bin/..
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall symlink
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules
/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'
npm ERR! { [Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>']
npm ERR! cause:
npm ERR! { Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/<package_name>/bin/..',
npm ERR! dest: '/usr/local/bin/ionic' },
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, symlink \'../lib/node_modules/ionic/bin/ionic\' -> \'/usr/local/bin/ionic\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/<package-name>/bin/<package-name>',
npm ERR! dest: '/usr/local/bin/<package-name>' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
npm ERR! A complete log of this run can be found in:
npm ERR! /home/User/.npm/_logs/2019-07-29T01_20_10_566Z-debug.log
Fix : Install with root permissions
sudo npm install <package_name> -g

You need the permission of superuser levels to install React. In Linux/Unix the superuser account is generally named 'root'.
To get superuser privilege just run the following command on your terminal:
sudo -i
and then simply run the command to install React:
npm install -g create-react-app
However, the reactjs team encourages us to use the following command instead of installing a global package.
npx create-react-app app_name

sudo chown -R $USER /usr/local/lib/node_modules

Similar to POsha's answer but this is what worked for me on ubuntu 19
sudo npm i -g ngrok --unsafe-perm=true --allow-root
From this link
https://github.com/inconshreveable/ngrok/issues/429

Just add "sudo" before npm command. Thats it.

brew uninstall node
brew install node
sudo npm install -g "your_package" --unsafe-perm=true --allow-root
Hopes this helps someone

On my mac similar issue is coming while installing Ionic.
I run the command
sudo chown -R $USER /usr/local/ path you can update as given in error.

Related

I am unable to install nestjs in ubuntu 20.4 [duplicate]

What might be causing the error Error: EACCES: permission denied, access '/usr/local/lib/node_modules'?
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/macbookmd101/.npm/_logs/2018-02-21T16_26_08_421Z-debug.log
Change your file permissions... Like this
First check who owns the directory
ls -la /usr/local/lib/node_modules
it is denying access because the node_module folder is owned by root
drwxr-xr-x 3 root wheel 102 Jun 24 23:24 node_modules
so this needs to be changed by changing root to your user but first run command below to check your current user How do I get the name of the active user via the command line in OS X?
id -un OR whoami
Then change owner
sudo chown -R [owner]:[owner] /usr/local/lib/node_modules
OR
sudo chown -R ownerName: /usr/local/lib/node_modules
OR
sudo chown -R $USER /usr/local/lib/node_modules
To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use a hidden directory in your home directory.
Back up your computer.
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create the ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
To test your new configuration, install a package globally without using sudo.
Source: the NPM website.
I tried the solution of the answer given by #okanda but it didn't work for me.
However it worked perfectly when I did it for several folders like mentioned in this thread: https://github.com/angular/angular-cli/issues/9676#issuecomment-464857493
sudo chown -R $USER /usr/local/lib/node_modules/
sudo chown -R $USER /usr/local/bin/
sudo chown -R $USER /usr/local/share/
All you need to do is to add USER to the owner of /local/lib
sudo chown -R $USER /usr/local/lib
EDIT :
To target precisely and only the node_modules folder, try using this command before using the previous one :
sudo chown -R $USER /usr/local/lib/node_modules
try appending sudo before whatever command you are trying.
like this : sudo npm install
Using sudo with a command in Linux/UNIX generally elevates your permissions to superuser levels. In Windows, the superuser account is usually called 'Administrator.' In Linux/Unix the superuser account is generally named 'root'.
The root user has permission to access, modify or delete almost any file on your computer. Normal user accounts can access, modify or delete many fewer files. The restrictions on a normal account protect your computer from unauthorized or harmful programs or users. Some processes require you to perform actions on files or folders you don't normally have permissions to access. Installing a program that everyone can access is one of these actions.
In your case, running the installation command with sudo gives you the permissions of the superuser, and allows you to modify files that your normal user doesn't have permission to modify.
You can install npm through Node version manager or a Node installer. In the docs it states:
We do not recommend using a Node installer, since the Node
installation process installs npm in a directory with local
permissions and can cause permissions errors when you run npm packages
globally.
NPM actually recommends using a Node Version Manager to avoid these errors.
Since you have the permission error, you probably installed npm through a Node installer and now you need to reinstalled it with a nvm (node version manager).
Luckily, this is very simple. You do not even need to remove your current version of npm or Node.js.
All you need to do is
Install nvm.
For OSX or Linux Node use:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<VERSION>/install.sh | bash
where <VERSION> should be replaced with the latest version
This creates a nvm folder in your home directory.
Then
Install npm and node.js through nvm. To do so, just call
nvm install node
("node" is an alias for the latest version)
Now you can install your package globally without using sudo or changing the owner of node_modules in usr folder.
If you are facing this issue on you Mac. Follow these steps
First checking who is owner of this file by using below command
ls -la /usr/local/lib/node_modules
you will find some file like below one of them is below
drwxr-xr-x 3 root wheel 768 May 29 02:21 node_modules
have you notice that above file is own by root, for make changes inside for you need to change owner ship of path.
you can use check who is current user by this command
id -un (in my case user is yamsol)
and then you can change by calling this command (just replace your user name with ownerName)
sudo chown -R ownerName: /usr/local/lib/node_modules
in my case as you know user is "yamsol" i will call this command in this way
sudo chown -R yamsol: /usr/local/lib/node_modules
thats it.
It looks like you're running into permission issues. If you are installing npm-packages then it might possible that you are getting an EACCES error when trying to install a package globally. This means you do not have permission to write to the directories npm uses to store global packages and commands.
Try running commands: sudo chmod u+x -R 775 ~/.npm and sudo chown $USER -R ~/.npm or you can just run any npm command with sudo, that should get resolve your issue.
If you are installing an npm-package locally, then you should be in your local project directory and can try running sudo npm install <pkg-name> command to install required package. the purpose of using sudo is that it will change your owner permissions so you can make your current user authorized to run npm commands.
I'd recommend you to take a look at https://docs.npmjs.com/getting-started/fixing-npm-permissions
While 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, it will be:
sudo npm install -g typescript
Be careful with all responses that change the owner of all directories under /usr/local
Basically, don't mess the Linux system!!!
Using sudo for your local stuff is a really bad recommendation as well.
The original link from www.competa.com is broken, so this is the original approach from there:
npm config set prefix ~/.npm
# open your .bashrc (Linux) or .bash_profile (Mac) file for editing:
nano ~/.bashrc # for Linux
# or...
nano ~/.bash_profile # for Mac if you haven't created a .bashrc file
# add these lines:
export PATH="$PATH:$HOME/npm/bin"
export NODE_PATH="$NODE_PATH:$HOME/npm/lib/node_modules"
# save the file and then enter this command or logout and login to make the changes take effect:
. ~/.bashrc
# or...
. ~/.bash_profile
Option B: Use a version manager like NVM
If it is still not working after giving permissions try running these commands:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
and finally test with this command
npm install -g jshint
This does not work for Windows.
I was trying to install react expo and apart from sudo I had to add --unsafe-perm
like this. This resolves my Issue
sudo npm install -g expo-cli --unsafe-perm
Review this carefully:
https://ionicframework.com/docs/developing/tips#resolving-permission-errors
sudo chown -R $(whoami) /usr/local/{lib/node_modules,bin,share}
sudo chown -R $(whoami) ~/.npm ~/.npmrc
For nvm users
sudo chown -R $USER /home/bereket/.nvm/versions/node/v8.9.1/lib/node_modules
and
sudo chown -R $USER /usr/local/lib/node_modules/
replace v8.9.1 with your node version you are using.
Encountered in CentOS 8 stream
Solution
(1/4) Creating node_modules folder
$ sudo mkdir /usr/local/lib/node_modules
(2/4) Own with Current User
$ sudo chown -R $USER /usr/local/lib/node_modules/
(3/4) Own the bin folder
$ sudo chown -R $USER /usr/local/bin/
(4/4) Own the share folder
$ sudo chown -R $USER /usr/local/share/
Seems like you tried to install a npm package globally rather than locally, as the man npm install describes:
The -g or --global argument will cause npm to install the package globally rather than locally.
Generally, when you are setting up a npm project (among many others that you could have), it's not a good idea to install packages on Node.js global modules (/usr/local/lib/node_modules), as your the debug log suggested.
Instead of using -g, use --save, which will automatically save the package as a dependency for your package.json file:
Like this:
$ npm install express-generator --save
$ cat package.json
{
"name": "first_app_generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ivanleoncz",
"license": "MIT",
"dependencies": {
"express-generator": "^4.16.0"
}
}
But as the other answers mentioned, if you're going to use -g, you have to use sudo (if your user has sudo privileges: see /etc/sudoers) when performing npm install express-generator -g, but indeed, it's not a good idea, possibly causing permission problems.
NOTICE
There are instructions for installing express-generator with -g option, in order to have the script express-cli.js available on the system path, but you can use the locally installed script as well, located at the node_modules if your npm project:
$ ./node_modules/express-generator/bin/express-cli.js --view=pug myapp
If a message like /usr/bin/env: ‘node’: No such file or directory shows up, install nodejs-legacy (Debian/Ubuntu)
IMHO, using -g (also using sudo) is like hic sunt dracones, if you are unsure of the consequences.
For further information:
Creating NPM Package (npm init)
Express.js Generator
I was able to fix the issue using the following in mac.
sudo npm install -g #aws-amplify/cli --unsafe-perm=true
This occurred as a result of npm not being able to access your global node_modules directory locally, running
sudo chown -R Name: /usr/local/lib/node_modules
e.g.
sudo chown -R developerayo: /usr/local/lib/node_modules
fixes the issue, now you can run the command you ran again.
For those of you still unable to fix the problem after using the above mentioned solutions. Try this
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
That should do the trick, cheers!
It is work 100%
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Use this command to install npm as the sudo user:
sudo npm install -g create-react-app
instead of
npm install -g create-react-a pp.
Below command worked for me:
sudo npm install -g appium --unsafe-perm=true --allow-root
Simply you can change the owner or just use sudo before you command like this
sudo chown -R [owner]:[owner] /usr/local/lib/node_modules (change owner)
or
sudo npm install -g json-server
that's it.
For linux / ubuntu if the command
npm install -g <package_name>
npm WARN deprecated superagent#4.1.0: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released.
npm ERR! path ../lib/node_modules/<package_name>/bin/..
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall symlink
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules
/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'
npm ERR! { [Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>']
npm ERR! cause:
npm ERR! { Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/<package_name>/bin/..',
npm ERR! dest: '/usr/local/bin/ionic' },
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, symlink \'../lib/node_modules/ionic/bin/ionic\' -> \'/usr/local/bin/ionic\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/<package-name>/bin/<package-name>',
npm ERR! dest: '/usr/local/bin/<package-name>' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
npm ERR! A complete log of this run can be found in:
npm ERR! /home/User/.npm/_logs/2019-07-29T01_20_10_566Z-debug.log
Fix : Install with root permissions
sudo npm install <package_name> -g
You need the permission of superuser levels to install React. In Linux/Unix the superuser account is generally named 'root'.
To get superuser privilege just run the following command on your terminal:
sudo -i
and then simply run the command to install React:
npm install -g create-react-app
However, the reactjs team encourages us to use the following command instead of installing a global package.
npx create-react-app app_name
sudo chown -R $USER /usr/local/lib/node_modules
Similar to POsha's answer but this is what worked for me on ubuntu 19
sudo npm i -g ngrok --unsafe-perm=true --allow-root
From this link
https://github.com/inconshreveable/ngrok/issues/429
Just add "sudo" before npm command. Thats it.
brew uninstall node
brew install node
sudo npm install -g "your_package" --unsafe-perm=true --allow-root
Hopes this helps someone
On my mac similar issue is coming while installing Ionic.
I run the command
sudo chown -R $USER /usr/local/ path you can update as given in error.

Instaling canvas-sketch in terminal fails [duplicate]

I run npm install lodash but it throws Error: EACCES: permission denied error. I know it is permission issue but as far as I know, sudo permission is not required for installing node module locally. If I run it with sudo, it gets installed inside ~/node_modules folder. drwxrwxr-x is the file permission of existing folder. I can't figure out what might have gone wrong.
Below is the error message.
npm ERR! tar.unpack untar error /home/rupesh/.npm/lodash/4.13.1/package.tgz
npm ERR! Linux 3.13.0-88-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "lodash"
npm ERR! node v4.3.1
npm ERR! npm v2.14.12
npm ERR! path /home/rupesh/node_modules/lodash
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir
npm ERR! Error: EACCES: permission denied, mkdir '/home/rupesh/node_modules/lodash'
npm ERR! at Error (native)
npm ERR! { [Error: EACCES: permission denied, mkdir '/home/rupesh/node_modules/lodash']
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/home/rupesh/node_modules/lodash',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/home/rupesh/node_modules/lodash',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:35:25',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:47:53',
npm ERR! 'FSReqWrap.oncomplete (fs.js:82:15)' ] }
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! /home/rupesh/Desktop/es6/npm-debug.log
This command fix the issue. It worked for me:
sudo npm install -g --unsafe-perm=true --allow-root
I have same issue with webpack server installation on globally, Use steps from this Url 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
I had problem on Linux. I wrote
chown -R myUserName /home/myusername/myfolder
in my project folder.
WARNING: this is NOT the right way to fix it; DO NOT RUN IT, if you aren't sure of what could be the consequences.
Try to give all permission to your project folder with below command
sudo chmod -R 777 /yourProjectDirectoryName
run with
sudo npm install lodash
Creating package.json using npm init solved my issue.
It doesn't have write permissions for others (r-x). Try with
chmod a+w <folder>
and repeat.
A related issue:
Wasted 3 hours spanning several days.
On a AWS EC2 machine, below worked:
sudo chown -R $(whoami) /home/ubuntu/.cache
sudo chown -R $(whoami) /home/ubuntu/.config
sudo chown -R $(whoami) /home/ubuntu/.local
sudo chown -R $(whoami) /home/ubuntu/.npm
sudo chown -R $(whoami) /home/ubuntu/.pm2
Hope that helps.
This solved my issue straight away - mac Mojave 10.14.6 - PhpStorm.
Unhandled rejection Error: EACCES: permission denied, mkdir
'/Users/myname/.npm/_cacache/index-v5/fb/5a'
sudo chown -R $USER:$GROUP ~/.npm
sudo chown -R $USER:$GROUP ~/.config
Original post:
https://stackoverflow.com/a/50639828
sudo chown -R $(whoami) /usr/local/bin
Makes current user owner of the /usr/local/bin folder. Permissions on this folder were the trouble.
LUBUNTU 19.10 / Same issue running: $ npm start
dump:
Error: EACCES: permission denied, open '/home/simon/xxx/pagebuilder/resources/scripts/registration/node_modules/.cache/#babel/register/.babel.7.4.0.development.json'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.writeFileSync (fs.js:1299:33)
at save (/home/simon/xxx/pagebuilder/resources/scripts/registration/node_modules/#babel/register/lib/cache.js:52:15)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
at Function.Module.runMain (module.js:696:11)
at Object. (/home/simon/xxxx/pagebuilder/resources/scripts/registration/node_modules/#babel/node/lib/_babel-node.js:234:23)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
Looks like my default user (administrator) didn't have rights on node-module directories.
This fixed it for me!
$ sudo chmod a+w node_modules -R ## from project root
From what i can see in your logs you posted:
npm ERR! code: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/home/rupesh/node_modules/lodash',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/home/rupesh/node_modules/lodash',
npm ERR! fstream_class: 'DirWriter',
directory /home/rupesh/node_modules/ doesn't have necessary permissions to create directory so run chown -r rupesh:rupesh /home/rupesh/node_modules/ this should solve it.
If you getting an error like below
Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/<PackageName>/vendor'
I suggest using the below command to install your global package
sudo npm install -g <PackageName> --unsafe-perm=true --allow-root
FWIW I had the same symptoms, but with a different package. Creating package.json and running npm init did NOT solve my issue.
On this system, apparently new folders in this location were being created with root permissions. During npm install, new folders are created. This caused npm install to fail partway, even with sudo.
The solution was to run npm install app in a different location with no root umask.
I solved this issue by changing the permission of my npm directory. I went to the npm global directory for me it was at
/home/<user-name>
I went to this directory by entering this command
cd /home/<user-name>
and then changed the permission of .npm folder by entering this command.
sudo chmod -R 777 ".npm"
It worked like a charm to me. But there is a security flaw with this i.e your global packages directory is accessible to all the levels.
Try using this:
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
Test installing package globally without using sudo, Hope it helps
After trying anything, I followed this video from NPM.
It solved for me smoothly!
https://www.youtube.com/embed/bxvybxYFq2o
Instructions as follow (on older Mac):
In the terminal, inside your user directory, create this folder mkdir .npm-global. This will be the new folder where all your global NPM installations will go, instead of the default .npm which has permission issues.
cd .npm-global/
Run npm config set prefix /Users/your_user/.npm-global
Set the Path for the system to know where to find the packages: Run vi ~/.profile. If you don't have this file, run touch ~/.profile first, then vi ~/.profile. Write export PATH=/Users/your_user/.npm-global/bin:$PATH at the very top. You could also open this file with a text editor (as it's a hidden file, use SHIFT + CMD + . to show it).
Back to the terminal, inside the /.npm-global folder, run source ~/.profile
You should now be ready to install your package globally.
However, the above seems not to be working on newer OS, like BigSure.
This should solve:
sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
First install without -g (global) on root. After try using -g (global) It worked for me.
Here's the solution for GNU/Linux (Debian) users (Replace USERNAME with your username):
sudo chown -R $USER:$(id -gn $USER) /home/USERNAME/.config
I tried a few of the answers from above on my Mac running Bigsur 11.4 and did not work. But what worked was the following instructions posted on npmjs docs website.
https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
I tried most of these suggestions but none of them worked. Then I ran npm clean-install and it solved my issues.
Remove dist folder and this solve my problem!!
On Windows it ended up being that the port was already in use by IIS.
Stopping IIS (Right-click, Exit), resolved the issue.
Just change the owner of the global node_modules directory to be your user:
sudo chown -R $USER:$GROUP /usr/local/lib/node_modules
node recommends executing following:
sudo chown -R $USER:$(id -gn $USER) /home/venkatesh/.config
If you execute
npm config
You will see something like this
│ npm update check failed │
│ Try running with sudo or get access │
│ to the local update config store via │
│ sudo chown -R $USER:$(id -gn $USER) /home/venkatesh/.config │
It worked for me.
Opening CMD(Windows WSl in this case) as Administrator worked out for me.I am using NodeJS Version 14.15.1
The permission denied error was persistent after all chown command. However, updating npm sudo npm install -g npm#8.3.2 and changing path to PATH="$PATH" worked for me.
Install nvm
curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh -o install_nvm.sh
Install a version of node v14 using
nvm install 14
Then use the node version
nvm use 14
Now you can remove / delete your node_modules folder and package-lock.json file and reinstall the packages using npm install and that's it you should be okay now

Missing write access in mac to /usr/local/lib/node_modules

I am trying to install angular cli but it's show me Missing write access to /usr/local/lib/node_modules
so, how can I fix it in my mac i try it may time but, not getting exact answer
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
Run these commands in a terminal window:
sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules
I had this issue, I could fixed it by calling sudo on every global install. I wasn't ok with that.
I followed the n package installation advices to be able to install npm packages without sudo.
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
Answer of #jeduard didn't work for me.
Best way to solve your npm install -g issues:
https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
This way, you will never need to use SUDO or change permissions.
Quoting:
Back up your computer.
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
Appending sudo before the npm install saved my day:
sudo npm install -g #angular/cli
Once you provide your system password, installation should finish smoothly:
Same problem. I reinstalled the latest version of node via https://nodejs.org/en/download/ instead of using terminal. This resolved the issue.
I had a similar issue when trying to install React -- issues with permissions being denied.
This is where SUDO can come in handy, it allows a permitted user to execute a command as the superuser or another user. Think of like "Run as Administrator" on Windows. If you type in the following command, it should allow you to access all of the needed folders with the proper elevated access.
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
This is also denoted in Dimitri's answer:
https://stackoverflow.com/a/60791736/3803726
For macOS try to run -
sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/local/lib/node_modules
sudo chown -R $USER /usr/local/bin/
If this still doesn't work you might need to add sudo to the npm install command.
If you're really blocked on installing NPM modules globally (e.g. on some corporate laptop), you could just install them all in a single local folder somewhere and then add the path to node_modules/.bin/ to your PATH environment.
I had the same issue.
type this cmd in terminal
sudo nautilus
after that, it opens another file manager that has root access
after that type
sudo apt install nodejs
sudo apt install npm
sudo npm install -g npm#latest
sudo npm install -g #angular/cli
ng --version

npm install permission denied (macOS)

To install a Bootstrap theme I want to run npm install. However I always receive a permission denied error.
I already tried nvm and then switched with nvm use 10.9.0 to run npm install.
I also tried sudo chown -R $(whoami) ~/.npmand sudo chown -R $USER /usr/local/lib/node_modules. Neither solved it and now I am bit out of ideas how I can continue. I use macOS High Sierra.
Marcs-MBP-3:masterclass Marc$ npm install
npm WARN deprecated gulp-uglifyjs#0.6.2: Since gulp-sourcemaps now works, use gulp-uglify instead
npm WARN deprecated babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm WARN deprecated browserslist#1.7.7: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN checkPermissions Missing write access to /Users/Marc/Desktop/Dev/masterclass/node_modules
npm ERR! path /Users/Marc/Desktop/Dev/masterclass/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/Users/Marc/Desktop/Dev/masterclass/node_modules'
npm ERR! { [Error: EACCES: permission denied, access '/Users/Marc/Desktop/Dev/masterclass/node_modules']
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, access \'/Users/Marc/Desktop/Dev/masterclass/node_modules\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/Users/Marc/Desktop/Dev/masterclass/node_modules' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/Marc/.npm/_logs/2018-08-22T12_46_51_786Z-debug.log
For Mac;
Run this on the Terminal >
sudo chown -R $USER /usr/local/lib/node_modules
Check permissions of your project root with ls -l /Users/Marc/Desktop/Dev/masterclass/. If the owner is not $USER, delete your node_modules directory, try changing the owner of that directory instead and run npm install again.
cd /Users/Marc/Desktop/Dev
rm -rf ./masterclass/node_mdoules/
chown -R $USER ./masterclass/
cd masterclass
npm install
I tried everything in this thread with no luck on Big Sur, but then I tried this:
sudo npm install -g yarn
And it worked!
KIndly run the below commands:
To check the location of the package:
npm config get prefix
Then run this :
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Enter the password and run installation commands
It worked for me.
For me it was,
npm cache clean --force
rm -rf node_modules
npm install
I tried deleting manually but didn't help
I did this for nodemon and it work
sudo chown -R $USER /usr/local/lib/node_modules
then install the packages that you need
I was having a similar issue, but the accepted answer did not work for me, so I will post my solution in case anyone else comes along needing it.
I was running npm install in a project cloned from GitHub and during the clone, for whatever reason the write permission was not actually set on the project directory. To check if this is your problem, pull up Terminal and enter the following:
cd path/to/project/parent/directory
ls -l
If the directory has user write access, the output will include a w in the first group of permissions:
drwxr-xr-x 15 user staff 480 Sep 10 12:21 project-name
This assumes that you're trying to access a project in the home directory structure of the current user. To make sure that the current user owns the project directory, follow the instructions in the accepted answer.
I entered the following:
cd /Users/Marc/Desktop/Dev
rm -rf ./masterclass/node_mdoules/
chown -R $USER ./masterclass/
cd masterclass
npm install
once this was completed the results indicated warnings and one notice instead of previous result of no permission and error.
I then entered the following:
% sudo npm install --global firebase-tools
my result was success upon completion of the last terminal entry.
I have same problem because i install it from pkg, and i solve this problem use below step:
1. sudo rm -rf /usr/local/lib/node_modules/npm/
2. brew doctor
3. brew cleanup --prune-prefix ( or sudo rm -f /usr/local/include/node)
4. brew install node
i use this command :
sudo npm install -g #angular/cli
Gave password and worked for mw. Took 10 secs to install angular
NPM_CONFIG_PREFIX=~/.npm-global
Copy this line into ur terminal, then hit enter. Then install the necessary packages you need WITHOUT the term "sudo" in front of npm.
i.e.,
npm install -g jshint
the only thing that work on me sudo npm i -g clasp --unsafe-perm
Just do :
sudo npm install -g #sanity/cli && sanity init
it will ask sudo password and you are good to go
That is because you dont have the "node modules". You can install with this code:
npm install -g node-modules
then, create your react app with npm init react-app my-app
For Macs running Big Sur or Monterey:
sudo chown -R $USER /usr/local/bin
Run on macOS Terminal:
sudo chown -R $USER /usr/local/bin
It will ask for the password then you're good to go!
Hope this helps.
Ok my problem was that I thought I was installing on the path:
/Users/mauro/Documents/dev/react
Where my project was setup, but instead I was doing it on:
Users/mauro/Documents/dev/
One path higher and that is why it did not perform the installation in my case.
I simply did: cd react and voila I was able to install without problem

Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

What might be causing the error Error: EACCES: permission denied, access '/usr/local/lib/node_modules'?
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/macbookmd101/.npm/_logs/2018-02-21T16_26_08_421Z-debug.log
Change your file permissions... Like this
First check who owns the directory
ls -la /usr/local/lib/node_modules
it is denying access because the node_module folder is owned by root
drwxr-xr-x 3 root wheel 102 Jun 24 23:24 node_modules
so this needs to be changed by changing root to your user but first run command below to check your current user How do I get the name of the active user via the command line in OS X?
id -un OR whoami
Then change owner
sudo chown -R [owner]:[owner] /usr/local/lib/node_modules
OR
sudo chown -R ownerName: /usr/local/lib/node_modules
OR
sudo chown -R $USER /usr/local/lib/node_modules
To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use a hidden directory in your home directory.
Back up your computer.
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create the ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
To test your new configuration, install a package globally without using sudo.
Source: the NPM website.
I tried the solution of the answer given by #okanda but it didn't work for me.
However it worked perfectly when I did it for several folders like mentioned in this thread: https://github.com/angular/angular-cli/issues/9676#issuecomment-464857493
sudo chown -R $USER /usr/local/lib/node_modules/
sudo chown -R $USER /usr/local/bin/
sudo chown -R $USER /usr/local/share/
All you need to do is to add USER to the owner of /local/lib
sudo chown -R $USER /usr/local/lib
EDIT :
To target precisely and only the node_modules folder, try using this command before using the previous one :
sudo chown -R $USER /usr/local/lib/node_modules
try appending sudo before whatever command you are trying.
like this : sudo npm install
Using sudo with a command in Linux/UNIX generally elevates your permissions to superuser levels. In Windows, the superuser account is usually called 'Administrator.' In Linux/Unix the superuser account is generally named 'root'.
The root user has permission to access, modify or delete almost any file on your computer. Normal user accounts can access, modify or delete many fewer files. The restrictions on a normal account protect your computer from unauthorized or harmful programs or users. Some processes require you to perform actions on files or folders you don't normally have permissions to access. Installing a program that everyone can access is one of these actions.
In your case, running the installation command with sudo gives you the permissions of the superuser, and allows you to modify files that your normal user doesn't have permission to modify.
You can install npm through Node version manager or a Node installer. In the docs it states:
We do not recommend using a Node installer, since the Node
installation process installs npm in a directory with local
permissions and can cause permissions errors when you run npm packages
globally.
NPM actually recommends using a Node Version Manager to avoid these errors.
Since you have the permission error, you probably installed npm through a Node installer and now you need to reinstalled it with a nvm (node version manager).
Luckily, this is very simple. You do not even need to remove your current version of npm or Node.js.
All you need to do is
Install nvm.
For OSX or Linux Node use:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<VERSION>/install.sh | bash
where <VERSION> should be replaced with the latest version
This creates a nvm folder in your home directory.
Then
Install npm and node.js through nvm. To do so, just call
nvm install node
("node" is an alias for the latest version)
Now you can install your package globally without using sudo or changing the owner of node_modules in usr folder.
If you are facing this issue on you Mac. Follow these steps
First checking who is owner of this file by using below command
ls -la /usr/local/lib/node_modules
you will find some file like below one of them is below
drwxr-xr-x 3 root wheel 768 May 29 02:21 node_modules
have you notice that above file is own by root, for make changes inside for you need to change owner ship of path.
you can use check who is current user by this command
id -un (in my case user is yamsol)
and then you can change by calling this command (just replace your user name with ownerName)
sudo chown -R ownerName: /usr/local/lib/node_modules
in my case as you know user is "yamsol" i will call this command in this way
sudo chown -R yamsol: /usr/local/lib/node_modules
thats it.
It looks like you're running into permission issues. If you are installing npm-packages then it might possible that you are getting an EACCES error when trying to install a package globally. This means you do not have permission to write to the directories npm uses to store global packages and commands.
Try running commands: sudo chmod u+x -R 775 ~/.npm and sudo chown $USER -R ~/.npm or you can just run any npm command with sudo, that should get resolve your issue.
If you are installing an npm-package locally, then you should be in your local project directory and can try running sudo npm install <pkg-name> command to install required package. the purpose of using sudo is that it will change your owner permissions so you can make your current user authorized to run npm commands.
I'd recommend you to take a look at https://docs.npmjs.com/getting-started/fixing-npm-permissions
While 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, it will be:
sudo npm install -g typescript
Be careful with all responses that change the owner of all directories under /usr/local
Basically, don't mess the Linux system!!!
Using sudo for your local stuff is a really bad recommendation as well.
The original link from www.competa.com is broken, so this is the original approach from there:
npm config set prefix ~/.npm
# open your .bashrc (Linux) or .bash_profile (Mac) file for editing:
nano ~/.bashrc # for Linux
# or...
nano ~/.bash_profile # for Mac if you haven't created a .bashrc file
# add these lines:
export PATH="$PATH:$HOME/npm/bin"
export NODE_PATH="$NODE_PATH:$HOME/npm/lib/node_modules"
# save the file and then enter this command or logout and login to make the changes take effect:
. ~/.bashrc
# or...
. ~/.bash_profile
Option B: Use a version manager like NVM
If it is still not working after giving permissions try running these commands:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
and finally test with this command
npm install -g jshint
This does not work for Windows.
I was trying to install react expo and apart from sudo I had to add --unsafe-perm
like this. This resolves my Issue
sudo npm install -g expo-cli --unsafe-perm
Review this carefully:
https://ionicframework.com/docs/developing/tips#resolving-permission-errors
sudo chown -R $(whoami) /usr/local/{lib/node_modules,bin,share}
sudo chown -R $(whoami) ~/.npm ~/.npmrc
For nvm users
sudo chown -R $USER /home/bereket/.nvm/versions/node/v8.9.1/lib/node_modules
and
sudo chown -R $USER /usr/local/lib/node_modules/
replace v8.9.1 with your node version you are using.
Encountered in CentOS 8 stream
Solution
(1/4) Creating node_modules folder
$ sudo mkdir /usr/local/lib/node_modules
(2/4) Own with Current User
$ sudo chown -R $USER /usr/local/lib/node_modules/
(3/4) Own the bin folder
$ sudo chown -R $USER /usr/local/bin/
(4/4) Own the share folder
$ sudo chown -R $USER /usr/local/share/
Seems like you tried to install a npm package globally rather than locally, as the man npm install describes:
The -g or --global argument will cause npm to install the package globally rather than locally.
Generally, when you are setting up a npm project (among many others that you could have), it's not a good idea to install packages on Node.js global modules (/usr/local/lib/node_modules), as your the debug log suggested.
Instead of using -g, use --save, which will automatically save the package as a dependency for your package.json file:
Like this:
$ npm install express-generator --save
$ cat package.json
{
"name": "first_app_generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ivanleoncz",
"license": "MIT",
"dependencies": {
"express-generator": "^4.16.0"
}
}
But as the other answers mentioned, if you're going to use -g, you have to use sudo (if your user has sudo privileges: see /etc/sudoers) when performing npm install express-generator -g, but indeed, it's not a good idea, possibly causing permission problems.
NOTICE
There are instructions for installing express-generator with -g option, in order to have the script express-cli.js available on the system path, but you can use the locally installed script as well, located at the node_modules if your npm project:
$ ./node_modules/express-generator/bin/express-cli.js --view=pug myapp
If a message like /usr/bin/env: ‘node’: No such file or directory shows up, install nodejs-legacy (Debian/Ubuntu)
IMHO, using -g (also using sudo) is like hic sunt dracones, if you are unsure of the consequences.
For further information:
Creating NPM Package (npm init)
Express.js Generator
I was able to fix the issue using the following in mac.
sudo npm install -g #aws-amplify/cli --unsafe-perm=true
This occurred as a result of npm not being able to access your global node_modules directory locally, running
sudo chown -R Name: /usr/local/lib/node_modules
e.g.
sudo chown -R developerayo: /usr/local/lib/node_modules
fixes the issue, now you can run the command you ran again.
For those of you still unable to fix the problem after using the above mentioned solutions. Try this
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
That should do the trick, cheers!
It is work 100%
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Use this command to install npm as the sudo user:
sudo npm install -g create-react-app
instead of
npm install -g create-react-a pp.
Below command worked for me:
sudo npm install -g appium --unsafe-perm=true --allow-root
On my mac similar issue is coming while installing Ionic.
I run the command
sudo chown -R $USER /usr/local/ path you can update as given in error.
Simply you can change the owner or just use sudo before you command like this
sudo chown -R [owner]:[owner] /usr/local/lib/node_modules (change owner)
or
sudo npm install -g json-server
that's it.
For linux / ubuntu if the command
npm install -g <package_name>
npm WARN deprecated superagent#4.1.0: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released.
npm ERR! path ../lib/node_modules/<package_name>/bin/..
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall symlink
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules
/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'
npm ERR! { [Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>']
npm ERR! cause:
npm ERR! { Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/<package_name>/bin/..',
npm ERR! dest: '/usr/local/bin/ionic' },
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, symlink \'../lib/node_modules/ionic/bin/ionic\' -> \'/usr/local/bin/ionic\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/<package-name>/bin/<package-name>',
npm ERR! dest: '/usr/local/bin/<package-name>' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
npm ERR! A complete log of this run can be found in:
npm ERR! /home/User/.npm/_logs/2019-07-29T01_20_10_566Z-debug.log
Fix : Install with root permissions
sudo npm install <package_name> -g
You need the permission of superuser levels to install React. In Linux/Unix the superuser account is generally named 'root'.
To get superuser privilege just run the following command on your terminal:
sudo -i
and then simply run the command to install React:
npm install -g create-react-app
However, the reactjs team encourages us to use the following command instead of installing a global package.
npx create-react-app app_name
sudo chown -R $USER /usr/local/lib/node_modules
Similar to POsha's answer but this is what worked for me on ubuntu 19
sudo npm i -g ngrok --unsafe-perm=true --allow-root
From this link
https://github.com/inconshreveable/ngrok/issues/429
Just add "sudo" before npm command. Thats it.
brew uninstall node
brew install node
sudo npm install -g "your_package" --unsafe-perm=true --allow-root
Hopes this helps someone

Resources