Material-UI + TypeScript提供ReferenceError:未定义global

问题描述 投票:1回答:1

我已经执行了

npm install --save react react-dom @material-ui/core
npm install --save-dev webpack webpack-cli typescript ts-loader @types/react @types/react-dom

和转换main.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";
import Button from '@material-ui/core/Button';

window.onload = () => { ReactDOM.render(<Button />, document.getElementById("app")) };

这个文件已成功编译,但我在node_modules/jss/lib/utils/escape.js:6中有一个ReferenceError

var CSS = global.CSS; // ReferenceError: global is not defined

如何抑制此错误?

这是我的webpack.config.js

module.exports = {
    mode: "development",
    entry: __dirname + "/src/main.tsx",
    output: {
        path: __dirname + "/www",
        filename: "bundle.js",
    },
    devtool: "source-map",
    module: {
        rules: [ {test: /\.tsx?$/, use: "ts-loader"} ]
    },
    resolve: {
        extensions: [".ts", ".tsx", ".js"]
    },
    target: "node"
};
reactjs typescript global material-ui referenceerror
1个回答
1
投票

你有target: "node"

像global和require这样的全局变量是由环境提供的。除非另有说明,否则Webpack假定浏览器环境并重写全局以指向窗口。

您可以从配置中删除target:'node',也可以通过将node:{global:true}添加到配置对象来显式启用全局重写。

© www.soinside.com 2019 - 2024. All rights reserved.