就是Webpack打包时把部分NodeJs核心库也打包进生成的js中。
Webpack官方描述可以这样做:Webpack中文文档/node
官方说可以在webpack.config.js中加入
//官方示例
node: {
console: false,
global: true,
process: true,
__filename: "mock",
__dirname: "mock",
Buffer: true,
setImmediate: true
// 更多选项,请查看“其他 Node.js 核心库”
}
这些选项可以配置是否 polyfill 或 mock 某些 Node.js 全局变量和模块。这可以使最初为 Node.js 环境编写的代码,在其他环境(如浏览器)中运行。
可是实际上当前Webpack5.42.0只允许
//我的webpack.config.js中
node: {
global: true,
__dirname: "mock",
__filename: "mock",
}
当我向上面我的代码中加入
assert:true,
util:true,
net:"mock",
tls:"mock",
string_decoder:true,
Buffer:true
时WebStorm 提示 webpack: Property 'Buffer' is not allowed (和Buffer一样其他属性也报错)。
但是没有上面这些,在执行webpack --config webpack.config.js后报错:
ERROR in ./node_modules/redis-errors/lib/modern.js 3:15-32
Module not found: Error: Can't resolve 'assert' in 'D:...(我的项目路径)\node_modules\redis-errors\lib'
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: { "assert": require.resolve("assert/") }'
- install 'assert'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "assert": false }
第一次用Webpack,甚至NodeJS也才用一个星期,如果说是Webpack版本问题请教一下我怎么换用低版本后怎么操作......或者当前版本下怎么操作才能解决问题
我就是想让一个NodeJs的redis客户端打包后在网页上跑起来罢了
下面是我的完整的webpack.config.js代码
const webpack=require('webpack')
const path = require('path')
function resolve(relatedPath) {
return path.join(__dirname, relatedPath)
}
module.exports = {
entry: {
index: resolve('../untitled1/index.js')
},
output: {
path: resolve('../untitled1/dist'),
filename: 'MyRedisClient.js',
},
target: "web",
node:{
global: true,
__dirname: "mock",
__filename: "mock",
},
mode:"development"
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。
我也有这个问题 ,解决了吗