Reputation: 1084
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"
]
}
Upvotes: 0
Views: 1081
Reputation: 276343
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 e.g. webpack.
{
"compilerOptions": {
"target": "es6",
"sourceMap": true,
"module": "commonjs"
},
"exclude": [
"node_modules",
"typings"
]
}
Note : There are edge cases here e.g. systemjs / amd support outFile. But that's not most likely not relevant here.
Upvotes: 2