npm - FAQ

这里记录常见的包管理相关的错误解决方案

1. node-sass 安装慢

方式 1: 设置 npm 源
然后在 ~/.npmrc 加入下面内容

1
sass_binary_site=https://npmmirror.com/mirrors/node-sass/

方式 2:安装替代工具

1
2
3
4
5
# yarn
$ yarn add node-sass-install
# npm
$ npm i node-sass-install --save-dev
$ npx node-sass-install

这个 node-sass-install 有什么神奇的魔力?其实代码很简单,甚至简单到几乎没有代码。只是在 package.json 的 dependencies 里做了配置(当然因为 npm 比较弱智,所以原项目还是额外增加了两行不太重要的代码):

1
2
3
4
5
{
"dependencies": {
"node-sass": "npm:dart-sass@latest"
}
}

2. node-pre-gyp ERR! build error 错误的几种处理方式

错误信息:
node-pre-gyp ERR! build error  node-pre-gyp ERR! stack Error: Failed to execute ..

解决方式(可能的解决方法)
删除 node_modules 文件夹, 删除 yarn.lock 或者 package.lock 文件, 然后再重新安装

3. 如何更新当前安装的包到最新版本

yarn upgrade 更新依赖包时 yarn.lock 更新但 package.json 不同步更新版本信息

1
2
# 需要手动选择升级的依赖包,按空格键选择,a 键切换所有,i 键反选选择
$ yarn upgrade-interactive --latest

使用 pnpm

1
2
3
4
5
6
7
8
$ pnpm upgrade --interactive
? Choose which packages to update (Press <space> to select, <a> to toggle all, <i> to invert selection) …
❯ ○ @commitlint/cli (dev) 16.2.4 ❯ 16.3.0
○ @types/node (dev) 17.0.31 ❯ 17.0.40
...
○ vue-tsc (dev) 0.34.11 ❯ 0.34.17

Enter to start updating. Ctrl-c to cancel.

4. 源使用国内, 但是包发布到 npm 上, 如何进行同步

打开网站 https://npmmirror.com/ 搜索包, 搜索到之后点击 sync 等待同步完成之后本地即可安装

5. Allocation failed - JavaScript heap out of memory

参考链接 : https://github.com/vitejs/vite/issues/2433#issuecomment-1159005523
这里遇到的问题是基于前端进行构建的, 和 jenkins 没有什么关系, 所以记录在前端这里, 报错的详细原因

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<--- Last few GCs --->

[12469:0x502f210] 76276 ms: Scavenge (reduce) 2039.1 (2082.7) -> 2038.9 (2082.9) MB, 4.9 / 0.0 ms (average mu = 0.289, current mu = 0.128) allocation failure
[12469:0x502f210] 77729 ms: Mark-sweep (reduce) 2039.8 (2083.4) -> 2039.5 (2084.2) MB, 1450.3 / 0.0 ms (average mu = 0.189, current mu = 0.021) allocation failure scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
1: 0xb0a860 node::Abort() [node]
2: 0xa1c193 node::FatalError(char const*, char const*) [node]
3: 0xcf9a6e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xcf9de7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xeb1685 [node]
6: 0xec134d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
7: 0xec404e v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
8: 0xe8558a v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
9: 0x11fe2d6 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
10: 0x15f2d39 [node]
Finished: SUCCESS

这里的默认内存限制在机器上是 1.7G 左右, 如有错误, 请联系我订正

What is the memory limit on a node process?
Currently, by default v8 has a memory limit of 512mb on 32-bit systems, and 1gb on 64-bit systems. The limit can be raised by setting –max_old_space_size to a maximum of 1024 (1 GiB) (32-bit) and 1741 (1.7GiB) (64-bit), but it is recommended that you split your single process into several workers if you are hitting memory limits.

可以通过设置参数来进行调整, 我们可以将 vite 的打包命令调整为

1
$ node --max_old_space_size=16384 ./node_modules/vite/bin/vite.js build --mode=dev

参考文章


语雀镜像 : npm - FAQ ,点此 提问

作者

Duoli

发布于

2022-04-20

更新于

2022-12-08

许可协议

评论