How to fix NPM permission issues

I ran into NPM errors at around about Lesson 19, specifically when attempting to install gulp-sass. This forum post is information on how I solved this problem as a note to self, but also as it might be useful to others. Skip to #3 below for (what I think is) the proper solve.

1. General permission errors when trying to use npm install

I solved this by prepending each package with sudo. E.g. sudo npm install @11ty/eleventy. It’s a stopgap solution. I would recommend not using it. Using sudo can be destructive, and this stopgap solution stopped working for me entirely in Lesson 19. See #3 below for a better solution.

2. Lesson 19: Trying to run npm install gulp-clean-css gulp-sass sass returns an error

I solved this by installing each package individually. I.e. npm install gulp-clean-css, npm sass, and so on. @colabottles had the same problem and solution, detailed here.

3. Lesson 19: npm install gulp-sass returns a permissions error

I solved this by uninstalling node and npm, then reinstalling via nvm, and then (most importantly) giving myself write access to the folder. It was tricky and time consuming, hence this forum post. Full instructions below.

The actual error message

Here’s the error I got in the terminal after running npm install gulp-sass. I know it’s a problem with my computer and not the package as I tried other npm packages that I know to work.

npm install gulp-sass
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN checkPermissions Missing write access to /Users/danny/Projects/eleventy-from-scratch/node_modules
npm WARN eleventy-from-scratch@1.0.0 No description
npm WARN eleventy-from-scratch@1.0.0 No repository field.

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /Users/danny/Projects/eleventy-from-scratch/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/Users/danny/Projects/eleventy-from-scratch/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/Users/danny/Projects/eleventy-from-scratch/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/Users/danny/Projects/eleventy-from-scratch/node_modules'
npm ERR! }
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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/danny/.npm/_logs/2020-11-22T00_31_07_499Z-debug.log

How I solved it, in detail

  1. I completely removed Node.js and npm from my computer following these instructions.
  2. I installed nvm following these instructions, but then had to do some extra to get it working (see Installing nvm below).
  3. I updated ‘write access’ permissions to the node_modules folder following these instructions, but then had to do some extra to get it working (see Setting write access below). This step might be all you need to do, depending on your situation.

Installing nvm

Adapted from the aforementioned instructions.

Run the following in terminal:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash

Because (at least in my case), the nvm installation failed to put the snippet in my .zshrc file, I had to do it manually:

open ~/.zshrc

Then, in that text file, type:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Save that file.

The above is assuming you use .zshrc, not .bash. You should also check the above commands against the [official readme file](Link B).

Close and reopen terminal. Then type the following command to see if nvm properly installed:

command -v nvm

Then, as these Eleventy tutorials suggest, install the LTS version of node:

nvm install --lts

Close terminal.

Setting write access

Adapted from the aforementioned instructions.

Enter the eleventy-from-scratch directory, then adapt the following command to match your computer’s folder structure:

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

For me, that meant adapting the above command to the following:

sudo chown -R $USER /Users/danny/Projects/eleventy-from-scratch/node_modules