Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

document using npm with a version manager to avoid permissions errors #240

Closed
kentonv opened this issue Jun 11, 2019 · 28 comments
Closed

document using npm with a version manager to avoid permissions errors #240

kentonv opened this issue Jun 11, 2019 · 28 comments
Labels
docs regression Something is broken, but works in previous releases

Comments

@kentonv
Copy link
Member

kentonv commented Jun 11, 2019

https://workers.cloudflare.com says to use npm install -g @cloudflare/wrangler to install, but on Linux that doesn't work because only root can perform global installs. The usual thing to do is put sudo in front of it, but that doesn't work either, producing the log shown below.

It appears the problem is that Wrangler's post-install script is trying to put stuff in the user's home directory. Fundamentally, install -g shouldn't be doing any per-user stuff; the whole point is to install for all users. The permission error seems to occur because when npm install is run as root, it actually runs the post-install script as nobody. This seems... bizarre, since I'm not sure what a post-install script could usefully do when running as nobody, but there you go. In any case, homedir() still returns /root (despite the active user being nobody), and of course nobody cannot access /root, hence the permissions error.

But even if the post-install script did run as root, it seems that Wrangler would end up installing files into the root user's home directory, where they would be inaccessible to any other user and thus not very useful.

Non-global installs appear work, with the caveat that the wrangler binary gets placed in $HOME/.wrangler with no versioning, so separate projects depending on different versions of Wrangler will interfere with each other. IMO it would be better to place the binary somewhere under the module's installation location in node_modules.

kenton@zero:~$ sudo npm install -g @cloudflare/wrangler
/usr/bin/wrangler -> /usr/lib/node_modules/@cloudflare/wrangler/run-wrangler.js

> @cloudflare/wrangler@1.0.2 postinstall /usr/lib/node_modules/@cloudflare/wrangler
> node install-wrangler.js

fs.js:115
    throw err;
    ^

Error: EACCES: permission denied, mkdir '/root/.wrangler'
    at mkdirSync (fs.js:753:3)
    at Object.<anonymous> (/usr/lib/node_modules/@cloudflare/wrangler/install-wrangler.js:60:3)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @cloudflare/wrangler@1.0.2 postinstall: `node install-wrangler.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @cloudflare/wrangler@1.0.2 postinstall 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/2019-06-11T12_29_59_623Z-debug.log
@ashleygwilliams
Copy link
Contributor

how do you have node installed?

@kentonv
Copy link
Member Author

kentonv commented Jun 12, 2019

I have node installed via the Debian Sid package, and I have npm installed via npm install -g npm@latest (I don't know how I originally installed npm, probably via Debian package as well).

kenton@zero:~$ npm --version
6.9.0
kenton@zero:~$ node --version
v10.15.2

@xtuc
Copy link
Member

xtuc commented Jun 12, 2019

We should stop trying to install Wrangler into the user's $HOME, or locally to the project, and require sudo.

In the case that the Nodejs installation allow global write access by the user, they will get a permission error (because we will try to install in /usr/bin). Users are already used to add sudo and retry.

@kentonv
Copy link
Member Author

kentonv commented Jun 12, 2019

FWIW I personally very much prefer to install tools locally to a project even when the tool authors think I should install it globally. I don't like polluting my system with npm packages that I only need for one particular project, and I like to pin tools to known-good versions in my projects.

@kentonv
Copy link
Member Author

kentonv commented Jun 12, 2019

In the case that the Nodejs installation allow global write access by the user, they will get a permission error (because we will try to install in /usr/bin). Users are already used to add sudo and retry.

Note that I did use sudo and still got a permissions error, because the post-install script appears to run as nobody rather than root.

@xtuc
Copy link
Member

xtuc commented Jun 12, 2019

FWIW I personally very much prefer to install tools locally to a project even when the tool authors think I should install it globally. I don't like polluting my system with npm packages that I only need for one particular project, and I like to pin tools to known-good versions in my projects.

Doing the same and absoluty agree. I understand that for better UX we should install it globally like every npm package does. However, we don't prevent users to download a GitHub release or use cargo.

Note that I did use sudo and still got a permissions error, because the post-install script appears to run as nobody rather than root.

I'm not familiar enought with npm's postinstall, I need to do some research.
My understand is that only the bin in the package.json is being move into system bins, but the script isn't ran as root (hopefully).

@younggrandpa
Copy link

Note that I did use sudo and still got a permissions error, because the post-install script appears to run as nobody rather than root.

@kentonv The same is happening on Mac. I created a temporary project folder and installed wrangler. I didn't use sudo and node install-wrangler.js went through without any errors. a .wrangler folder in the user root directory was created.

npm init
npm install @cloudflare/wrangler --save

The instructions in Cloudflare say to confirm that Wrangler has successfully installed on your machine, run wrangler --help on the command-line. However you will not be able to do this just as yet. Install again wrangler but this time use sudo and --global.

sudo npm install @cloudflare/wrangler --global

You should see the files installed in @cloudflare/wrangler@1.0.2 postinstall /usr/local/lib/node_modules/@cloudflare/wrangler. Ignore the error spewed by the post-install script. Then try running this command again: wrangler --help on any directory and you should see:

Screen Shot 2019-06-18 at 4 50 48 pm

@ashleygwilliams
Copy link
Contributor

running npm install with sudo is not recommended (source: used to work at npm, owned the docs around this)

the way to solve this (as recommended by npm) is to install node using a version manager. this will create your global node_modules directory in a user directory which prevents the need to run as root. i would recommend using nvm, though there are several options available on many platforms.

OS pkg managers unfortunately do a bad job of this installation. this has been a longtime struggle in the node world.

you can read more about this at https://www.npmjs.com/get-npm

if this doesn't work- please follow up!

(i understand this is a pain and i know that the node community has long struggled with this, long term, beyond this bug, using a node version manager is by far the better experience, so it is worth it, regardless of your feeling on this issue.)

@ashleygwilliams
Copy link
Contributor

we can improve the docs here. i know that our readme currently recommends brew which is definitely not the ideal solution (this was due to a mistaken merge of a community member's PR). i will update the docs to reflect this.

@kentonv
Copy link
Member Author

kentonv commented Jun 22, 2019

I see. I think the best practices around node and npm have changed many times over the years, and people like me who don't use them every day have trouble keeping up. I was aware of nvm but didn't realize that it's now assumed that everyone is using it...

@ashleygwilliams
Copy link
Contributor

@kentonv yeah- very sorry about that. we should do a better job of explaining. sorry about that! and i'll make sure we prioritize this.

@ashleygwilliams ashleygwilliams added docs regression Something is broken, but works in previous releases labels Jun 24, 2019
@ashleygwilliams ashleygwilliams changed the title Global install doesn't work on Linux document using npm with a version manager to avoid permissions errors Jun 24, 2019
@exvuma
Copy link
Contributor

exvuma commented Jul 1, 2019

Probably not best practice but I ran yarn install before to solve the issue on my machine.

@ashleymichal
Copy link
Contributor

Closed by #283. Future improvements will come when we execute on #126 .

@magicfredo
Copy link

Try this :
sudo npm install @cloudflare/wrangler -g --unsafe-perm=true --allow-root
Enjoy

@alexscott2891
Copy link

@magicfredo - Thanks!

@Goblin80
Copy link

Try this :
sudo npm install @cloudflare/wrangler -g --unsafe-perm=true --allow-root
Enjoy

The installation process completed successfully but whenever I try to run wrangler:

/usr/lib/node_modules/@cloudflare/wrangler/node_modules/binary-install/src/binary.js:60
      throw `You have not installed ${this.name ? this.name : "this package"}`;
      ^
You have not installed wrangler
(Use `node --trace-uncaught ...` to show where the exception was thrown)

@EverlastingBugstopper
Copy link
Contributor

@Goblin80 the above recommended sudo npm install is not officially supported nor recommended by the Wrangler team. We recommend using a version manager such as nvm for your node installs which should then allow you to install Wrangler normally.

@negrusti
Copy link

negrusti commented May 9, 2020

Same issue when running in node:latest Docker container (GitLab CI/CD), without sudo. Works with @magicfredo suggestion.

@kulxtreme
Copy link

Same issue when running in node:latest Docker container (GitLab CI/CD), without sudo. Works with @magicfredo suggestion.

It installs "successfully" on newest Fedora and wrangler than throwing

/usr/local/lib/node_modules/@cloudflare/wrangler/node_modules/binary-install/src/binary.js:60
      throw `You have not installed ${this.name ? this.name : "this package"}`;
      ^
You have not installed wrangler

@istiyakamin
Copy link

`istiyak@istiyak-amin:/var/www/html/cf-worker$ wrangler --help

/usr/lib/node_modules/@cloudflare/wrangler/node_modules/binary-install/src/binary.js:60
throw You have not installed ${this.name ? this.name : "this package"};
^
You have not installed wrangler
(Use node --trace-uncaught ... to show where the exception was thrown)
istiyak@istiyak-amin:/var/www/html/cf-worker$

`

The same issue happened also for me

@GabrielDeml
Copy link

npm didn't work for me, but using yarn worked.
yarn global add @cloudflare/wrangler

@mrushyendra
Copy link

It seems like the repo's README has been updated, but not the main 'Getting Started' webpage, which doesn't mention this issue at all.

@georgelzh
Copy link

georgelzh commented Oct 17, 2020

try this
sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
then install again.

@khangly
Copy link

khangly commented Oct 19, 2020

The installation process completed successfully but whenever I try to run wrangler:

/usr/lib/node_modules/@cloudflare/wrangler/node_modules/binary-install/src/binary.js:60
      throw `You have not installed ${this.name ? this.name : "this package"}`;
      ^
You have not installed wrangler
(Use `node --trace-uncaught ...` to show where the exception was thrown)

I got the exact issue, but after I updated npm it worked.

@emt-mocha
Copy link

Hi,
I managed to solve the permission issue with ubuntu by doing the followings at below:

Open terminal and paste the following codes.
mkdir "${HOME}/.npm-packages"
npm config set prefix "${HOME}/.npm-packages"

Use nano editor
nano ~/.bashrc

Paste the following codes at the bottom => .bashrc
NPM_PACKAGES="${HOME}/.npm-packages"
export PATH="$PATH:$NPM_PACKAGES/bin"
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"

Finally
source ~/.bashrc
npm install -g @cloudflare/wrangler

Hope this helps. Happy coding....

@whereisaaron
Copy link

This error still occur as at 6 Sep 2021 just trying to install wrangler into the official node container. #848 did not seem to address it. How to recreate:

Dockerfile

FROM node:14-bullseye-slim
RUN npm install -g @cloudflare/wrangler

Command: docker build .

Error:

[+] Building 8.9s (6/6) FINISHED
 => [internal] load build definition from Dockerfile-foo                                                                                                  0.0s
 => => transferring dockerfile: 114B                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/node:14-bullseye-slim                                                                                  4.0s
 => [auth] library/node:pull token for registry-1.docker.io                                                                                               0.0s
 => CACHED [1/2] FROM docker.io/library/node:14-bullseye-slim@sha256:1b2caf980f422bf707f9dc64879a0a4f9f748d6d3a4e504d8485aa71884c9ec8                     0.0s
 => ERROR [2/2] RUN npm install -g @cloudflare/wrangler                                                                                                   4.7s
------
 > [2/2] RUN npm install -g @cloudflare/wrangler:
#6 4.341 /usr/local/bin/wrangler -> /usr/local/lib/node_modules/@cloudflare/wrangler/run-wrangler.js
#6 4.353
#6 4.353 > @cloudflare/wrangler@1.19.2 postinstall /usr/local/lib/node_modules/@cloudflare/wrangler
#6 4.353 > node ./install-wrangler.js
#6 4.353
#6 4.515 internal/fs/utils.js:314
#6 4.515     throw err;
#6 4.515     ^
#6 4.515
#6 4.515 Error: EACCES: permission denied, mkdir '/root/.wrangler'
#6 4.515     at mkdirSync (fs.js:1009:3)
#6 4.515     at Binary._getInstallDirectory (/usr/local/lib/node_modules/@cloudflare/wrangler/binary-install.js:52:13)
#6 4.515     at Binary.install (/usr/local/lib/node_modules/@cloudflare/wrangler/binary-install.js:78:26)
#6 4.515     at install (/usr/local/lib/node_modules/@cloudflare/wrangler/binary.js:50:10)
#6 4.515     at Object.<anonymous> (/usr/local/lib/node_modules/@cloudflare/wrangler/install-wrangler.js:4:1)
#6 4.515     at Module._compile (internal/modules/cjs/loader.js:1072:14)
#6 4.515     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
#6 4.515     at Module.load (internal/modules/cjs/loader.js:937:32)
#6 4.515     at Function.Module._load (internal/modules/cjs/loader.js:778:12)
#6 4.515     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
#6 4.515   errno: -13,
#6 4.515   syscall: 'mkdir',
#6 4.515   code: 'EACCES',
#6 4.515   path: '/root/.wrangler'
#6 4.515 }
#6 4.585 npm ERR! code ELIFECYCLE
#6 4.585 npm ERR! errno 1
#6 4.589 npm ERR! @cloudflare/wrangler@1.19.2 postinstall: `node ./install-wrangler.js`
#6 4.589 npm ERR! Exit status 1
#6 4.589 npm ERR!
#6 4.589 npm ERR! Failed at the @cloudflare/wrangler@1.19.2 postinstall script.
#6 4.589 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#6 4.602
#6 4.602 npm ERR! A complete log of this run can be found in:
#6 4.602 npm ERR!     /root/.npm/_logs/2021-09-06T07_01_31_977Z-debug.log
------
executor failed running [/bin/sh -c npm install -g @cloudflare/wrangler]: exit code: 1

@stefanct
Copy link

Why even does the readme tell everybody to install wrangler globally when this is obviously not what most setups require? Just remove the -g to install it for your user only and there are no problems AFAICT. This option should probably be the default but needs to be at least mentioned in the readme.

@prdpjngd
Copy link

prdpjngd commented Jan 18, 2022

If you guys are macOS users (Monterey v12.1) ... i tried many solutions this works for me by @younggrandpa 👍🏻

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
docs regression Something is broken, but works in previous releases
Projects
None yet
Development

No branches or pull requests