简体   繁体   English

无法让“npm install -g”在任何包上工作(AppData/Roaming/npm 总是空的)

[英]Cannot get "npm install -g" to work on any packages (AppData/Roaming/npm always empty)

Running nodejs on Windows 7 Enterprise at work.在 Windows 上运行 nodejs 7 企业在工作。

Whenever I install a node_module that needs -g access, from experience I know it's supposed to create a *.bat file in %AppData$/Roaming/npm , but for some reason it no longer does that.每当我安装需要 -g 访问权限的 node_module 时,根据经验我知道它应该在%AppData$/Roaming/npm中创建一个 *.bat 文件,但由于某种原因它不再这样做。

For example, I will run npm install gulp -g , console looks like it installed correctly, but the files will not be in the AppData folder.例如,我将运行npm install gulp -g ,控制台看起来安装正确,但文件不会在 AppData 文件夹中。 And if I try running a gulp command, I get error sh.exe": gulp: command not found .如果我尝试运行 gulp 命令,我会收到错误sh.exe": gulp: command not found

If I run the npm install gulp -g command in Console As Administrator, it installs the files into the %AppData% folder of the administrator (instead of the regular user).如果我以管理员身份在控制台中运行npm install gulp -g命令,它会将文件安装到管理员(而不是普通用户)的 %AppData% 文件夹中。 So if I run the gulp command through my non-administrator user, I still get error sh.exe": gulp: command not found .因此,如果我通过我的非管理员用户运行 gulp 命令,我仍然会收到错误sh.exe": gulp: command not found

Any ideas?有任何想法吗?

Found solution: 找到解决方案

(1) Upon running the command: npm config get prefix , output is C:\\Program Files (x86)\\Git\\local . (1)运行命令时: npm config get prefix ,输出为C:\\Program Files (x86)\\Git\\local No idea why it was set to this, as it's not the default. 不知道为什么会这样设置,因为它不是默认值。

But I changed it using: npm config set prefix "$APPDATA\\npm" . 但是我使用以下命令更改了它: npm config set prefix "$APPDATA\\npm"

Now when I install a --g module, ie. 现在我安装了一个--g模块,即。 npm install gulp -g , it installs into this desirable directory, no longer throwing EPERM and ENOENT errors. npm install gulp -g ,它安装到这个理想的目录中,不再抛出EPERMENOENT错误。

(2) Still need to add a PATH entry for the npm folder. (2)仍然需要为npm文件夹添加PATH条目。 The command export PATH=$PATH:/c/Users/{YOUR_USERNAME}/AppData/Roaming/npm works temporarily, but if you close console and open it again, might not be saved (if you are not an administrator). 命令export PATH=$PATH:/c/Users/{YOUR_USERNAME}/AppData/Roaming/npm暂时有效,但如果您关闭控制台并再次打开它,则可能无法保存(如果您不是管理员)。

But you can also use echo 'export PATH=$PATH:/c/Users/{YOUR_USERNAME}/AppData/Roaming/npm' >> ~/.bash_profile , which will create a .bash_profile file, which is run each time as your console is opened. 但你也可以使用echo 'export PATH=$PATH:/c/Users/{YOUR_USERNAME}/AppData/Roaming/npm' >> ~/.bash_profile ,这将创建一个.bash_profile文件,每次运行时都会运行控制台已打开。 So from this point, it should automatically add the required PATH entry. 所以从这一点来说,它应该自动添加所需的PATH条目。

I also faced this same issue.我也遇到了同样的问题。

After installing node.js( https://nodejs.org/en/download/ ) npm folder(in appdata folder) remain empty.安装 node.js 后( https://nodejs.org/en/download/npm文件夹(在appdata文件夹中)保持为空。

so, at this stage if you try to build/run angular project(ng build/ng serve), it will give error as:所以,在这个阶段,如果你尝试构建/运行 angular 项目(ng build/ng serve),它会给出如下错误:

The term 'ng' is not recognized as the name of a cmdlet, function, script file, or operable program. 

So, for fixing this issue install angular globally in your project with following command:因此,要解决此问题,请使用以下命令在您的项目中全局安装 angular:

npm install -g @angular/cli

Now, there would be data in npm folder(node modules etc.) and ng command will run now.现在,npm 文件夹(节点模块等)中将有数据并且 ng 命令现在将运行。

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

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