求助:执行npm start时出现stream模块未找到的Node模块错误
解决Webpack 5中
stream模块找不到的错误 执行npm start时触发以下模块解析错误:
ERROR in ../../node_modules/socks/build/common/helpers.js 9:15-32 Module not found: Error: Can't resolve 'stream' in 'D:\Full stack web developer\node_modules\socks\build\common' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it. If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }' - install 'stream-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "stream": false }
解决方案
根据是否需要stream模块的功能,选择以下任意一种配置:
1. 引入stream的polyfill(需要该模块功能时)
- 首先安装依赖包:
npm install stream-browserify --save-dev
- 修改webpack配置文件(如
webpack.config.js),添加resolve.fallback配置:
module.exports = { // 保留原有其他配置 resolve: { fallback: { "stream": require.resolve("stream-browserify") } } };
2. 禁用stream模块的解析(不需要该模块功能时)
直接修改webpack配置文件,将stream的fallback设为false:
module.exports = { // 保留原有其他配置 resolve: { fallback: { "stream": false } } };
原因说明
Webpack 5移除了对Node.js核心模块默认polyfill的支持,所有这类模块都需要开发者手动决定是否引入对应的浏览器兼容实现,或是直接禁用解析。
内容的提问来源于stack exchange,提问作者Rajesh Kumar
相关产品推荐
相关产品推荐

