After installing n on macOS 10.12.3 and node 7.5.0 previously installed, I ran simply n and got "permission denied" issues along stating that I hadn't any version installed along the list of possible commands.
Then I ran one command with sudo and the "permission denied" issue disappeared.
2.1.4
npm install -g n
permission denied for operations along with list of possible commands
list of possible commands
4.1.2
which n: /usr/local/bin/nnode -v: v7.5.0tiago at Tiagos-MacBook-Pro in ~
$ n
mkdir: /usr/local/n/versions/node: Permission denied
mkdir: /usr/local/n/versions/io: Permission denied
find: /usr/local/n/versions: No such file or directory
Error: no installed version
$ n -V
mkdir: /usr/local/n/versions/node: Permission denied
mkdir: /usr/local/n/versions/io: Permission denied
2.1.3
tiago at Tiagos-MacBook-Pro in ~
$ sudo n -V
Password:
2.1.3
tiago at Tiagos-MacBook-Pro in ~
$ node
> .exit
tiago at Tiagos-MacBook-Pro in ~
$ sudo n
Error: no installed version
You don't own the folder /usr/local/ so executing anything will have to come from sudo. If you chown the directory, using sudo will not be necessary for n
If you ever had brew installed they do that for you.
So for your issue, sudo is the way you need to go to use n.
Note that as of 2017 homebrew no longer changes the ownership of this folder. You will have to do it yourself.
I did these
1) create n directory in /user/local
2) sudo chown -R $(whoami) /usr/local/n
now it's work.
Is there any reason these steps are not in the README? I'd be happy to add them and submit a PR.
I also had to run sudo chown -R $(whoami) /usr/local/lib/node_modules/
@phongsakornp special thanks! works for me with the code below!
sudo chown -R $(whoami) /usr/local
I also had to run
sudo chown -R $(whoami) /usr/local/bin/npm
sudo chown -R $(whoami) /usr/local/bin/node
sudo chown -R $(whoami) /usr/local/bin/npx
Since, High Sierra doesn't allow chown on /usr, the following solved this issue for me (OS 10.13.6):
sudo mkdir /usr/local/n
sudo chown -R $(whoami) $_
# Commands like 'n lts' can now be run without sudo
The original posters problem is that n reports a permission error the first time it is run. The underlying reason this happens is because n is checking its cache folders exist every time it is run.
Assuming you want to do node installs to /usr/local/ (which is the default for n):
1) If you will use sudo when you run installs, simplest is just run sudo n once and it will create the necessary folders as a side-effect. After doing that, you can run read-only commands like n and n --lts without sudo.
2) If you do not wish to use sudo for installs and are in control of the computer, you can take ownership of the cache folder and install locations. (This ownership change also allows npm installs of global packages without using sudo.)
# make cache folder (if missing) and take ownership
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
# take ownership of node install destination folders
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
# no longer need sudo as you own the folders
n lts
@phongsakornp very very thanks!! works for me!!
This should really be in the ReadMe, I can't use n without changing ownership and I had to find this issue ti get it working.
please add this to the readme
There are lots of upvotes for work-arounds and README despite being a closed issue. Reopening to increase visibility, until fixed.
Removed need for sudo for read-only operations. Added chown tips to README.
Fixed in v3.0.2.
seems to me that changing the permissions for /usr/local is a terrible idea as it will break other users on the system
@leifett Changing the ownership is a convenience to avoid needing sudo for the single user on the computer (most common case), or a single administrator running n and brew et al. I don't see it "breaking" other users even on a multi-user system though. Have you got an example, or just uncomfortable about changing ownership? (In which case, use sudo or install to your home directory!)
I would like to point out: it's not recommended to chown /usr/local, only the directory needed inside...
I mean that whole directory was classified root only, if you reduce that security level, some apps could change/create files in that directory which are eventually executed by root later...
=> obvious isn't it? or do you think the Apple team sets /usr/local back to root on every release just to annoy you? :)
Most helpful comment
I did these
1) create
ndirectory in /user/local2)
sudo chown -R $(whoami) /usr/local/nnow it's work.