TypeScript外部节点模块有时会传送到module.exports和exports
我正在将我的节点应用程序转换为使用TypeScript外部模块的过程。 运行应用程序时一切正常,但转换我的一些.ts文件,由于SyntaxError: Unexpected reserved word
摩卡testing“爆炸” SyntaxError: Unexpected reserved word
。
经过多次debugging,我发现以下可重现的故障情况。 我有一个简单的autoRoles.ts文件,它定义了可用的用户angular色。 在使用外部模块之前,它看起来像:
/// <reference path="../../typings/backend_typings.d.ts" /> module.exports.roles = { // role definitions }
现在转换之后:
/// <reference path="../../typings/backend_typings.d.ts" /> export let roles = { // role definitions }
运行摩卡testing时,会产生以下错误:
>> Mocha exploded! >> SyntaxError: Unexpected reserved word >> at exports.runInThisContext (vm.js:53:16) >> at Module._compile (module.js:413:25) >> at Object.Module._extensions..js (module.js:452:10) >> at Module.load (module.js:355:32) >> at Function.Module._load (module.js:310:12) >> at Module.require (module.js:365:17) >> at require (module.js:384:17) >> at Object.<anonymous> (/Users/abc/esupport/code/asp_v4/lib/models/userRole.ts:77:17) >> at Module._compile (module.js:434:26) >> at Object.Module._extensions..js (module.js:452:10) >> at Module.load (module.js:355:32) >> at Function.Module._load (module.js:310:12) >> at Module.require (module.js:365:17) >> at require (module.js:384:17) >> at Object.<anonymous> (/Users/abc/esupport/code/asp_v4/lib/users/lib/ssoAuth.ts:7:17) >> at Module._compile (module.js:434:26) >> at Object.Module._extensions..js (module.js:452:10) >> at Module.load (module.js:355:32) >> at Function.Module._load (module.js:310:12) >> at Module.require (module.js:365:17) >> at require (module.js:384:17) >> at Object.<anonymous> (/Users/abc/esupport/code/asp_v4/lib/users/index.ts:5:31)
我可以在旧实现和autoRoles.ts文件的新实现之间进行切换,并分别让摩卡通过和下降。 请注意, require('<path>/autoRoles')
第77行有一个require('<path>/autoRoles')
。
在比较两个版本之间的区别时,唯一的区别是旧版本使用“module.exports”,而新版本只有“exports”。
旧:
/// <reference path="../../typings/backend_typings.d.ts" /> exports.roles = { // role definitions }
新:
/// <reference path="../../typings/backend_typings.d.ts" /> module.exports.roles = { // role definitions }
所以我知道“exports”只是“module.exports”的一个快捷方式,所以我不能解释为什么这会导致mocha失败,但我知道如果我在两者之间切换而不改变别的,mocha“爆炸”。 我也注意到,对于其他的模块,tsc有时使用“module.exports”,有时使用“exports”。 为什么差异更重要呢,为什么摩卡爆炸?
意外的保留字
有"use strict";
在你的文件的顶部。 您可能有一个保留关键字的variables。 请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Paving_the_way_for_future_ECMAScript_versions 。 如果文件中有这个头文件,TypeScript将会在这样的variables名称上发出警告。
module.exports.roles = {
不是你的错误的来源。
我也注意到,对于其他的模块,tsc有时使用“module.exports”,有时使用“exports”。
它类似于nodejs约定。 基本上保存运行时需要parsing的字符(字节)。
export let foo = 123;
会给你
exports.foo = 123;
(因为exports == module.export
因此exports == module.export
exports.foo == module.export.foo
…就像你已经知道的那样)。 但在:
let foo = 123; export = foo;
它确实
var foo = 123; module.exports = foo;
因为如果你重新分配出口,即exports = foo
然后module.export !== exports
。 所以你可以使用exports
扩展….但不分配 。
经过更多的debugging之后,我发现摩卡没有使用生成的.js源代码文件进行传输。 我不确定如何,但它试图执行位于.ts文件中的“导出varangular色”,“导出”是一个保留字。
我遇到了这个post ,向我表示,摩卡正在试图做自己的传译。 那家伙build议使用“打字稿 – 要求”,但该包看起来像被弃用的中间,而赞成“TS节点”。 所以我改变了我的grunt-tsconfiguration看起来像:
mochaTest: { test: { options: { reporter: 'spec', require: [ 'ts-node/register' ] }, src: ['lib/test/**/*.spec.js'] } },
这样做很有效,但是我会很乐意让人们了解一下摩卡在做什么。 另外,为什么mocha成功地在其他使用导出的.ts文件中检测/不检测“保留字”?
编辑10/30/2015:
所以我发现了摩卡为什么试图执行我的.ts文件。 我愚蠢地导入其中一些作为要求('/path/ file.ts'),我应该离开'.ts'扩展。 我的摩卡跑者不再需要'ts-node'了。 这也解释了为什么摩卡只是在我的.ts文件的一些错误。