I want to import 'express' module to my app.
as Mozilla says, we can use this code write below:
import { Application }, * as Express from 'express'
but when I use it in typescript and VSCode it shows error so I forced to use this code:
import * as Express from 'express'
import { Application } from 'express'
how can I solve the problem?
I want to import 'express' module to my app.
as Mozilla says, we can use this code write below:
import { Application }, * as Express from 'express'
but when I use it in typescript and VSCode it shows error so I forced to use this code:
import * as Express from 'express'
import { Application } from 'express'
how can I solve the problem?
Share asked Jul 21, 2017 at 7:19 Hossain KhademianHossain Khademian 1,7064 gold badges19 silver badges29 bronze badges 1- What happens if you rearrange it? * as Express should be first. – Win Commented Jul 21, 2017 at 7:35
2 Answers
Reset to default 5Your code :
import { Application }, * as Express from 'express'
Is wrong. Fortunately you are using TypeScript and its preventing a bug. The correct syntax is:
import * as Express from 'express'
import { Application } from 'express'
Which you have already figured out. You cannot have member imports and *
imports in the same line. The MDN docs also reflect that https://developer.mozilla/en/docs/web/javascript/reference/statements/import
If I have interpreted TypeScript correctly then importing modules using:
import * as Express from 'express'
Will create a Namespace that you can reference all of the different methods/ponents inside using syntax such as:
Express.Application
Imports with
import { Application } from 'express'
Will instead only import the Application member of Express, which then is referensed as its own class.
If you don't need everything from Express, then the first import is unnecessary, you can instead chain your dependencies with
import { Application, 'Member1', 'Member2' } from 'express'