我有一个用打字稿编写的快速服务器"module": "es2020"
在其 tsconfig.
我还开发了另一个es2020
我的 graphql API 的模块,仍然是打字稿,并且该模块使用带有此类命名导入的 mongoose:
import { Types } from 'mongoose'
当我使用以下命令编译 graphql 模块时,一切正常tsc
。但是运行的快递服务器
nodemon --watch './**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' src/index.ts
无法处理名为 import 的猫鼬。
import { Types } from 'mongoose';
^^^^^
SyntaxError: Named export 'Types' not found. The requested module 'mongoose' 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 'mongoose';
const { Types } = pkg;
解决方案#1
import mongoose from 'mongoose'
并更换Types
by mongoose.Types
.
但是由于tsc
可以处理名为 import 的猫鼬,我希望也可以使 ts-node 能够这样做。
解决方案#2
切换到commonjs
,我可以将导入/导出语法保留在我的 graphql 模块中并将其编译为 cjs 模块。但我必须在我的 Express 服务器中使用 cjs 语法,但我不想这样做。