简体   繁体   English

Ionic,npm和cordova命令都需要sudo才能工作

[英]Ionic, npm and cordova commands all require sudo to work

The sudo, by the looks of what I have seen in some forums, is why I am getting an error when running sudo ionic emulate ios in this post New to ionic - can't build for ios (9) on El Capitan, and sudo ionic is required, deprecated npmconf . sudo,通过我在一些论坛中看到的外观,为什么我在这篇文章中运行sudo ionic emulate ios时出现错误新的离子 - 无法在El Capitan上构建ios(9),并且sudo离子是必需的,不推荐使用npmconf (I am running El Capitan btw). (我正在运行El Capitan btw)。

Whenever I need to use an ionic, npm, or cordova command, I always have to put sudo in front of it otherwise I get a "bash command not found" error. 每当我需要使用离子,npm或cordova命令时,我总是要把sudo放在它前面,否则我会收到“bash command not found”错误。 I have tried to use this fix https://www.npmjs.com/package/npm-sudo-fix but it doesn't work. 我试图使用此修复程序https://www.npmjs.com/package/npm-sudo-fix但它不起作用。 This is what happens in the log: 这是日志中发生的事情:

Dylans-MBP:Ionic Projects Dylan $ sudo npm install -g npm-sudo-fix
Password:
/usr/local/bin/npm-sudo-fix -> /usr/local/lib/node_modules/npm-sudo-fix/index.js
npm-sudo-fix@0.1.3 /usr/local/lib/node_modules/npm-sudo-fix
└── spawny@0.0.1 (cmdify@0.0.4)
Dylans-MBP:Ionic Projects Dylan$ sudo npm-sudo-fix
chown: /users/root/.npm: No such file or directory

Edit (28 August 2018): 编辑(2018年8月28日):

I wrote this post and answer many years ago when I was not very familiar with the terminal. 多年前我写这篇文章并回答我对终端不太熟悉的问题。 Now that I am more experienced, I can recommend the better solution. 既然我更有经验,我可以推荐更好的解决方案。 It is good practice to avoid unnecessary usages of sudo. 避免不必要的sudo用法是一种很好的做法。

I believe the problem is caused by installing node JS from the installer from the node JS website. 我认为问题是由安装程序从节点JS网站安装节点JS引起的。 This version of node JS seems to write various files in the filesystem as root, potentially (and unnecessarily) causing global packages to require root permissions to be installed. 这个版本的节点JS似乎以root身份在文件系统中编写各种文件,可能(并且不必要地)导致全局包需要安装root权限。

The ideal solution would be to completely remove node JS, that was installed from the node JS installer from the website, from your machine (see https://stackabuse.com/how-to-uninstall-node-js-from-mac-osx/ ). 理想的解决方案是从您的机器上完全删除从网站上的节点JS安装程序安装的节点JS(请参阅https://stackabuse.com/how-to-uninstall-node-js-from-mac- osx / )。 Then install node from a commandline package manager, such as homebrew. 然后从命令行包管理器安装节点,例如homebrew。


Original Answer (2016) 原答案(2016)

Finally came up with a solution by playing with the permissions! 终于通过玩权限想出了一个解决方案! Hopefully I didn't stuff up anything. 希望我没有填充任何东西。 (I am a terminal noob btw.) Anyway, here's the solution. (我是一个终端noob btw。)无论如何,这是解决方案。

Run this code here in the terminal (you may or may not have to run cd .. before hand) 在终端中运行此代码(您可能需要或不必在运行之前运行cd ..

sudo chown -R $USER /usr/local/

This changes the permissions of every thing inside the local folder (hence the -R which means recursive). 这会更改本地文件夹中每个内容的权限(因此-R表示递归)。 (I found this line somewhere in the ionic forums). (我在离子论坛的某个地方发现了这条线)。

Now, you have to reinstall cordova and ionic 现在,你必须重新安装cordova和离子

sudo npm install -g cordova
sudo npm install -g ionic

For me anyway, after I do this, I can type ionic and not get any errors. 无论如何,对我来说,在我这样做之后,我可以输入ionic而不会出现任何错误。 I get this in the terminal if I run cordova though. 如果我运行cordova我会在终端得到这个。

/usr/local/lib/node_modules/cordova/node_modules/update-notifier/node_modules/configstore/index.js:53
                throw err;
                ^

Error: EACCES: permission denied, open '/Users/Dylan/.config/configstore/update-notifier-cordova.json'
You don't have access to this file.

What I did to fix this was do this (after opening up a new terminal window): 我做的是解决这个问题(打开一个新的终端窗口后):

sudo chown -R $USER Dylan

(Dylan is my user folder; replace it with yours. Also, you may have to do a cd .. before running that line And that fixes the permissions for that config file. Now running cordova works without errors! Woot! That error might come back though, so you have to run that line of code again. (Dylan是我的用户文件夹;用你的用户文件夹替换它。另外,你可能必须在运行该行之前做一个cd ..这修复了该配置文件的权限。现在运行cordova没有错误!Woot!那个错误可能会来回过头来,所以你必须再次运行那行代码。

Hope this helps somebody! 希望这有助于某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM