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