src/polyfills.ts(73,1) 中出现错误时的 web-pack 错误:错误 TS2304:找不到名称“全局”。 [ng] src/polyfills.ts(73,17):

Posted

技术标签:

【中文标题】src/polyfills.ts(73,1) 中出现错误时的 web-pack 错误:错误 TS2304:找不到名称“全局”。 [ng] src/polyfills.ts(73,17):【英文标题】:web-pack error when ERROR in src/polyfills.ts(73,1): error TS2304: Cannot find name 'global'. [ng] src/polyfills.ts(73,17): 【发布时间】:2019-11-29 05:54:45 【问题描述】:

我将 Ionic 3 项目升级为 Ionic 4 项目。使用“ionic serve”启动服务器时,总是出现上述错误。

我尝试了多种方法,例如:

    polyfills.ts 文件中,

    (window as any).process = 
      env:  DEBUG: undefined ,
     ;
    (window as any).global = window;
    (window as any).process = window;
    (window as any).process.browser = true;
    (window as any).process.version = '';
    (window as any).process.versions = node: false;
    global.Buffer = global.Buffer || require('buffer').Buffer;
    

    在头部添加script标签,定义全局等。

我的 tsconfig.json 文件


  "compileOnSave": false,
  "compilerOptions": 
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  

都没有用。

我该如何解决这个错误?

【问题讨论】:

你的 tsconfig 中有节点定义吗? 【参考方案1】:

将此添加到您的 index.html

    <script>
        if (global === undefined) 
            var global = window;
        
    </script>

【讨论】:

【参考方案2】:

tsconfig.json 包含以下代码:


"compileOnSave": false, “编译器选项”: "baseUrl": "./", "outDir": "./dist/out-tsc", “源地图”:是的, “声明”:错误, “模块”:“es2015”, "moduleResolution": "节点", “emitDecoratorMetadata”:真, “experimentalDecorators”:是的, “importHelpers”:是的, “目标”:“es5”, “类型根”:[ “node_modules/@types” ], “库”:[ "es2018", “dom” ]

不确定这是否包括节点定义。

【讨论】:

【参考方案3】:

这里再一次,更具可读性:


  "compileOnSave": false,
  "compilerOptions": 
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  

【讨论】:

以上是关于src/polyfills.ts(73,1) 中出现错误时的 web-pack 错误:错误 TS2304:找不到名称“全局”。 [ng] src/polyfills.ts(73,17):的主要内容,如果未能解决你的问题,请参考以下文章