【发布时间】:2019-04-16 05:55:19
【问题描述】:
我创建了一个 npm 项目,我在其中使用 typescript 配置 (tsconfig.json)。
在一个 .ts 文件中,我有如下代码:
export = function loginToLCSteps() {
this.setDefaultTimeout(30 * 1000);
let stage = serenity.callToStageFor({
actor: (name) => Actor.named('James').whoCan(BrowseTheWeb.using(protractor.browser))
});
this.Given(/^that Doctor is on Login screen$/, function () {
return stage.theActorCalled('James').attemptsTo(
AccessLifecare.called()
);
});
this.When(/^he enters credentials and click on Login button$/, function () {
return stage.theActorCalled('James').attemptsTo(
Login.called()
);
});
this.Then(/^he should be navigate to Role and Care Unit selection screen$/, function () {
return stage.theActorCalled('James').attemptsTo(
Wait.until(SelectRoleAndUnitPage.selectRoleAndUnitLabel, Is.visible()),
);
});
};
tsconfig.json 的模块系统为“commonjs”。
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "commonjs",
"moduleResolution": "node"
.....
以上代码适用于“commonjs”模块加载器。
但是,我希望模块加载器是“es2015”。
将模块系统更改为“es2015”,在 export = function loginToLCSteps() 行的 Visual Studio Code 中出现以下错误:
以 ECMAScript 模块为目标时,不能使用导出分配。考虑改用“导出默认值”或其他模块格式。 ts(1203)
上面的代码可以在我的几台机器上运行。
我安装了最新版本的 node、npm、Typescript 但没有成功。
【问题讨论】:
-
"考虑使用 'export default' 或其他模块格式。" 非常正确。
-
使用'export default'开始给我不同的运行时错误=>“意外的令牌{”。该 .ts 文件之上的导入很少。
-
您究竟是如何尝试使用它的?你将它转译成什么,当你得到错误时你在什么环境下运行它?
标签: typescript ecmascript-6 commonjs