gpt4 book ai didi

node.js - TypeScript 外部 Node 模块有时会转换为 module.exports 和导出

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:20 24 4
gpt4 key购买 nike

我正在将我的 Node 应用程序转换为使用 TypeScript 外部模块。运行应用程序时一切正常,但在转换我的一些 .ts 文件时,摩卡测试由于SyntaxError: Unexpected reserved word而“爆炸”。 .

经过多次调试,我发现了以下可重现的失败案例。我有一个简单的 autoRoles.ts 文件,它定义了可用的用户角色。在使用外部模块之前,它看起来像:

/// <reference path="../../typings/backend_typings.d.ts" />

module.exports.roles = {
// role definitions
}

现在转换后:

/// <reference path="../../typings/backend_typings.d.ts" />

export let roles = {
// role definitions
}

运行摩卡测试时,会生成以下错误:

>> 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 文件的旧实现和新实现之间切换,并分别让 mocha 通过和下降。注意,有一个require('<path>/autoRoles')在 userRoles.ts 第 77 行。

比较转译版本时,唯一的区别是旧版本使用“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”的快捷方式,所以我无法解释为什么这会导致摩卡失败,但我确实知道如果我在两者之间切换并且不改变其他任何内容,摩卡就会“爆炸”。我还注意到,对于其他转译模块,tsc 有时使用“module.exports”,有时使用“exports”。为什么会有这种差异,更重要的是,为什么摩卡会爆炸?

最佳答案

Unexpected reserved word

在文件顶部添加"use strict";。您可能有一个作为保留关键字的变量。请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Paving_the_way_for_future_ECMAScript_versions 。如果文件中有该 header ,TypeScript 将针对此类变量名称发出警告。

module.exports.roles = { 不是错误的根源。

I've also noticed that for other transpiled modules, tsc sometimes uses "module.exports" and sometimes uses "exports".

它类似于nodejs约定。基本上保存运行时需要解析的字符(字节)。

export let foo = 123;

给你

exports.foo = 123;

(因为 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 进行扩展……但不能使用赋值

关于node.js - TypeScript 外部 Node 模块有时会转换为 module.exports 和导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com