NPM 全局标志在 Windows 上似乎不一致 [英] The NPM global flag seems to be inconsistent on Windows

查看:36
本文介绍了NPM 全局标志在 Windows 上似乎不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从控制台运行

> npm root -g

或以编程方式

var npm = require("npm");
npm.load(null, function (err, npm) {
    npm.config.set("global", true);
    npm.root;
});

我在 Windows 上得到了不同的结果.第一个返回 C:\Users\myuser\AppData\Roaming\npm\node_modules 和第二个 C:\Program Files (x86)\nodejs\node_modules.当我安装模块时也会发生同样的情况,以编程方式执行此操作会尝试将其安装在程序文件而不是 AppData 上.

I get a different results on Windows. The first one returns C:\Users\myuser\AppData\Roaming\npm\node_modules and the second one C:\Program Files (x86)\nodejs\node_modules. The same happens when I install a module, doing this programmatically tries to install it on program files instead of AppData.

这可能是一个错误吗?还是我做错了什么?

Could this be a bug? Or am I doing something wrong?

在 linux 上它始终如一地工作.我没在 Mac 上试过.

On linux it works consistently. I've not tried on Mac.

更新:我在 Mac 上试过了,效果很好.

Update: I tried this on Mac and it works fine.

推荐答案

区别似乎在于 Windows 的 Node.js 安装中包含的 npmrc:

The difference appears to be the npmrc that's included in the Node.js install for Windows:

# C:\Program Files (x86)\nodejs\node_modules\npm\npmrc

prefix=${APPDATA}\npm

它的路径定位它被处理 asbuiltin 配置,而不是 全局用户 配置,因此 npmlocal"安装不会处理它.直接执行本地安装就可以看到这个:

It's path positions it to be handled as a builtin config, rather than a global or user config, so "local" installs of npm won't process it. You can see this when executing the local install directly:

> .\node_modules\.bin\npm root -g
C:\Program Files (x86)\nodejs\node_modules

这篇关于NPM 全局标志在 Windows 上似乎不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆