TS2451: Cannot redeclare block-scoped variable 'custom' - node.js

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?

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"
]
}

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"*/]

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

I added this piece of code twice in protractor.conf.js file
require('ts-node').register({
project: require('path').join(__dirname, './tsconfig.e2e.json')
});

Related

Mongoose + Typescript - Unable to require file: mongodb\index.ts

I have a Node + Express application with Typescript, which was running fine, until I decided to include Mongoose in the equation. When I try running it, I get the following error:
TypeError: Unable to require file: mongodb\index.ts
This is usually the result of a faulty configuration or import. Make sure there is a '.js', '.json' or other executable extension with loader attached before 'ts-node' available.
I'm running the application with Nodemon, and have the following configuration in nodemon.json:
{
"execMap": {
"ts": "ts-node"
}
}
Here's my tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution":"node",
"baseUrl": ".",
"target": "es6",
"paths": {
"#controllers/*": ["./controllers/*"],
"#services/*": ["./services/*"],
"#routes/*": ["./routes/*"],
"#customTypes/*": ["./types/*"],
"#utils/*": ["./utils/*"],
"#graphql/*": ["./graphql/*"]
}
}
}
I'm kind of new to Node with Typescript, so I probably made some mistakes, but cannot find any info regarding what exactly is wrong.
Tried downgrading Mongoose, installing MongoDB manually and changed versions of #types/mongoose, but to no avail.

How do I prevent tsc from including browser types in the compliation

I have a script that uses ts-node:
#!/usr/binenv ts-node
const top: number[] = [];
but tsc complains:
top3.ts(3,7): error TS2451: Cannot redeclare block-scoped variable 'top'.
because apparently top is a global variable in browsers.
I've installed #types/node and my tsconfig.json reads:
{
"compilerOptions": {
"noImplicitAny": true,
"target": "es6",
"types": ["node"],
}
}
so I can refer to node builtins like process.
How do I configure tsc so that it does not include browser builtins, but only pure ECMAScript + node.js builtins?

Typescript compiling errors using 'vss-web-extension-sdk' & 'azure-pipelines-task-lib'

I am trying to follow the following guide to make a custom task in azure devops https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops
When I install both 'azure-pipelines-task-lib' & 'vss-web-extension-sdk'
and have a single typescript file that requires the task-lib, like the guide suggests, I get tons of typescript errors.
package.json
"dependencies": {
"azure-pipelines-task-lib": "^3.1.9",
"typescript": "^4.4.3",
"vss-web-extension-sdk": "^5.141.0"
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"rootDir": "src/",
"outDir": "dist/",
"types": [
"vss-web-extension-sdk"
]
},
"files": [
"src/index.ts"
]
}
index.ts
import tl = require('azure-pipelines-task-lib/task');
export function Foo() {
return "BAR"
}
File structure
Root
tsconfig.json
package.json
src
index.ts
dist
index.js
Errors
https://imgur.com/4aNJetK
node_modules/#types/node/module.d.ts:2:5 - error TS2300: Duplicate identifier 'mod'.
2 export = NodeJS.Module;
~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#types/requirejs/index.d.ts:38:14
38 export = mod;
~~~
'mod' was also declared here.
node_modules/#types/requirejs/index.d.ts:38:14 - error TS2300: Duplicate identifier 'mod'.
38 export = mod;
~~~
node_modules/#types/node/module.d.ts:2:5
2 export = NodeJS.Module;
~~~~~~~~~~~~~~~~~~~~~~~
'mod' was also declared here.
node_modules/#types/requirejs/index.d.ts:422:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type
'NodeRequire', but here has type 'Require'.
422 declare var require: Require;
~~~~~~~
node_modules/#types/node/globals.d.ts:213:13
213 declare var require: NodeRequire;
~~~~~~~
'require' was also declared here.
node_modules/vss-web-extension-sdk/typings/vss.d.ts:3168:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.
3168 declare var require: Require;
~~~~~~~
node_modules/#types/node/globals.d.ts:213:13
213 declare var require: NodeRequire;
~~~~~~~
'require' was also declared here.
Can someone provide me with ANY working example of compiling typescript when these libraries are installed?

exports is not defined when running compiled typescript

I am trying to take my first steps into working with typescript and I've run into an issue when trying to run my application.
I get the error ReferenceError: exports is not defined
the code I have is quite simple:
// --src/changeset.ts
export enum ChangeAction {
ADD,
DELETE,
MODIFY
}
export class Changeset {
constructor(
public version: Number,
public content: String,
public path: String,
public action: ChangeAction
) {}
}
// --src/index.ts
import { Changeset, ChangeAction } from "./changeset";
const set = new Changeset(0, "Hello world", "/dev/null", ChangeAction.ADD);
set.version = 0;
console.log("Hello World! " + set.version);
// --tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "build"
},
"include": ["src/**/*"]
}
running tsc, it compiles and seems to work without any real issues, however when I try to run it with node build/index.js it crashes with this
build/index.js:2
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
It feels like I am missing something quite obvious, but I can't really seem to put my finger on it, so what am I missing?
You appear to have enabled Node's ES modules by setting "type": "module" in your package.json, but your tsconfig tells typescript to emit code compatible with CommonJS.
Either remove "type": "module", or configure tsconfig to emit code targeting ES modules.

Starting nestjs in production mode

I am using akveo backend bundle that I bought, and while everything seems to be working fine in development mode starting in production gives me following errors, I am new to nestjs itself.
Anyone know what's going here?
node_modules/#nestjs/core/adapters/http-adapter.d.ts:5:31 - error TS2420: Class 'AbstractHttpAdapter<TServer, TRequest, TResponse>' incorrectly implements interface 'HttpServer<TRequest, TResponse>'.
Property 'status' is missing in type 'AbstractHttpAdapter<TServer, TRequest, TResponse>' but required in type 'HttpServer<TRequest, TResponse>'.
5 export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any, TResponse = any> implements HttpServer<TRequest, TResponse> {
~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/common/interfaces/http/http-server.interface.d.ts:26:5
26 status(response: any, statusCode: number): any;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'status' is declared here.
node_modules/#nestjs/core/application-config.d.ts:2:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
2 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/guards/guards-context-creator.d.ts:3:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
3 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/interceptors/interceptors-context-creator.d.ts:2:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
2 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/nest-application.d.ts:24:5 - error TS2416: Property 'getHttpAdapter' in type 'NestApplication' is not assignable to the same property in base type 'INestApplication'.
Type '() => AbstractHttpAdapter<any, any, any>' is not assignable to type '() => HttpServer<any, any>'.
Property 'status' is missing in type 'AbstractHttpAdapter<any, any, any>' but required in type 'HttpServer<any, any>'.
24 getHttpAdapter(): AbstractHttpAdapter;
~~~~~~~~~~~~~~
node_modules/#nestjs/common/interfaces/http/http-server.interface.d.ts:26:5
26 status(response: any, statusCode: number): any;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'status' is declared here.
Found 5 errors.
I am using tsc -p tsconfig.build.json command to build it with tsconfig.build.json:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "**/*spec.ts"]
}
and tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./"
},
"exclude": ["node_modules"]
}
I expect that this config should compile typescript code to javascript which then I would run with node dist/main.js command. Which it actually does. But I am worried about the typescript compiler errors.
Not sure without the code but got similar mismatches once upon a time in nest core packages. Check out package.json and ensure that all #nest packages have the same version, as sometimes they have 'breaking' changes even if it wasn't major release.
For an abstract example, something like that gave me errors related to core when building production:
"#nestjs/common": "6.6.0", // notice the core/common does not match, it should be bumped
"#nestjs/core": "6.6.2",

Resources