Wrangler: document using npm with a version manager to avoid permissions errors

Created on 11 Jun 2019  路  24Comments  路  Source: cloudflare/wrangler

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/[email protected] 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/[email protected] postinstall: `node install-wrangler.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @cloudflare/[email protected] 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
category - docs priority - critical user report

Most helpful comment

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

All 24 comments

how do you have node installed?

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

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.

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.

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.

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).

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/[email protected] 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

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.)

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.

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...

@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.

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

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

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

@magicfredo - Thanks!

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)

@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.

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

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

`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

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

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.

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ispivey picture ispivey  路  4Comments

nickbalestra picture nickbalestra  路  5Comments

GregBrimble picture GregBrimble  路  6Comments

EverlastingBugstopper picture EverlastingBugstopper  路  3Comments

shyim picture shyim  路  5Comments