使用 esm 时,带有 tsconfig-paths 的 ts-node 将不起作用
我无法弄清楚为什么 ts-node 在启用 esm 时无法解析别名
我做了一个小项目试图尽可能地隔离问题
package.json
{
"type": "module"
}
tsconfig.json
{
"compilerOptions": {
"module": "es2020",
"baseUrl": "./",
"paths": {
"$lib/*": [
"src/lib/*"
]
},
},
"ts-node": {
"esm": true
}
}
test.ts
import { testFn } from "$lib/module"
testFn()
lib/module.ts
export function testFn () {
console.log("Test function")
}
命令
ts-node -r tsconfig-paths/register src/test.ts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解决方案来自: https://github.com/TypeStrong/ts-node /discussions/1450#discussion-3563207
目前,ESM 加载器不处理 TypeScript 路径映射。为了使其工作,您可以使用以下自定义加载程序:
然后使用加载程序:
node --loader loader.js index.ts
注意:这仅适用于没有扩展名的模块说明符。例如, import
/foo/bar
有效,但 import/foo/bar.js
和 import/foo/bar.ts
则无效。请记住还要安装这些软件包:
更新:09/11/2023
新加载器将自动解析 index.(js|ts):
示例:
更新:11/04/2024
对于节点版本20、我们需要设置arg:--experimental-specifier-resolution=node
Solution from: https://github.com/TypeStrong/ts-node/discussions/1450#discussion-3563207
At the moment, the ESM loader does not handle TypeScript path mappings. To make it work you can use the following custom loader:
Then use the loader with:
node --loader loader.js index.ts
Caveat: This only works for module specifiers without an extension. For example, import
/foo/bar
works, but import/foo/bar.js
and import/foo/bar.ts
do not.Remember to install these packages as well:
Updated: 09/11/2023
The new loader will automatically resolve the index.(js|ts):
Example:
Updated: 11/04/2024
For the node version 20, we need to set the arg: --experimental-specifier-resolution=node
我建议使用 esno
esno src/test.ts
I suggest to use esno
esno src/test.ts
您可以使用 @bleed- believeer/path-alias 来执行您的 ESM 项目(ts-node 作为依赖项包含在内):
到使用ts-node执行源文件:
假设
"outDir": "./dist"
,直接使用node执行转译代码(绕过ts-node):如果如果您不想使用该库来执行转译后的项目,可以使用 swc (此转换器解析路径别名),或者您可以使用 tsc -alias 使用 tsc 转译项目后。
注意
我根据上次更新更新了库的使用方法。
You can use @bleed-believer/path-alias to execute your ESM project (ts-node is included as dependency):
To execute your source files with ts-node:
Assuming
"outDir": "./dist"
, to execute your transpiled code directly with node (bypassing ts-node):If you don't want to use the library to execute your transpiled project, you can use swc (this transpiler resolves the path aliases), or you can use tsc-alias after transpile your project with tsc.
Note
I updated the method of use the library according the last update.
现在有一个更好的打字稿执行节点模块“可以正常工作”。
https://tsx.is/
There's a better typescript execution node module that "just works" now.
https://tsx.is/