npm install 失败并显示“子集不是函数"
我在 Arch Linux x86_64
上有 node v15.14.0
和 npm 7.8.0
(从该发行版的存储库安装).
I have node v15.14.0
and npm 7.8.0
on Arch Linux x86_64
(installed from that distro's repos).
使用 npm init
在空目录中启动项目,然后尝试使用 npm i
安装某些东西总是以同样的方式失败.一个例子,在$HOME/tmp
中运行:
Starting a project in an empty directory with npm init
and then trying to install something with npm i <blah>
always fails in the same fashion. An example, run in $HOME/tmp
:
$ npm i btoa
npm ERR! subset is not a function
npm ERR! A complete log of this run can be found in:
npm ERR! $HOME/.npm/_logs/2021-04-08T00_32_19_504Z-debug.log
该日志文件内容如下:
0 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'i', 'btoa' ]
1 info using npm@7.8.0
2 info using node@v15.14.0
3 timing npm:load:whichnode Completed in 0ms
4 timing config:load:defaults Completed in 1ms
5 timing config:load:file:/usr/lib/node_modules/npm/npmrc Completed in 0ms
6 timing config:load:builtin Completed in 0ms
7 timing config:load:cli Completed in 1ms
8 timing config:load:env Completed in 0ms
9 timing config:load:file:$HOME/tmp/.npmrc Completed in 0ms
10 timing config:load:project Completed in 1ms
11 timing config:load:file:$HOME/.npmrc Completed in 0ms
12 timing config:load:user Completed in 0ms
13 timing config:load:file:/usr/etc/npmrc Completed in 0ms
14 timing config:load:global Completed in 0ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setEnvs Completed in 1ms
17 timing config:load Completed in 5ms
18 timing npm:load:configload Completed in 5ms
19 timing npm:load:setTitle Completed in 0ms
20 timing npm:load:setupLog Completed in 1ms
21 timing npm:load:cleanupLog Completed in 1ms
22 timing npm:load:configScope Completed in 1ms
23 timing npm:load:projectScope Completed in 0ms
24 timing npm:load Completed in 9ms
25 timing config:load:flatten Completed in 2ms
26 timing arborist:ctor Completed in 0ms
27 timing arborist:ctor Completed in 0ms
28 timing idealTree:init Completed in 13ms
29 timing idealTree:userRequests Completed in 3ms
30 silly idealTree buildDeps
31 silly fetch manifest btoa@*
32 http fetch GET 304 https://registry.npmjs.org/btoa 193ms (from cache)
33 silly placeDep ROOT btoa@1.2.1 KEEP for: tmp@1.0.0 want: *
34 timing idealTree:#root Completed in 198ms
35 timing idealTree:buildDeps Completed in 199ms
36 timing idealTree:fixDepFlags Completed in 0ms
37 timing idealTree Completed in 217ms
38 timing reify:loadTrees Completed in 217ms
39 timing reify:diffTrees Completed in 0ms
40 silly reify moves {}
41 timing reify:retireShallow Completed in 0ms
42 timing reify:createSparse Completed in 0ms
43 timing reify:loadBundles Completed in 0ms
44 timing reify:unpack Completed in 0ms
45 timing reify:unretire Completed in 0ms
46 timing build:queue Completed in 0ms
47 timing build:deps Completed in 1ms
48 timing build Completed in 1ms
49 timing reify:build Completed in 1ms
50 timing reify:trash Completed in 0ms
51 timing command:install Completed in 230ms
52 verbose stack TypeError: subset is not a function
52 verbose stack at Arborist.[saveIdealTree] (/usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:956:37)
52 verbose stack at /usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:138:39
52 verbose stack at async Arborist.reify (/usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:134:5)
52 verbose stack at async Install.install (/usr/lib/node_modules/npm/lib/install.js:137:5)
53 verbose cwd $HOME/tmp
54 verbose Linux 5.10.25-1-lts
55 verbose argv "/usr/bin/node" "/usr/bin/npm" "i" "btoa"
56 verbose node v15.14.0
57 verbose npm v7.8.0
58 error subset is not a function
59 verbose exit 1
所以你可以看到
- 我的全球"模块位于
/usr/lib/node_modules
(我可以在其中使用sudo npm install -g
安装得很好); -
subset
函数似乎在install
命令调用时弹出
- my "global" modules live in
/usr/lib/node_modules
(where I can install just fine withsudo npm install -g
); - the
subset
function seems to pop up when theinstall
command calls
/usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js
我已经检查过那个文件定义了
I've checked that that file defines
const {subset} = require('semver')
我该如何解决这个问题?
How do I fix this?
这是为了解决下面的评论:这个subset
问题不管我尝试安装什么.btoa
只是一个例子.
This is to address a comment below: this subset
issue occurs regardless of what I try to install. btoa
was nothing but an example.
我能够通过更改 /usr/lib/node_modules/npm/node_modules/semver/ranges/subset.js
来重现您的问题> 成为一个空文件.听起来您在安装 npm
时出了点问题,并且至少有一个依赖项 (semver
) 没有完全安装.或者也许后来发生了一些事情,将文件覆盖为一个空文件.
I was able to replicate your problem by changing /usr/lib/node_modules/npm/node_modules/semver/ranges/subset.js
to be an empty file. Sounds like something went wrong during your installation of npm
and at least one dependency (semver
) didn't fully install. Or maybe somehow something happened later that overwrote the file to be an empty file.
最好的办法是重新安装 node
和 npm
.但是您可以尝试一些更窄的事情.你说 npm install -g
正在工作.我觉得这很令人惊讶,但正因为如此,您可以尝试 npm install -g npm
重新安装 npm
本身.那应该可以解决问题.如果这不起作用,您可以尝试 npm install --force -g npm
.
Your best bet is to reinstall node
and npm
. But there are narrower things you can try. You say npm install -g
is working. I find that surprising, but because that's the case, you can try npm install -g npm
to reinstall npm
itself. That should fix things. If that doesn't work, you can try npm install --force -g npm
.
但同样:最好的办法是重新安装 node
和 npm
.
But again: Your best bet is to reinstall node
and npm
.