最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I dynamically import a TypeScript module in Node.js with ES modules enabled? - Stack Overflow

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论