Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Cannot find namespace

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"],
}
like image 275
CaribouCode Avatar asked May 28 '19 11:05

CaribouCode


People also ask

Can not find namespace TypeScript?

To solve the "Cannot find namespace context" error in React typescript, use a . tsx extension for the files in which you use JSX, set jsx to react-jsx in your tsconfig. json file and make sure to install all of the necessary @types packages for your application.

Does TypeScript have namespace?

In TypeScript, you can use namespaces to organize your code. Previously known as internal modules, namespaces in TypeScript are based on an early draft of the ECMAScript modules.

How do I create a namespace in TypeScript?

We can create a namespace by using the namespace keyword followed by the namespace_name. All the interfaces, classes, functions, and variables can be defined in the curly braces{} by using the export keyword. The export keyword makes each component accessible to outside the namespaces.

How do I refer a namespace in a file student ts?

To use it, it must be included using triple slash reference syntax e.g. ///<reference path="path to namespace file" /> . Must import it first in order to use it elsewhere. Compile using the --outFile command. Compile using the --module command.


3 Answers

In case you are a React dev that ends up here as I did - if there is JSX syntax in the file, try changing the extension of the file from .ts to .tsx.

like image 108
a2f0 Avatar answered Sep 29 '22 17:09

a2f0


After a lot of wasted time, it turns out this was due to the module setting in the tsconfig file which should be:

"module": "commonjs"

This means Typescript will output common js modules instead of ES6 modules, meaning the code will run correctly as NodeJS code. I was therefore able to change require to import, since it compiles.

like image 33
CaribouCode Avatar answered Oct 02 '22 17:10

CaribouCode


Maybe you have to use import instead of require:

import * as express from 'express';
like image 24
Darwin Gonzalez Avatar answered Oct 01 '22 17:10

Darwin Gonzalez