gpt4 book ai didi

node.js - 如何导出具有多个静态方法的类

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

我正在尝试在node.js中导出此类:

export class className {
static method1(param1) {
// do something
}

static method2(param1, param2) {
// do something
}
}

但是它在终端中出现以下错误:

complete_path.....\node_modules@babel\runtime\helpers\esm\classCallCheck.js:1 [2] (function (exports, require, module, __filename, __dirname) { export default function _classCallCheck(instance, Constructor) { [2]
^^^^^^ [2] [2] SyntaxError: Unexpected token export [2] at new Script (vm.js:83:7) [2] at createScript (vm.js:267:10) [2] at Object.runInThisContext (vm.js:319:10) [2] at Module._compile (internal/modules/cjs/loader.js:685:28) [2] at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10) [2] at Module.load (internal/modules/cjs/loader.js:620:32) [2]
at tryModuleLoad (internal/modules/cjs/loader.js:560:12) [2] at Function.Module._load (internal/modules/cjs/loader.js:552:3) [2]
at Module.require (internal/modules/cjs/loader.js:658:17) [2] at require (internal/modules/cjs/helpers.js:22:18) [2] [nodemon] app crashed - waiting for file changes before starting...

最佳答案

Node.js 尚不支持 export 关键字。您必须使用 exportsmodule.exports

在您的情况下,您应该使用module.exports:

module.exports = class className {
static method1(param1) {
// do something
}

static method2(param1, param2) {
// do something
}
}

有关 exportsmodule.exports 之间差异的更多信息,我建议您这样做 post .

关于node.js - 如何导出具有多个静态方法的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978866/

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