149

I am using TypeScript with Express/Node.js.

For consuming modules, the TypeScript Handbook shows the following syntax:

import express = require('express');

But also the typescript.d.ts file shows:

import * as express from "express";

I also searched the MSDN blog but could not find anything.

Which one is more correct as of early 2016? What are the differences between the two, if any?

Where is the best source to find information on the latest syntax to use so I can find this information in the future?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Adam Thompson
  • 3,278
  • 3
  • 22
  • 37

2 Answers2

202

These are mostly equivalent, but import * has some restrictions that import ... = require doesn't.

import * as creates an identifier that is a module object, emphasis on object. According to the ES6 spec, this object is never callable or newable - it only has properties. If you're trying to import a function or class, you should use

import express = require('express');

or (depending on your module loader)

import express from 'express';

Attempting to use import * as express and then invoking express() is always illegal according to the ES6 spec. In some runtime+transpilation environments this might happen to work anyway, but it might break at any point in the future without warning, which will make you sad.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • 7
    “In general you should use the ES6 syntax unless you have some reason not to (I can't think of any offhand)” FWIW I [gave an answer why not](http://stackoverflow.com/a/29598404/252087) in [this duplicate question here](http://stackoverflow.com/questions/29596714/new-es6-syntax-for-importing-commonjs-amd-modules-i-e-import-foo-require). – C Snover Mar 01 '16 at 22:18
  • I'm a little confused between the apparent contradiction here. Which advice should I be following? @CSnover is your advice still just as applicable today in 2016 as it was when you posted it? Thank you. – Adam Thompson Mar 02 '16 at 19:09
  • 1
    @AdamThompson yes, see the part “2016 update”. – C Snover Mar 02 '16 at 19:12
  • In the source code of visual studio, I found they write `import express = require('express');` – cwtuan Aug 31 '17 at 03:14
  • 3
    @Ryan Cavanaugh What does "depending on your module loader" mean? How do I find out what module loader I have? Thanks. – Old Geezer Sep 12 '17 at 05:27
  • That's fine. But how do I instruct the TypeScript transpiler to output an import instead of a require()? – Thomas Weller Dec 16 '22 at 19:45
35
import * as express from "express";

This is the suggested way of doing it because it is the standard for JavaScript (ES6/2015) since last year.

In any case, in your tsconfig.json file, you should target the module option to commonjs which is the format supported by nodejs.

thitemple
  • 5,833
  • 4
  • 42
  • 67
  • 8
    but this means `express` should be an object of exports and would not make sense to be called as `express()` – Emobe Mar 16 '19 at 11:51
  • 1
    @Emobe I found that to be true only if your ts.config has "esModuleInterop": true if that flag is set to false, express() seems to be valid code – Sam Aug 14 '19 at 23:59
  • @Sam Valid but not clean. It does not make sense to use it like that. – Emobe Aug 15 '19 at 19:48