我正在尝试在节点项目上安装npm软件包,但是无论我做什么,该模块都会继续在全局安装。
我当前的目录为D:\wrkfldr\NodeTemp
,其中包含package.json
:
{
"name": "nodetemp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
如果我运行npm install websocket
,则package.json
保持不变,并且模块已在C:\Users\Me\AppData\Roaming\npm\node_modules
中全局安装
在安装软件包时,我收到以下消息:
PS D:\wrkfldr\NodeTemp> npm install websocket
> websocket@1.0.28 install C:\Users\Me\AppData\Roaming\npm\node_modules\websocket
> (node-gyp rebuild 2> builderror.log) || (exit 0)
C:\Users\Me\AppData\Roaming\npm\node_modules\websocket>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild ) else (node "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
+ websocket@1.0.28
added 7 packages from 15 contributors in 5.812s
我正在Windows下工作,并且刚刚删除/重新安装了节点,但是并不能解决问题。
如何获取软件包以在本地安装?
注意:如果我在package.json
中手动添加依赖项,然后运行npm install
,则它可以正常工作,目录node_modules已创建,并且导入工作正常。
这是我的npm-config ls -l
; cli configs
long = true
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.4.1 node/v10.15.0 win32 x64"
; userconfig C:\Users\Me\.npmrc
global = true
; builtin config undefined
prefix = "C:\\Users\\Me\\AppData\\Roaming\\npm"
; default values
access = null
allow-same-version = false
also = null
always-auth = false
audit = true
audit-level = "low"
auth-type = "legacy"
bin-links = true
browser = null
ca = null
cache = "C:\\Users\\Me\\AppData\\Roaming\\npm-cache"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = undefined
cert = null
cidr = null
color = true
commit-hooks = true
depth = null
description = true
dev = false
dry-run = false
editor = "notepad.exe"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
force = false
git = "git"
git-tag-version = true
; global = false (overridden)
global-style = false
globalconfig = "C:\\Users\\Me\\AppData\\Roaming\\npm\\etc\\npmrc"
globalignorefile = "C:\\Users\\Me\\AppData\\Roaming\\npm\\etc\\npmignore"
group = 0
ham-it-up = false
heading = "npm"
https-proxy = null
if-present = false
ignore-prepublish = false
ignore-scripts = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "C:\\Users\\Me\\.npm-init.js"
init-version = "1.0.0"
json = false
key = null
legacy-bundling = false
link = false
local-address = undefined
loglevel = "notice"
logs-max = 10
; long = false (overridden)
maxsockets = 50
message = "%s"
; metrics-registry = null (overridden)
node-options = null
node-version = "10.15.0"
noproxy = null
offline = false
onload-script = null
only = null
optional = true
otp = null
package-lock = true
package-lock-only = false
parseable = false
prefer-offline = false
prefer-online = false
; prefix = "C:\\Program Files\\nodejs" (overridden)
preid = ""
production = false
progress = true
proxy = null
read-only = false
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = true
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
save-prod = false
scope = ""
script-shell = null
scripts-prepend-node-path = "warn-only"
searchexclude = null
searchlimit = 20
searchopts = ""
searchstaleness = 900
send-metrics = false
shell = "C:\\WINDOWS\\system32\\cmd.exe"
shrinkwrap = true
sign-git-commit = false
sign-git-tag = false
sso-poll-frequency = 500
sso-type = "oauth"
strict-ssl = true
tag = "latest"
tag-version-prefix = "v"
timing = false
tmp = "C:\\Users\\LEROIA~1\\AppData\\Local\\Temp"
umask = 0
unicode = false
unsafe-perm = true
update-notifier = true
usage = false
user = 0
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch}" (overridden)
userconfig = "C:\\Users\\Me\\.npmrc"
version = false
versions = false
viewer = "browser"
答案 0 :(得分:0)
您安装软件包的默认设置被覆盖。运行命令npm config ls -l
时,可以按格式; key = default_value (overridden)
来识别被覆盖的值。当您运行上述命令时,被覆盖的值也位于输出的顶部。它将显示如下内容:
; cli configs
(...)
; userconfig C:\Users\Me\.npmrc
global = true ---> here you can see it is overridden
; globalconfig /path/to/global/config/file
(...) ----> if you got globally overridden default values, you see those values here
; default values
(...)
; global = false (overridden) --> here you can see what the default value is
(...)
在您的情况下,global
的值在C:\Users\Me\.npmrc
中被覆盖(如果您将其去离子化,则将其更改为实际路径)。打开该文件并删除global = true
行。您也可以使用npm-config
CLI,但是我的经验是,这种方法并不总是有效。
删除该行之后,再次运行npm config ls -l
,以验证配置是否恢复为默认值。