David Long
David Long

Reputation: 75

TS2451: Cannot redeclare block-scoped variable 'custom'

I'm using typescript in a web project. I use awesome-typescript-loader as a webpack loader. I am getting error when building my project:

ERROR in [at-loader] ./node_modules/@types/node/index.d.ts:82:15 
    TS2451: Cannot redeclare block-scoped variable 'custom'.
ERROR in [at-loader] ./node_modules/@types/node/index.d.ts:85:15 
    TS2451: Cannot redeclare block-scoped variable 'custom'.
ERROR in [at-loader] ./node_modules/@types/node/ts3.2/util.d.ts:7:15 
    TS2451: Cannot redeclare block-scoped variable 'custom'.
ERROR in [at-loader] ./node_modules/@types/node/ts3.2/util.d.ts:10:15 
    TS2451: Cannot redeclare block-scoped variable 'custom'.

I initiated a complete new folder with just typescript and @types/typescript installed, I can still see the same error complained by visual studio code.

Versions below:

  "dependencies": {
    "@types/node": "^11.13.6",
    "typescript": "^3.4.4"
  }

As the error showed above, I found

in index.d.ts:

declare module "util" {
    namespace inspect {
        const custom: symbol;
    }
    namespace promisify {
        const custom: symbol;
    }
    namespace types {
        function isBigInt64Array(value: any): boolean;
        function isBigUint64Array(value: any): boolean;
    }
}

in util.d.ts:

declare module "util" {
    namespace inspect {
        const custom: unique symbol;
    }
    namespace promisify {
        const custom: unique symbol;
    }
    namespace types {
        function isBigInt64Array(value: any): value is BigInt64Array;
        function isBigUint64Array(value: any): value is BigUint64Array;
    }
}

We can see custom is indeed being re-declared in index.d.ts and util.d.ts.

So my question is how to fix this issue? Is this a bug of @types/node?

Upvotes: 4

Views: 13555

Answers (4)

Partha
Partha

Reputation: 106

I added this piece of code twice in protractor.conf.js file

require('ts-node').register({
    project: require('path').join(__dirname, './tsconfig.e2e.json')
 });

Upvotes: 0

Tomas
Tomas

Reputation: 379

I had the same issue, but the error message was misleading just because of something wrong cached. I tried many things, but finally solved it by removing npm_modules, clearing cache, and new installation:

npm cache clean --force 
npm install

Upvotes: 0

Bruce Lee
Bruce Lee

Reputation: 4441

My problem is that I accidentally included node_modules/@types. Fixed by comment it out like this:

"include": ["src/**/*.ts","__tests__/**/*.ts"/*, "node_modules/@types"*/]

Upvotes: 0

Shashank Raj
Shashank Raj

Reputation: 106

I was facing the same issue. Removing reference to node in tsconfig fixed the issue for me.

Sample tsconfig.

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true
  },
  "files": [
     // "./node_modules/@types/node/index.d.ts",
    "./node_modules/@types/express/index.d.ts"
  ],
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "lib": [
    "es2017"
  ]
}

Upvotes: 6

Related Questions