Permission denied when installing npm module

26,060

Solution 1

You need to reclaim ownership of the .npm directory.

sudo chown -R $(whoami) ~/.npm

and need the write permission in node_modules directory:

sudo chown -R $(whoami) /usr/lib/node_modules

Solution 2

A rule of thumb as I have learned is to never run npm install with sudo. That creates items with root being the owner as opposed to your username.

It's best to change ownership to the directory to your username. $(whoami)

Share:
26,060

Related videos on Youtube

laurent
Author by

laurent

Updated on June 09, 2021

Comments

  • laurent
    laurent almost 3 years

    I'm getting a strange permission error when I try to install an npm module. I'm starting it with sudo so I'm sure I do have access, but for some reason it keeps complaining with an error:

    stack Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/joplin/node_modules/sqlite3/build'

    I've tried restarting my computer, and creating a directory /usr/lib/node_modules/joplin with chmod 777, but it still doesn't work.

    $ sudo npm install -g joplin
    /usr/bin/joplin -> /usr/lib/node_modules/joplin/main.js
    
    > [email protected] install /usr/lib/node_modules/joplin/node_modules/sqlite3
    > node-pre-gyp install --fallback-to-build
    
    node-pre-gyp ERR! Tried to download(undefined): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v3.1.9/node-v48-linux-x64.tar.gz
    node-pre-gyp ERR! Pre-built binaries not found for [email protected] and [email protected] (node-v48 ABI) (falling back to source compile with node-gyp)
    gyp ERR! configure error
    gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/joplin/node_modules/sqlite3/build'
    gyp ERR! stack     at Error (native)
    gyp ERR! System Linux 4.4.0-43-Microsoft
    gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node" "--module_name=node_sqlite3" "--module_path=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64"
    gyp ERR! cwd /usr/lib/node_modules/joplin/node_modules/sqlite3
    gyp ERR! node -v v6.11.2
    gyp ERR! node-gyp -v v3.6.2
    gyp ERR! not ok
    node-pre-gyp ERR! build error
    node-pre-gyp ERR! stack Error: Failed to execute '/usr/bin/nodejs /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node --module_name=node_sqlite3 --module_path=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64' (1)
    node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/joplin/node_modules/sqlite3/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
    node-pre-gyp ERR! stack     at emitTwo (events.js:106:13)
    node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
    node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:891:16)
    node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
    node-pre-gyp ERR! System Linux 4.4.0-43-Microsoft
    node-pre-gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/joplin/node_modules/sqlite3/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
    node-pre-gyp ERR! cwd /usr/lib/node_modules/joplin/node_modules/sqlite3
    node-pre-gyp ERR! node -v v6.11.2
    node-pre-gyp ERR! node-pre-gyp -v v0.6.36
    node-pre-gyp ERR! not ok
    

    Edit

    I've also tried sudo npm i sqlite3 -g --build-from-source but getting the same error Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/sqlite3/build' so it seems to be something to do with sqlite3.

  • lxknvlk
    lxknvlk almost 6 years
    what is $(whoami) ?
  • Abhishek Singh
    Abhishek Singh almost 6 years
    whoami refers to the linux command which displays the current logged in user and $(whoami) gives you the output of the command.
  • geeky_monster
    geeky_monster almost 6 years
    wow. this worked. had tried my head on lots of things till I found this and it worked!
  • AnchovyLegend
    AnchovyLegend over 5 years
    The who am I command answers philosophical questions about life.
  • Kokodoko
    Kokodoko about 5 years
    Sadly, this does not fix the problem for me in OS X Mojave
  • Abhishek Singh
    Abhishek Singh about 5 years
    The global npm directory might be different in your case. You might as well try running the command on /usr/local/lib/node_modules
  • Katia Savina
    Katia Savina about 5 years
    For some reason, after these commands executing sudo npm i failed with the same error, but without sudo - it works!