n: Permission denied before running with sudo

Issue Template:

Describe issue including what OS you are using

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.

Describe what version of N you have

2.1.4

Describe how you installed N

npm install -g n

Steps to reproduce issue 1.2.3

  1. install n for the first time
  2. run n
  3. get issue

Describe the results you received

permission denied for operations along with list of possible commands

Describe the results you expected

list of possible commands

What version of npm you are using

4.1.2

Additional information you deem important (e.g. issue happens only occasionally):

  • Output of which n: /usr/local/bin/n
  • Output of node -v: v7.5.0

(paste your output here)

tiago 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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 17

Most upvoted comments

I did these

  1. create n directory in /user/local
  2. sudo chown -R $(whoami) /usr/local/n

now it’s work.

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

I also had to run sudo chown -R $(whoami) /usr/local/lib/node_modules/

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.

Is there any reason these steps are not in the README? I’d be happy to add them and submit a PR.

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.

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

Reference.

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

There are lots of upvotes for work-arounds and README despite being a closed issue. Reopening to increase visibility, until fixed.

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? 😃

Removed need for sudo for read-only operations. Added chown tips to README. Fixed in v3.0.2.