18

I have this in my app with the @types/express dependency installed

import express = require('express');

It is pointing to the express and saying this is an unexpected identifier when I run my server. I believe this is correct TS syntax and the regular JS way of const express = .. has the same error.

Do I need regular express? or wouldn't I need the one I already installed, which should be for TS specifically?

4 Answers 4

24

To replace require statement with import statement, for example:

    const express = require('express');

You can convert it to this:

    import * as express from "express";

And yes, you need both, regular express as dependency and @types/express as dev-dependency to have TypeScript type definitions working.

20

The syntax you want will be

import express from "express";

and it shouldn't result in a duplicate identifier error unless its simply a IDE bug. You can look into a common setup most people use to work with NodeJS/Typescript here.

https://github.com/microsoft/TypeScript-Node-Starter

4
  • 3
    appreciate the answer, it's still giving me that same error when run server.ts
    – mph85
    Commented Jul 4, 2019 at 4:14
  • Hmmm make sure you're building your application with "tsc" and make sure your file extension is ".ts" Commented Jul 4, 2019 at 4:16
  • gotcha, yeah i have a tsconfig.json file generated by tsc --init and also have a server.ts file that has that import statement
    – mph85
    Commented Jul 4, 2019 at 4:18
  • Hmmm.... its tough to say for sure what it is... the fact that it says the regular JS way is also an error makes me suspect it's like an invalid character somewhere in the file above the import or something "this is an unexpected identifier" Commented Jul 4, 2019 at 4:44
0

I had a particularly difficult situation because I was using esmodules with our production/development, but commonjs with our testing tools.

I ended up getting it to work by using both imports.

app.ts

import express, * as express_test from "express"

const app = express ? express() : express_test()
0

this article helped me a bunch, primarily w/r to the module: "commonjs" package.json entry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.