Skip to content

npm - FAQ

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

包安装

node-sass 安装慢

方式 1: 设置 npm 源

然后在 ~/.npmrc 加入下面内容

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

方式 2:安装替代工具

# 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 比较弱智,所以原项目还是额外增加了两行不太重要的代码):

json
{
	"dependencies": {
		"node-sass": "npm:dart-sass@latest"
	}
}

参考:

🔗 https://blog.csdn.net/qq_31201781/article/details/106147842

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 文件, 然后再重新安装

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

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

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

使用 pnpm

$ 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.

npm error when installing pinia ‘Conflicting peer dependency: vue@3.2.23’

详细错误

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: repo@0.0.0-development
npm ERR! Found: vue@3.2.22
npm ERR! node_modules/vue
npm ERR!   peerOptional vue@"^2 || ^3.0.0-0" from @vue/babel-preset-app@4.5.15
npm ERR!   node_modules/@vue/babel-preset-app
npm ERR!     @vue/babel-preset-app@"^4.5.15" from @vue/cli-plugin-babel@4.5.15
npm ERR!     node_modules/@vue/cli-plugin-babel
npm ERR!       dev @vue/cli-plugin-babel@"~4.5.0" from the root project
npm ERR!   peer vue@"3.2.22" from @vue/server-renderer@3.2.22
npm ERR!   node_modules/@vue/server-renderer
npm ERR!     @vue/server-renderer@"3.2.22" from vue@3.2.22
npm ERR!   5 more (@vue/test-utils, primevue, vue-jest, vue-router, the root project)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! pinia@"*" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: vue@3.2.23
npm ERR! node_modules/vue
npm ERR!   peer vue@">= 2.5 < 3" from @vue/composition-api@1.4.0
npm ERR!   node_modules/@vue/composition-api
npm ERR!     peerOptional @vue/composition-api@"^1.4.0" from pinia@2.0.4
npm ERR!     node_modules/pinia
npm ERR!       pinia@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See C:\Users\me\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\me\AppData\Local\npm-cache\_logs\2021-11-26T09_39_36_434Z-debug.log

主要的原因是在项目中找到了两个 vue 依赖包, 不知道安装哪一个更合适, 只要显式的在项目依赖中添加上vue 的依赖即可

json
{
	"dependencies": {
    "vue": "^3.2.33"
  }
}

yarn 下载包提示 : "There appears to be trouble with your network connection. Retrying..."

问题

今天早上准备使用 taro 的时候无法下载, 提示网络问题, 但是网络的访问都是正常的, npm, pnpm 都是正常的

解决

一般网上给到的方法都是设置超时时间, 但是不起作用

yarn install --network-timeout 1000000

当然, 前提是确保没有设置代理

npm config rm proxy
npm config rm https-proxy

我这里的问题是前两天使用 pnpm 下载东西的时候给 npm 设置了一个代理, 因为代理的模式 yarn 不支持, 所以出现的问题,
解决方法就是换一个支持的代理或者移除代理

国内源使用

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

打开网站 https://npmmirror.com/ 搜索包, 搜索到之后点击 sync 等待同步完成之后本地即可安装, 或者在本地安装 cnpm 包, 使用 cnpm sync package 命令

打包 / 构建

Allocation failed - JavaScript heap out of memory

🔗 https://github.com/vitejs/vite/issues/2433#issuecomment-1159005523

这里遇到的问题是基于前端进行构建的, 和 jenkins 没有什么关系, 所以记录在前端这里, 报错的详细原因

<--- 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 的打包命令调整为

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

说明

创建时间: 2023-01-15 00:32:00 , 最后编辑于 2023-12-20 22:40:00