简体   繁体   English

在Typescript中导入ES6样式

[英]ES6 Style Import in Typescript

I would like to import an external module from node_modules in node using typescript using the import syntax. 我想使用导入脚本使用typescript从node中的node_modules导入一个外部模块。 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). 我已经将类型定义添加到我的项目中,因此我可以调用install()而不会导致Typescript返回错误(我知道我可以执行any强制转换要求,但如果可能的话,我显然想保留所有类型)。

/// <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? 有人可以向我解释为什么它在生成的js文件中不输出需求吗? Also I'm compiling to ES6 as I need a access to the async & await keywords - my tsconfig.json is as follows. 另外我正在编译到ES6,因为我需要访问async&await关键字-我的tsconfig.json如下。

{
   "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 有人可以向我解释为什么它在生成的js文件中不输出需求

Because you are using outFile in tsconfig "outFile": "build/server.js" . 因为您在tsconfig "outFile": "build/server.js"中使用outFile Don't use outFile if you are using a module format. 如果使用模块格式,请不要使用outFile That is a job a bundler eg webpack. 那是捆绑器(例如webpack)的工作。

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

PS 聚苯乙烯

Note : There are edge cases here eg systemjs / amd support outFile. 注意:这里有一些极端的情况,例如systemjs / amd支持outFile。 But that's not most likely not relevant here. 但这并不是最不相关的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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