Mocha在使用ESM时,使用Typegoose时出现“找不到命名导出’Ref’”错误。

huangapple go评论102阅读模式
英文:

Mocha fails with "named export 'Ref' not found" error with Typegoose when using ESM

问题

抱歉,我明白你只需要翻译代码部分。以下是代码部分的翻译:

I have a project written in TypeScript with ESM. Some of it uses Typegoose. The project codebase builds fine and runs perfectly, but Mocha fails to run tests with this error:

> SyntaxError: Named export 'Ref' not found. The requested module '@typegoose/typegoose' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

> import pkg from '@typegoose/typegoose';
const { modelOptions, prop, plugin, getModelForClass, Ref, } = pkg;

It doesn't matter if I use the alternate method of importing, I still get an error about Ref not being found. This is the only Typegoose piece I have issues with. If I don't use Ref Mocha has no issues testing any of the other code that depends on Typegoose.

tsconfig.json has the following set:

"module": "esnext"
"moduleResolution": "node"

package.json has the following set:

"type": "module",

.mocharc.jsonc has the following set:

"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"],

Does anyone know how to resolve this issue? Since the code runs fine outside of Mocha I assume it is some sort of configuration thing I'm missing, but I can't figure out what it might be.
英文:

I have a project written in TypeScript with ESM. Some of it uses Typegoose. The project codebase builds fine and runs perfectly, but Mocha fails to run tests with this error:

> SyntaxError: Named export 'Ref' not found. The requested module '@typegoose/typegoose' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

> import pkg from '@typegoose/typegoose';
const { modelOptions, prop, plugin, getModelForClass, Ref, } = pkg;

It doesn't matter if I use the alternate method of importing, I still get an error about Ref not being found. This is the only Typegoose piece I have issues with. If I don't use Ref Mocha has no issues testing any of the other code that depends on Typegoose.

tsconfig.json has the following set:

"module": "esnext"
"moduleResolution": "node"

package.json has the following set:

"type": "module",

.mocharc.jsonc has the following set:

  "node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"],

Does anyone know how to resolve this issue? Since the code runs fine outside of Mocha I assume it is some sort of configuration thing I'm missing, but I can't figure out what it might be.

答案1

得分: 0

Ok, with the help of the Typegoose folks I figured out the cause: my tsconfig.json specified "transpileOnly" as true, which was causing the type to incorrectly get imported. Turning "transpileOnly" to false fixed the problem.

英文:

Ok, with the help of the Typegoose folks I figured out the cause: my tsconfig.json specified "transpileOnly" as true, which was causing the type to incorrectly get imported. Turning "transpileOnly" to false fixed the problem.

huangapple
  • 本文由 发表于 2023年8月10日 22:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定