我试图在phpstorm上设置较少,这样我就可以在保存时将.less文件编译为.css。我已经安装了node.js,下一步(根据这个https://www.jetbrains.com/webstorm/help/transpiling-sass-less-and-scss-to-css.html)是在终端上运行这个命令

npm install -g less

然而,当我这样做时,我得到这些错误

☁  ~  npm install -g less
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/mkdirp/0.5.1'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/graceful-fs/3.0.8'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/extend/3.0.0'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/readable-stream/2.0.4'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/chalk/1.1.1'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/xtend/4.0.1'
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
/usr/local/lib
└─┬ less@2.5.3
  ├─┬ errno@0.1.4
  │ └── prr@0.0.0
  ├── image-size@0.3.5
  ├── mime@1.3.4
  ├─┬ promise@6.1.0
  │ └── asap@1.0.0
  └─┬ source-map@0.4.4
    └── amdefine@1.0.0

npm ERR! Darwin 15.0.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "less"
npm ERR! node v5.0.0
npm ERR! npm  v3.3.6
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/brentscholl/npm-debug.log

我在这里完全是个新手,不知道下一步该做什么。任何帮助都将非常感激!


当前回答

使用node版本管理器重新安装node和npm(按照npm文档中所写的),以避免权限错误:

在OSX:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

或者关注这篇文章:

http://dev.topheman.com/install-nvm-with-homebrew-to-use-multiple-versions-of-node-and-iojs-easily/

Windows用户应该安装nvm-windows。 有关如何安装nvm的进一步帮助,请参阅nvm自述文件。

然后选择例子:

nvm install 8.0.0
nvm use 8.0

现在你可以再试一次:

npm install -g less

其他回答

你可以试试用sudo,像下面这样

sudo npm install -g less

如果它不起作用,您可以参考这个链接 https://ionicframework.com/docs/developing/tips#resolving-permission-errors

对于我的mac环境

sudo chown -R $USER /usr/local/lib/node_modules

解决问题

在终端窗口中运行这些命令(注意:不要替换$USER部分…这是一个Linux命令来获取你的用户!)

sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules

不建议使用sudo。它可能稍后给你许可问题。 虽然上述方法可行,但我不赞成将root用户拥有的文件夹更改为用户可写的文件夹,尽管这可能只是多个用户的问题。 为了解决这个问题,你可以使用“npm users”组,但这也会增加管理开销。 查看这里的选项来处理文档中的权限: https://docs.npmjs.com/getting-started/fixing-npm-permissions

我选择选项2:

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder. Make a directory for global installations: mkdir ~/.npm-global Configure npm to use the new directory path: npm config set prefix '~/.npm-global' Open or create a ~/.profile file and add this line: export PATH=~/.npm-global/bin:$PATH Back on the command line, update your system variables: source ~/.profile Test: Download a package globally without using sudo. npm install -g jshint If still show permission error run (mac os): sudo chown -R $USER ~/.npm-global

这适用于默认的ubuntu安装:

sudo apt-get install nodejs npm

如果你想要更灵活地管理版本,我推荐nvm: https://github.com/creationix/nvm

在MacOS上使用brew,如果你使用的是最新的npm版本,它应该可以在没有sudo的情况下工作。 享受:)

可以在root用户密码下使用该命令为node_module文件夹增加写入权限:

sudo chown -R $USER /usr/local/lib/node_modules