Npm Please try using this command again as root/ad

2019-01-08 08:12发布

I've been desperately trying to install modules using node.js but it always fails getting packages with npm.

I logged in as Administrator and used powershell/cmd with "run as administrator". I also had problems with the registry so I used npm set registry http://registry.npmjs.org/

I tried everything... it just drives me nuts.

Here are the errors:

npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Windows_NT 6.2.9200
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "generator-knockout"
npm ERR! cwd D:\Sergiu\Knockout Test
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path D:\Sergiu\Knockout Test\node_modules\generator-knockout\node_modules\yeoman-generator\node_modules\tar\node_modules\block-stream\block-stream.js
npm ERR! fstream_path D:\Sergiu\Knockout Test\node_modules\generator-knockout\node_modules\yeoman-generator\node_modules\tar\node_modules\block-stream\block-stream.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code EPERM
npm ERR! errno 50
npm ERR! stack Error: EPERM, lstat 'D:\Sergiu\Knockout Test\node_modules\generator-knockout\node_modules\yeoman-generator\node_modules\tar\node_modules\block-stream\block-stream.js'
npm ERR! fstream_stack C:\Program Files\nodejs\node_modules\npm\node_modules\fstream\lib\writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: EPERM, lstat 'D:\Sergiu\Knockout Test\node_modules\generator-knockout\node_modules\yeoman-generator\node_modules\tar\node_modules\fstream\LICENSE'
npm ERR!  { [Error: EPERM, lstat 'D:\Sergiu\Knockout Test\node_modules\generator-knockout\node_modules\yeoman-generator\node_modules\tar\node_modules\fstream\LICENSE']
npm ERR!   errno: 50,
npm ERR!   code: 'EPERM',
npm ERR!   path: 'D:\\Sergiu\\Knockout Test\\node_modules\\generator-knockout\\node_modules\\yeoman-generator\\node_modules\\tar\\node_modules\\fstream\\LICENSE',
npm ERR!   fstream_type: 'File',
npm ERR!   fstream_path: 'D:\\Sergiu\\Knockout Test\\node_modules\\generator-knockout\\node_modules\\yeoman-generator\\node_modules\\tar\\node_modules\\fstream\\LICENSE',
npm ERR!   fstream_class: 'FileWriter',
npm ERR!   fstream_stack:
npm ERR!    [ 'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\fstream\\lib\\writer.js:284:26',
npm ERR!      'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

28条回答
走好不送
2楼-- · 2019-01-08 08:32

Also remember to end all other commands e.g. npm run dev-server.

查看更多
混吃等死
3楼-- · 2019-01-08 08:35

It turns out that you don’t have to run the command again as Administrator, and doing so won’t fix the problem.

Try:

  1. npm cache clean first.

  2. If that doesn’t fix things, take a look in %APPDATA%\npm-cache, or if you’re using PowerShell, $env:APPDATA\npm-cache.

After cleaning the cache, you may still be left with remnants. Manually remove everything in that directory, and try again. This has always fixed things for me.

As @Crazzymatt was mentioning, as of the npm@5 version and up, we need to use npm cache verify instead of npm cache clean. Or else you will get an error as preceding.

npm ERR! As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead.

(Source: MSDN Blog post)

查看更多
爷的心禁止访问
4楼-- · 2019-01-08 08:35

Here is how I fixed this on my Windows (7) Dev. environment. I assume the following...

  • You are Running Command Prompt, Git Bash, Cmder or your favorite Terminal, as an Administrator by right clicking
  • Privileges Permissions have been granted for Read/Write (i.e chmod -777)

Okay, let's get to it:

  1. Update any packages where a version check is returning a warning ("npm WARN"..) for example...

    npm update -g npm

    npm update -g graceful-fs

  2. Next we want to force a cache clean. This is flagged by an '--f' or '-f'..

    npm cache clean --f

  3. In Widows Explorer search for the following path

%APPDATA%\npm-cache

And Delete it's contents

  1. Start a fresh instance of your Terminal, remembering to 'Right-Click' and 'Run as Administrator', install the packages again.

Hope this helps someone!!

查看更多
女痞
5楼-- · 2019-01-08 08:35

If you're using TFS or any other source control for your project that sets your checked in files to readonly mode, then you gotta make sure package.json is checked out before running npm install. I've made this mistake plenty of times.

查看更多
神经病院院长
6楼-- · 2019-01-08 08:35

This is the flow often happens in this case. You run a command with no admin rights, you get message npm ERR! Please try running this command again as root/Administrator.. Then you open one more CLI(cmd, powershell, bash or whatever) and don't close the previous CLI. It appears you have 2 prompts opened in the same directory. And until you close CLI which runs with no admin rights you will be continuously getting npm ERR! Please try running this command again as root/Administrator. So close CLI which runs with no admins rights before running a new one.

NOTE: a lot of IDE has embedded CLI(Visual Studio, VS Code etc) so please close the instance of IDE as well

查看更多
可以哭但决不认输i
7楼-- · 2019-01-08 08:35

I messed with this problem 2 times and tried all suggestions here with mediocre success.

  • First time I tried (and succeeded luckily) by:

    1. delete the node_modules folder in your project

    2. npm clean cache -f

    3. check your %APPDATA%/npm-cache and delete everything if not empty

    4. start the terminal as administrator (in my opinion, it isnt a problem of rights, but it wont hurt if you do so)

    5. npm install and then npm update

    6. you will get errors, just install every module that throws an error manually and step by step try to eliminate everything whats wrong

However, this method is quite frustrating and not working everytime. So try this:

  • Second time I had no luck with the steps above, so I tried to completely reinstall npm and it failed. So I thought there must something be wrong with my node.js installation. My team is running 6.10. and I had the 6.11. and tried it with the 6.10. with no luck.

    1. So I completely wiped everything off my system. Deleted all node modules, checked every path where node or npm could be and cleaned my system.

    2. Then I installed newest 8.1.3

    3. Deleted my project and cloned it to a complete different directory.

    4. Now I installed and updated everything with npm install npm update

    5. Then I compiled with just 2 modules missing and installed them manually

Tadaaaaa, its working now. So all in all it was a problem with node.js. I recommend to everyone who runs into this problem: Try different node versions! And clean ABSOLUTE everything.

查看更多
登录 后发表回答