简体   繁体   中英

ES6 Style Import in Typescript

I would like to import an external module from node_modules in node using typescript using the import syntax. I've added the type definitions to my project so I can call install() without typescript returning errors (I know I can do an any cast require but I would obviously like to keep everything typed if possible).

/// <reference path="typings/source-map-support/source-map-support.d.ts" />

import * as sourceMapSupport from 'source-map-support';
sourceMapSupport.install();

However when my code is output it returns the following:

/// <reference path="../typings/source-map-support/source-map-support.d.ts" />
//# sourceMappingURL=server.js.map

Could someone explain to me why it doesn't output a require in the resulting js file? Also I'm compiling to ES6 as I need a access to the async & await keywords - my tsconfig.json is as follows.

{
   "compilerOptions": {
   "target": "es6",
   "sourceMap": true,
   "outFile": "build/server.js"
},
   "exclude": [
       "node_modules",
       "typings"
   ]
}

Could someone explain to me why it doesn't output a require in the resulting js file

Because you are using outFile in tsconfig "outFile": "build/server.js" . Don't use outFile if you are using a module format. That is a job a bundler eg webpack.

{
   "compilerOptions": {
   "target": "es6",
   "sourceMap": true,
   "module": "commonjs"
},
   "exclude": [
       "node_modules",
       "typings"
   ]
}

PS

Note : There are edge cases here eg systemjs / amd support outFile. But that's not most likely not relevant here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM