admin管理员组文章数量:1400420
I’m working on a CLI tool using Node.js + TypeScript (ESM). I want to load a plugin module dynamically at runtime based on user input, something like:
const plugin = await import(pluginPath);
However, when I try this, I run into a ERR_MODULE_NOT_FOUND error even though the path seems correct and the file definitely exists. The plugin file is a .ts file that gets compiled to .js, and I’m importing the compiled file.
The weird thing is:
If I hardcode the import path, it works If I use require() (with "type": "module" in package.json), it throws If I log the full resolved path, it looks right — but still throws
My project is using "module": "ESNext" and "target": "ES2020" in tsconfig.json.
I tried:
Wrapping the import path with pathToFileURL() from url
Making sure the compiled JS file is in the right location and has .js extension
Using dynamic await import() with both absolute and relative paths
Changing "module" settings to ES2022, NodeNext, etc.
Expected: dynamic import should work when I pass a valid file path that points to a .js file Actual: throws ERR_MODULE_NOT_FOUND even though the file exists
Not sure if this is a path resolution issue, a TypeScript thing, or a Node.js ESM quirk. Would love help from someone who has dealt with dynamic imports in TypeScript + ESM context.
本文标签: How can I dynamically import a TypeScript module in Nodejs with ES modules enabledStack Overflow
版权声明:本文标题:How can I dynamically import a TypeScript module in Node.js with ES modules enabled? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744259165a2597627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论