npm install puppeteer showing permission denied errors

14,004

Solution 1

EDIT 20th April 2019:

The easy solution suggested by lauraalvarezz1 is,

sudo npm install -g puppeteer --unsafe-perm=true

This is okay as long as you trust puppeteer and want it to install puppeteer globally.

However beware of using --unsafe-perm=true for permission related problems. Reasons are:

  • Running unsafe-perm=true with sudo, as a non-root user, will give the script root access. This might be okay only if you trust the script and do not concern about security that much.
  • You might need to use --no-sandbox in every script you run, because the chrome installed with this command might not run without this parameter. See this github issue.

You have installed npm with sudo. Thus anything you install globally will require sudo.

To install anything on var/www/html folder, either you have to own that folder,

sudo chown -R $USER /var/www/html

Or you can use nvm to manage npm. Technically it will use your home directory and your current user.

After installing nvm, you can install puppeteer globally with it,

npm i -g puppeteer

or you have to use sudo

sudo npm install --save puppeteer

However chromium will not be downloaded due to permission error, that's why you have to use ---unsafe-perm=true as stated before.

Security Related Resources:

  • Resolve this without sudo, you can use this answer.
  • Learn more about best practices dealing with /var/www folder, refer to this answer.

Best of luck!

Solution 2

Run this on your terminal:

sudo npm install -g puppeteer --unsafe-perm=true

Solution 3

Before you begin, make sure you have the most recent version of Node.js.

The Puppeteer Documentation states:

Note: Puppeteer requires at least Node v6.4.0, but the examples below use async/await which is only supported in Node v7.6.0 or greater.

You can check which version of Node.js you have using the following command:

node -v
# OR nodejs -v

If your version of Node.js is less then v7.6.0, you can completely uninstall your current version of Node.js.

Then, you can use complete the reinstallation using a PPA:

sudo apt update
curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs
sudo apt install build-essential

You can check the new versions of Node.js and NPM installed on your system:

node -v
npm -v

Finally, you can install Puppeteer:

sudo npm install puppeteer --unsafe-perm=true --allow-root

Now you can run Puppeteer scripts using the node command:

node puppeteer-script.js
Share:
14,004
d-_-b
Author by

d-_-b

(your about me is currently blank)

Updated on July 05, 2022

Comments

  • d-_-b
    d-_-b almost 2 years

    I'm unable to install puppeteer as a project dependency, and I've tried re-installing node. Anyone have an idea on how to fix this? Running Ubuntu 17.10 x64

    sudo apt-get purge nodejs;
    curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -;
    apt-get install -y nodejs;
    sudo npm install -g n;
    sudo n stable;
    

    Node versions:

    $ node -v
    v9.4.0
    $ npm -v
    5.6.0
    

    I try to install:

    root@server:/var/www/html# npm install --save puppeteer
    

    Error message:

    > [email protected] install /var/www/html/node_modules/puppeteer
    > node install.js
    
    ERROR: Failed to download Chromium r536395! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.
    { Error: EACCES: permission denied, mkdir '/var/www/html/node_modules/puppeteer/.local-chromium'
      errno: -13,
      code: 'EACCES',
      syscall: 'mkdir',
      path: '/var/www/html/node_modules/puppeteer/.local-chromium' }
    npm WARN [email protected] No description
    
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected] install: `node install.js`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected] install script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /root/.npm/_logs/2018-02-19T15_55_52_669Z-debug.log
    

    I don't see any ways to fix this in the referenced issue: https://github.com/GoogleChrome/puppeteer/issues/375

  • Sam Denty
    Sam Denty over 6 years
    Recursively changing the owner for every file on the system? Surely you meant ./ at least
  • Md. Abu Taher
    Md. Abu Taher over 6 years
    Sorry, fixed :D
  • Md. Abu Taher
    Md. Abu Taher about 5 years
    unsafe-perm=true has some (security) drawbacks and that's why I did not update my answer last year, even if it's a quick solution. The accepted and most upvoted answer, both are correct. All links in my answer are from SO for more resource.
  • Admin
    Admin about 3 years
    "some (security) drawbacks" - it may be not a problem if people run the code that did not get sufficient scrutiny in a VM, as they should.
  • Artyom Ganev
    Artyom Ganev over 2 years
    sudo + global is a very bad hack
  • Artyom Ganev
    Artyom Ganev over 2 years
    sudo + global is unsafe
  • Md. Abu Taher
    Md. Abu Taher over 2 years
    Yes this answer explains this. ^