npm ERR! code ERR_STREAM_WRITE_AFTER_END npm install 报错

实力踩坑npm,自从用了npm之后项目构建和插件管理确实方便了很多,但也是被坑的不要不要的。由于平时只是用用,并没有深入了解node的npm,所以遇到问题都比较棘手,能够查到的资料也不多。阐述以下我遇到的问题,和解决的一些思路,希望能够启发大家。

遇到问题

在学习react应用的时候想用npm安装ant design,结果遇到了error,执行命令和错误如下

E:\workspace\my-react-app>npm install antd --save-dev
npm ERR! code ERR_STREAM_WRITE_AFTER_END
npm ERR! write after end
npm ERR! code ERR_STREAM_WRITE_AFTER_END
npm ERR! write after end
npm ERR! code ERR_STREAM_WRITE_AFTER_END
npm ERR! write after end
npm ERR! code ERR_STREAM_WRITE_AFTER_END
npm ERR! write after end
npm ERR! code ERR_STREAM_WRITE_AFTER_END
npm ERR! write after end


npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2018-05-20T13_15_29_184Z-debug.log、

使用的node版本和npm版本,都是最新版

E:\workspace\my-react-app>npm -v
6.0.1


E:\workspace\my-react-app>node -v
v10.1.0

详细错误日志log文件(2018-05-20T13_15_29_184Z-debug.log)

1626 http fetch GET 200 http://registry.cnpmjs.org/antd 2705ms
1627 verbose stack Error [ERR_STREAM_WRITE_AFTER_END]: write after end
1627 verbose stack     at writeAfterEnd (_stream_writable.js:243:12)
1627 verbose stack     at PassThrough.Writable.write (_stream_writable.js:292:5)
1627 verbose stack     at PassThrough.Writable.end (_stream_writable.js:582:10)
1627 verbose stack     at ReadEntry.entry.on (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\pacote\lib\extract-stream.js:19:41)
1627 verbose stack     at ReadEntry.emit (events.js:187:15)
1627 verbose stack     at ReadEntry.emit (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\node_modules\minipass\index.js:296:25)
1627 verbose stack     at ReadEntry.[maybeEmitEnd] (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\node_modules\minipass\index.js:249:12)
1627 verbose stack     at ReadEntry.end (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\node_modules\minipass\index.js:162:27)
1627 verbose stack     at Unpack.[consumeBody] (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\lib\parse.js:210:13)
1627 verbose stack     at Unpack.[consumeChunkSub] (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\lib\parse.js:391:40)
1627 verbose stack     at Unpack.[consumeChunk] (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\lib\parse.js:362:30)
1627 verbose stack     at Unzip.(anonymous function).on.chunk (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\lib\parse.js:291:59)
1627 verbose stack     at Unzip.emit (events.js:182:13)
1627 verbose stack     at Unzip.emit (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\node_modules\minipass\index.js:296:25)
1627 verbose stack     at Unzip.write (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\node_modules\minipass\index.js:99:17)
1627 verbose stack     at Unzip.write (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\tar\node_modules\minizlib\index.js:284:29)
1628 verbose cwd E:\workspace\my-react-app
1629 verbose Windows_NT 10.0.16299
1630 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "antd" "--save-dev"
1631 verbose node v10.1.0
1632 verbose npm  v6.0.1
1633 error code ERR_STREAM_WRITE_AFTER_END
1634 error write after end
1635 verbose exit [ 1, true ]

尝试的失败方案

  • 卸载重装nodejs,然后尝试npm install,并没有用
  • 开始网上寻求帮助,npm i npm@latest -g,将npm升级到最新版本,结果发现我原来使用的就是最新版本,再次失败。
  • 尝试使用cnpm, 淘宝的registry, npm install -g cnpm --registry=https://registry.npm.taobao.org,结果出现同样的error,连cnpm都不能使用,方案又失败
  • 直接在配置文件.npmrc中,设置registry=https://registry.npm.taobao.org,或者使用官方的registry=http://registry.npm.org/, 也不行
  • 尝试过npm cache clean --force也不管用,甚至前几种方法交替在使用依然不行
  • 在npm的github issue上看到大佬讨论到这个问题,里面也有提交到上面尝试的几种方案——更新啊、换镜像地址啊,结果看到一个新的方案,然后尝试将npm降到[email protected],意外的成功了

最终的解决方法

强制将npm回退到[email protected]的版本

E:\workspace\my-react-app>npm install -g [email protected]
C:\Users\Administrator\AppData\Roaming\npm\npm -> C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
C:\Users\Administrator\AppData\Roaming\npm\npx -> C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\bin\npx-cli.js
+ [email protected]
added 40 packages from 6 contributors, removed 246 packages and updated 125 packages in 113.579s

最后再尝试antd的安装,结果安装成功了,cnpm也安装成功了,花费了大半天的时间。

E:\workspace\my-react-app>npm install antd --save
npm WARN npm npm does not support Node.js v10.1.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9.
npm WARN npm You can find the latest version at https://nodejs.org/
npm WARN [email protected] requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (curre
nt: {"os":"win32","arch":"x64"})


+ [email protected]
added 74 packages in 140.349s


E:\workspace\my-react-app>npm install -g cnpm --registry=https://registry.npm.taobao.org
npm WARN npm npm does not support Node.js v10.1.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9.
npm WARN npm You can find the latest version at https://nodejs.org/
npm WARN deprecated [email protected]: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data f
low and an import issue introduced in 2.1.0
C:\Users\Administrator\AppData\Roaming\npm\cnpm -> C:\Users\Administrator\AppData\Roaming\npm\node_modules\cnpm\bin\cnpm
+ [email protected]
added 802 packages in 436.099s

可以看到antd包安装成功了,尽管它会提示我npm与node的版本不兼容,至少让让我成功的装上了这些包。因为npm是国外的所有有些地址可能访问不到导致错误,使用淘宝镜像有时因为更新不同步也会报错,墙这个问题确实让人很心烦。不过值得庆幸的是,我知道了使用最新版本的工具并不总是最好的,具有一定的不稳定性,毕竟会受到很多外部环境的影响,一旦出错都很难找出原因。所以还是建议大家使用稳定的版本进行开发,功能够用就行。

参考:

https://github.com/npm/npm/issues/19989

https://stackoverflow.com/questions/49148753/error-npm-err-write-after-end?rq=1




你可能感兴趣的:(错误锦集)