gpt4 book ai didi

Typescript + Webpack 库生成 "ReferenceError: self is not defined"

转载 作者:行者123 更新时间:2023-12-03 13:45:19 25 4
gpt4 key购买 nike

我正在构建一个与 Webpack 5.3.2 捆绑在一起的 TS 库。然后我尝试加载输出包,dist/scalextric.js , 变成 node.js脚本:

node -e 'console.dir(Object.keys(require("./dist/scalextric.js")));'
我得到:
ReferenceError: self is not defined
at Object.<anonymous> (/media/kratib/Data/src/infojunkie/scalextric/dist/scalextric.js:2:215)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at [eval]:1:25
at Script.runInThisContext (vm.js:132:18)
at Object.runInThisContext (vm.js:309:38)
我需要帮助修复此错误。这是我的各种配置:
  • webpack.config.js :
  • const path = require('path');

    module.exports = {
    entry: './src/index.ts',
    devtool: 'inline-source-map',
    module: {
    rules: [
    {
    test: /\.tsx?$/,
    use: 'ts-loader',
    exclude: /node_modules/,
    },
    ],
    },
    resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
    },
    output: {
    filename: 'scalextric.js',
    library: 'Scalextric',
    libraryTarget: 'umd',
    path: path.resolve(__dirname, 'dist'),
    umdNamedDefine: true,
    },
    };
  • tsconfig.json :
  • {
    "compilerOptions": {
    "module": "commonjs",
    "lib": ["dom", "esnext.asynciterable"],
    "target": "es6",
    "moduleResolution": "node",
    "outDir": "./dist",
    "sourceMap": true,
    "declaration": true,
    "importHelpers": true
    },
    "include": [
    "src"
    ]
    }
  • index.ts :
  • export * from './Tuning';
    export * from './TuningNotation';
    export * from './Interval';
    我犯了什么明显的错误? Here's the full code .

    最佳答案

    此问题与选项 output.globalObject 有关其中是described here .
    我相信 webpack已将其设置为 self出于某种原因。但是为了在浏览器和节点上工作,你可以简单地切换到 this :

    output: {
    // ...
    globalObject: 'this',
    },

    关于Typescript + Webpack 库生成 "ReferenceError: self is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64639839/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com