升级后无package-lock.json时npm install触发内存不足错误
npm install 缺失 package-lock.json 时触发内存不足错误
升级项目依赖、Node.js 及 npm 后,执行 npm install 时,若不存在 package-lock.json 文件就会触发内存不足错误。
出错环境
- Ubuntu 23.10 系统(8GB 内存)
- Node.js v18.19.1
- npm 10.5.0
当存在 package-lock.json 时,npm install 可成功执行;但缺失该文件时,新版本的依赖解析阶段内存占用比旧版本显著更高。
错误日志
$ npm install npm WARN using --force Recommended protections disabled. (⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂) ⠧ idealTree:@angular/core: sill placeDep ROOT zone.js@0.14.4 OK for: my-app@3.4.0 want: ^0.14.4 <--- Last few GCs --- [6093:0x6643c70] 32888 ms: Scavenge 1987.5 (2035.9) -> 1985.7 (2035.4) MB, 7.8 / 0.0 ms (average mu = 0.704, current mu = 0.714) task; [6093:0x6643c70] 33552 ms: Mark-sweep (reduce) 2064.0 (2110.8) -> 2058.5 (2095.6) MB, 455.9 / 0.1 ms (+ 101.8 ms in 28 steps since start of marking, biggest step 8.6 ms, walltime since start of marking 575 ms) (average mu = 0.500, current mu = 0.301) <--- JS stacktrace --- FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb95be0 node::Abort() [npm install] 2: 0xa9a7f8 [npm install] 3: 0xd6f5b0 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [npm install] 4: 0xd6f957 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [npm install] 5: 0xf4ceb5 [npm install] 6: 0xf5f38d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [npm install] 7: 0xf39a7e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [npm install] 8: 0xf3ae47 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [npm install] 9: 0xf1b3c0 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [npm install] 10: 0xf12e34 v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [npm install] 11: 0xf150e8 v8::internal::FactoryBase<v8::internal::Factory>::NewRawOneByteString(int, v8::internal::AllocationType) [npm install] 12: 0x1055349 v8::internal::JsonParser<unsigned short>::MakeString(v8::internal::JsonString const&, v8::internal::Handle<v8::internal::String>) [npm install] 13: 0x1057066 v8::internal::JsonParser<unsigned short>::ParseJsonValue() [npm install] 14: 0x1057b4f v8::internal::JsonParser<unsigned short>::ParseJson() [npm install] 15: 0xdf5213 v8::internal::Builtin_JsonParse(int, unsigned long*, v8::internal::Isolate*) [npm install] 16: 0x170e179 [npm install] Aborted (core dumped)
复现步骤
- 使用如下
package.json配置:
{ "name": "npm-oom", "version": "0.0.1", "dependencies": { "@angular/animations": "^16.2.12", "@angular/cdk": "^16.2.14", "@angular/cli": "^16.2.12", "@angular/common": "^16.2.12", "@angular/compiler": "^16.2.12", "@angular/compiler-cli": "^16.2.12", "@angular/core": "^16.2.12", "@angular/forms": "^16.2.12", "@angular/language-service": "^16.2.12", "@angular/material": "^16.2.14", "@angular/platform-browser": "^16.2.12", "@angular/platform-browser-dynamic": "^16.2.12", "@angular/router": "^16.2.12", "@sendgrid/mail": "^8.1.1", "@stomp/stompjs": "^7.0.0", "@types/file-saver-es": "^2.0.3", "cross-var": "^1.1.0", "enhanced-resolve": "^5.15.1", "file-saver-es": "^2.0.5", "jwt-decode": "^4.0.0", "ng-storages": "^1.1.5", "npm": "^10.5.0", "reflect-metadata": "^0.2.1", "reinstall": "^2.0.0", "rxjs": "^7.4.0", "tslib": "^2.0.0", "typescript": "^5.1.6", "zone.js": "^0.14.4" }, "devDependencies": { "@angular-devkit/build-angular": "^16.2.12", "@angular-eslint/builder": "^16.3.1", "@angular-eslint/eslint-plugin": "^16.3.1", "@angular-eslint/eslint-plugin-template": "^16.3.1", "@angular-eslint/schematics": "^16.3.1", "@angular-eslint/template-parser": "^16.3.1", "@types/jasmine": "^5.1.4", "@types/node": "^20", "@typescript-eslint/eslint-plugin": "^7.1.1", "@typescript-eslint/parser": "^7.1.1", "jasmine-core": "^5.1.2", "jasmine-spec-reporter": "^7.0.0", "karma": "^6.3.14", "karma-chrome-launcher": "^3.1.0", "karma-coverage-istanbul-reporter": "^3.0.2", "karma-jasmine": "^5.1.0", "karma-jasmine-html-reporter": "^2.1.0", "protractor": "^7.0.0", "ts-node": "^10.0.0", "webdriver-manager": "^12.1.9" } }
- 执行命令:
npm install -g npm@10.5.0; rm -rf node_modules/ package-lock.json; npm install --force
内容的提问来源于stack exchange,提问作者Jan Nielsen
相关产品推荐
相关产品推荐

