自定义GitHub npm包安装失败:node_modules未生成包文件夹
GitHub Packages CLI包安装失败:找不到package.json且未创建node_modules文件夹
我正在开发一个CLI包,已发布至GitHub Packages,且.npmrc已配置@myorg:registry=https://npm.pkg.github.com。通过npm pack <package>可查看已发布包的内容,但执行npm install <package>时失败,提示找不到package.json,原因是node_modules中未创建该包的文件夹。
执行npm install --verbose <package>后,发现错误出现前所有请求均成功。这是我创建的第二个包,第一个包可正常安装。
2024-09-17更新:使用SysInternals Process Monitor对比正常包和此包的安装文件系统行为,正常包会创建node_modules文件夹,而此包甚至未尝试创建任何相关文件夹。
相关文件与日志
package.json内容
{ "name": "@myorg/cy-membership-test-client", "bin": { "cy-membership-test-client": "./lib/index.js" }, "scripts": { "build": "tsc", "start": "ts-node ./src/index.ts", "patch-release": "npm version patch && npm publish && git push --follow-tags" }, "repository": { "type": "git", "url": "git+https://github.com/myorg/cy-membership-test-client.git" }, "publishConfig": { "registry": "https://npm.pkg.github.com" }, "owner": "myorg", "author": "Josh Russo", "license": "ISC", "bugs": { "url": "https://github.com/myorg/cy-membership-test-client/issues" }, "homepage": "https://github.com/myorg/cy-membership-test-client#readme", "dependencies": { "@myorg/cy-membership-api-client": "^1.0.104", "@myorg/cy-membership-test-client": "file:", "command-line-args": "^6.0.0", "cy-membership-test-client": "file:", "dotenv": "^16.4.5", "ts-node": "^10.9.2" }, "devDependencies": { "@types/node": "^22.3.0" }, "version": "0.0.3" }
npm install --verbose 日志
npm verb cli C:\Program Files\nodejs\node.exe C:\Users\jrusso\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js npm info using npm@9.2.0 npm info using node@v20.10.0 npm verb title npm install @myorg/cy-membership-test-client npm verb argv "install" "--loglevel" "verbose" "@myorg/cy-membership-test-client" npm verb logfile logs-max:10 dir:C:\Users\jrusso\AppData\Local\npm-cache\_logs\2024-09-16T19_08_22_238Z- npm verb logfile C:\Users\jrusso\AppData\Local\npm-cache\_logs\2024-09-16T19_08_22_238Z-debug-0.log npm verb shrinkwrap failed to load node_modules/.package-lock.json missing from node_modules: node_modules/membership-test-client-install-test npm http fetch GET 200 https://npm.pkg.github.com/@myorg%2fcy-membership-test-client 456ms (cache miss) npm http fetch GET 200 https://registry.npmjs.org/ts-node 165ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/command-line-args 217ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@swc%2fcore 111ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@swc%2fhelpers 90ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@swc%2fwasm 100ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@types%2fnode 83ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/typescript 77ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/typical 112ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/array-back 118ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/undici-types 151ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/find-replace 181ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/arg 181ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/create-require 187ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@cspotcode%2fsource-map-support 199ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@tsconfig%2fnode16 214ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/make-error 241ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/v8-compile-cache-lib 240ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@tsconfig%2fnode14 244ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/acorn 248ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/lodash.camelcase 271ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/acorn-walk 271ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/diff 273ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/yn 284ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@tsconfig%2fnode10 313ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@tsconfig%2fnode12 329ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@jridgewell%2ftrace-mapping 100ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@jridgewell%2fresolve-uri 113ms (cache revalidated) npm http fetch GET 200 https://registry.npmjs.org/@jridgewell%2fsourcemap-codec 118ms (cache revalidated) npm verb stack Error: ENOENT: no such file or directory, open 'C:\temp\my-org\membership-test-client-install-test\node_modules\@myorg\cy-membership-test-client\package.json' npm verb cwd C:\temp\my-org\membership-test-client-install-test npm verb Windows_NT 10.0.22621 npm verb node v20.10.0 npm verb npm v9.2.0 npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\temp\my-org\membership-test-client-install-test\node_modules\@myorg\cy-membership-test-client/package.json npm ERR! errno -4058 npm ERR! enoent ENOENT: no such file or directory, open 'C:\temp\my-org\membership-test-client-install-test\node_modules\@myorg\cy-membership-test-client\package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm verb exit -4058 npm verb unfinished npm timer reify 1726513702302 npm verb unfinished npm timer reify:loadTrees 1726513702303 npm verb code -4058 npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\jrusso\AppData\Local\npm-cache\_logs\2024-09-16T19_08_22_238Z-debug-0.log
npm pack 输出
npm notice npm notice 📦 @myorg/cy-membership-test-client@0.0.4 npm notice === Tarball Contents === npm notice 0B .env npm notice 23B build.ps1 npm notice 2.8kB lib/index.js npm notice 77B lib/messageTest.js npm notice 6.2kB lib/userTests.js npm notice 725B package.json npm notice 80B run.ps1 npm notice 1.6kB src/index.ts npm notice 193B src/messageTest.ts npm notice 5.0kB src/userTests.ts npm notice 12.3kB tsconfig.json npm notice === Tarball Details === npm notice name: @myorg/cy-membership-test-client npm notice version: 0.0.4 npm notice filename: myorg-cy-membership-test-client-0.0.4.tgz npm notice package size: 7.3 kB npm notice unpacked size: 29.0 kB npm notice shasum: 2ef3f1a94b93c8ca347433cc317d3c6dd3ba97e7 npm notice integrity: sha512-wbq6+NtXgZVfK[...]XCDQIWjy5N6mA== npm notice total files: 11 npm notice myorg-cy-membership-test-client-0.0.4.tgz
解决思路与步骤
问题根源
package.json的dependencies中存在两条循环本地依赖:
@myorg/cy-membership-test-client": "file:"cy-membership-test-client": "file:"
发布到GitHub Packages后,其他环境安装时无法找到本地文件,导致npm中断安装流程,甚至无法创建对应包的文件夹。
解决步骤
- 移除循环本地依赖:删除
dependencies中的上述两行配置,修改后的依赖如下:
"dependencies": { "@myorg/cy-membership-api-client": "^1.0.104", "command-line-args": "^6.0.0", "dotenv": "^16.4.5", "ts-node": "^10.9.2" },
- 重新构建发布:执行
npm run build确保编译产物正确,再通过npm version patch更新版本号,最后执行npm publish重新发布包。 - 清除缓存并重新安装:在测试项目中执行
npm cache clean --force清除缓存,再运行npm install @myorg/cy-membership-test-client。
额外检查点
- 确认
.npmignore或files字段配置正确,确保package.json和lib目录的编译产物被包含在发布包中(从npm pack输出看已满足)。 - 可下载GitHub Packages上的包tarball解压,确认内容完整性。
内容的提问来源于stack exchange,提问作者Josh Russo
相关产品推荐
相关产品推荐

