I have the following simple NodeJS code:
const express = require('express');
const server: express.Application = express();
I'm adding Typescript to my project and am new to it so forgive me. With the above code I get the following issues/errors:
For the require:
var require: NodeRequire (id: string) => any (+1 overload)
'require' call may be converted to an import.
For the express.Application usage:
Cannot find namespace 'express'.
If I switch the 'require' to 'import' it fixes the namespace error but is no longer valid Node code so doesn't run (throws a new error about unexpected token for the import).
What's the correct way to write Node code like this with Typescript to avoid these errors?
My tsconfig.json looks like this:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "esnext",
},
"exclude": ["node_modules"],
}
tsconfig.json?declare function require(name:string);at the top. Typescript doesnt support require by default