打字稿加载器在编译时抛出多个“重复标识符.."错误 [英] Typescript loader throws multiple 'Duplicate identifier ..' error when compiling

查看:27
本文介绍了打字稿加载器在编译时抛出多个“重复标识符.."错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天将一个项目从我的工作站转移到我的家用电脑上,现在我无法再编译它了.

I moved a project from my workstation to my home pc today and now I can't compile it anymore.

每当我运行webpack"时,我都会收到以下错误:

Whenever I'm running 'webpack', I'm getting the following errors:

TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:9360:13
    TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:9365:11
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:9376:13
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14893:18
    TS2451: Cannot redeclare block-scoped variable 'fetch'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14898:6
    TS2300: Duplicate identifier 'BodyInit'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14919:6
    TS2300: Duplicate identifier 'HeadersInit'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14929:6
    TS2300: Duplicate identifier 'RequestInfo'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:11:13
    TS2451: Cannot redeclare block-scoped variable 'fetch'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:13:14
    TS2300: Duplicate identifier 'HeadersInit'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:14:15
    TS2300: Duplicate identifier 'Headers'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:31:14
    TS2300: Duplicate identifier 'BodyInit'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:43:14
    TS2300: Duplicate identifier 'RequestInfo'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:44:15
    TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:64:11
    TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:70:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'referrerPolicy' must be of type 'string', but here has type 'ReferrerPolicy'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:71:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'mode' must be of type 'string', but here has type 'RequestMode'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:72:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'credentials' must be of type 'string', but here has type 'RequestCredentials'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:73:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'cache' must be of type 'string', but here has type 'RequestCache'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:74:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'redirect' must be of type 'string', but here has type 'RequestRedirect'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:76:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'window' must be of type 'any', but here has type 'null'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:88:15
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:107:11
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] node_modules\@types\whatwg-streams\index.d.ts:32:15
    TS2300: Duplicate identifier 'ReadableStream'.

我删除并重新安装了所有 node_modules(和类型),尝试在本地和全局安装 Typescript,并从 ts-loader 切换到 awesome-typescript-loader(这至少解决了我遇到的一些其他错误).

I've removed and re-installed all node_modules (and types), tried installing Typescript locally and globally and switched from ts-loader to awesome-typescript-loader (which at least solved some other errors I had).

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "app",
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es6",
        "jsx": "preserve",
        "noImplicitAny": false,
        "sourceMap": true,
        "lib": [
            "es6",
            "dom"
        ],
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    },
    "exclude": [
        "node_modules"
    ]
}

webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const { CheckerPlugin } = require('awesome-typescript-loader')
var TsConfigPathsPlugin = require('awesome-typescript-loader').TsConfigPathsPlugin;

module.exports = {
    entry: {
        'vendor': [
            'react',
            'react-dom',
            'react-router',
            'react-bootstrap',
            'react-router-bootstrap',
            'react-datetime'
        ],
        'client': [
            'webpack-dev-server/client?http://0.0.0.0:8080', // WebpackDevServer host and port
            'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
            './app/boot-client.tsx'
        ],
    },
    output: {
        filename: '[name].bundle.js',
        path: './app/',
    },
    resolve: {
        plugins: [
            new TsConfigPathsPlugin('./tsconfig.json')
        ],
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.scss']
    },
    module: {
        loaders: [
            {
                test: /\.tsx?$/,
                include: /app/,
                loaders: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.tsx?$/,
                include: /app/,
                loaders: 'awesome-typescript-loader'
            },
            {
                test: /\.scss$/,
                loaders: ['style-loader', 'css-loader', 'sass-loader']
            }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin('vendor'),
        new HtmlWebpackPlugin({
            inject: 'body',
            template: 'app/index_template.html',
            filename: 'index.html'
        }),
        new webpack.HotModuleReplacementPlugin(),
        new CheckerPlugin()
    ],
    devServer: {
        contentBase: "./app",
        port: 8080
    },
};

不幸的是,我在这方面不是很有经验,所以我在这里缺少什么?

Unfortunately I'm not very experienced in this area, so what am I missing here?

推荐答案

错误在 [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:11:13

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:11:13

TypeScript 最新版本附带了开箱即用的 fetch 定义.所以卸载whatwg-fetch定义:

TypeScript latest ships with fetch definitions out of the box. So uninstall whatwg-fetch definitions:

npm uninstall @types/whatwg-fetch

这篇关于打字稿加载器在编译时抛出多个“重复标识符.."错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆