npm prune删除已使用的包,使构建损坏。有什么选择?

时间:2016-05-03 10:00:28

标签: node.js npm

我注意到节点5/6中的奇怪行为或npm prune与npm 3.8.6:它删除了已使用的包并使构建中断。

这是dep.list:

  "dependencies": {
    "browser-sync": "^2.11.1",
    "browserify": "^13.0.0",
    "coffee-script": "^1.9.3",
    "coffeeify": "^2.0.1",
    "cson": "^3.0.2",
    "fastclick": "^1.0.6",
    "gulp": "github:gulpjs/gulp#4.0",
    "gulp-git": "^1.7.0",
    "gulp-uglify": "^1.2.0",
    "gulp-util": "^3.0.6",
    "howler": "github:goldfire/howler.js#2.0",
    "minimatch": "^3.0.0",
    "nouislider": "^8.2.1",
    "parcelify": "^2.1.0",
    "q": "^1.4.1",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.7.0"
  },

全新安装的结果:

┌(~/Sites/test_client)─(16 files, 280b)
└> nvm use v5
Now using node v5.11.0 (npm v3.8.6)

┌(~/Sites/test_client)─(16 files, 280b)
└> npm cache clean

┌(~/Sites/test_client)─(16 files, 280b)
└> rm -rf node_modules/

┌(~/Sites/test_client)─(15 files, 280b)
└> npm i

> fsevents@1.0.12 install /Users/metalim/Sites/test_client/node_modules/fsevents
> node-pre-gyp install --fallback-to-build

[fsevents] Success: "/Users/metalim/Sites/test_client/node_modules/fsevents/lib/binding/Release/node-v47-darwin-x64/fse.node" is installed via remote
test_client@1.0.0 /Users/metalim/Sites/test_client
├── browser-sync@2.12.5 
├── browserify@13.0.0 
├── coffee-script@1.10.0 
├── coffeeify@2.0.1 
├── cson@3.0.2 
├── fastclick@1.0.6 
├── gulp@4.0.0-alpha.2  (git://github.com/gulpjs/gulp.git#b4d89c391b40cd29e8526e0391981eef67cfccab)
├── gulp-git@1.7.1 
├── gulp-uglify@1.5.3 
├── gulp-util@3.0.7 
├── howler@2.0.0-beta12  (git://github.com/goldfire/howler.js.git#db831e03cf696d1ca5cfdd8f87d66e66da2c5029)
├── minimatch@3.0.0 
├── nouislider@8.5.1 
├── parcelify@2.1.0 
├── q@1.4.1 
├── vinyl-buffer@1.0.0 
├── vinyl-source-stream@1.1.0 
└── watchify@3.7.0 

紧接着:

┌(~/Sites/test_client)─(16 files, 280b)
└> npm prune
unbuild archy@1.0.0
unbuild align-text@0.1.4
unbuild center-align@0.1.3
unbuild findup-sync@0.3.0
unbuild flagged-respawn@0.3.2
unbuild formidable@1.0.17
unbuild interpret@1.0.1
unbuild lazy-cache@1.0.4
unbuild liftoff@2.2.1
unbuild lodash._getnative@3.9.1
unbuild lodash.isarguments@3.0.8
unbuild lodash.isarray@3.0.4
unbuild lodash.sortby@4.4.2
unbuild longest@1.0.1
unbuild mute-stdout@1.0.0
unbuild matchdep@1.0.1
unbuild lru-cache@2.7.3
unbuild os-homedir@1.0.1
unbuild pretty-hrtime@1.0.2
unbuild rechoir@0.6.2
unbuild right-align@0.1.3
unbuild semver-greatest-satisfied-range@1.0.0
unbuild semver-regex@1.0.0
unbuild sigmund@1.0.1
unbuild stack-trace@0.0.9
unbuild tildify@1.2.0
unbuild v8flags@2.0.11
unbuild wordwrap@0.0.2
unbuild wreck@6.3.0
unbuild user-home@1.1.1

构建破了。怎么了?是npm bug吗?

经过一些测试后,我发现npm prune仅在v5(npm v3)之前的节点版本中按预期工作:在v4.4.3(npm v2.15.1)及以下版本中。基本上它不适用于npm v3中引入的平面依赖列表。

什么是npm prune替代平面依赖列表来清理未使用的模块?

1 个答案:

答案 0 :(得分:3)

回答自己。

问题出现在npm confignpm prune对--depth选项的意外依赖关系中。 如果你npm config set depth 0清除npm ls命令的输出,npm prune会在npm v3 +中被破坏。它似乎考虑了--depth选项,并且在平面依赖列表上失败了。

所以,问题的解决方法是: npm config delete depth

之后npm prune工作正常。