天天看点

ReferenceError: self is not defined

使用webpack打包库文件后,在node端执行引入报错

ReferenceError: self is not defined      

解决办法:

module.exports = {
  output: {
    globalObject: 'this'
  }
}      

完整配置

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'more-echo.js',
    globalObject: 'this',
    library: {
      name: 'moreEcho',
      type: 'umd',
    },
  },
};      

依赖版本

{
  "devDependencies": {
    "webpack": "^5.72.1",
    "webpack-cli": "^4.9.2"
  }
}      
参考
  1. https://github.com/webpack/webpack/issues/6784
  2. https://webpack.js.org/configuration/output/#outputglobalobject

继续阅读