天天看點

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

繼續閱讀