You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dynamically importing files with the ".ts" extension from a package dependency throws ERR_UNKNOWN_FILE_EXTENSION when starting a service through a Vitest setup file. Note that both the service and the package dependency are ESM.
To demonstrate, I have a dependency that exports the following importFile utility:
And I have a service that calls the importFile function:
import{importFile}from'fake-dep';import{dirname,parse,resolve}from'path';import{fileURLToPath}from'url';constfileName=fileURLToPath(import.meta.url);constdirName=dirname(fileName);constdir=resolve(dirName,'./dir/');importFile(`${dir}/some-file.ts`);// Throws ERR_UNKNOWN_FILE_EXTENSION with .ts extensionimportFile(`${dir}/some-file.js`);// Throws ERR_MODULE_NOT_FOUND with .js extension
Running the service (with ts-node) works as expected and neither of these errors are thrown. But they are when running the Vitest suite.
But when exactly the same function exists directly in the service rather than in a dependency, it works as expected running with ts-node or through Vitest:
import{importFileasdepImportFile}from'fake-dep';import{dirname,parse,resolve}from'path';import{fileURLToPath}from'url';constfileName=fileURLToPath(import.meta.url);constdirName=dirname(fileName);constdir=resolve(dirName,'./dir/');constlocalImportFile=async(filePath: string)=>{constfileName=parse(filePath).name;constexports=awaitimport(filePath);return{[fileName]: exports,};};// This workslocalImportFile(`${dir}/some-file.ts`);// This works toolocalImportFile(`${dir}/some-file.js`);// This doesn't workdepImportFile(`${dir}/some-file.ts`);// Neither does thisdepImportFile(`${dir}/some-file.js`);
Interesting repro. Two errors are probably due to difference causes.
The first one ERR_UNKNOWN_FILE_EXTENSION with .ts extension is about externalized (i.e. not transformed by Vite) fake-dep importing .ts file, which causes NodeJs to simply reject such import("xxx.ts"). This needs to be fixed by test.server.deps.inline: ["fake-dep"] since Vitest by default doesn't transform `node_modules.
Somewhere inside of webpack loading of the "/src/webpackLoader.ts" happens, and similar error is thrown.
As far as i understand that is caused by similar reason, the call chain is vitest -> test -> webpack (node_modules) -> webpackLoader.ts which is not processed by Vitest.
I tried to add
test.server.deps.inline: ["webpack"] as well as test.server.deps.inline: ["loader-runner"] and test.server.deps.inline = true but nothing works, the loader from sourcecode is not processed by Vite.
Any ideas how to fix that? May be there is a way to manually instrument this import:
"@lingui/loader": path.resolve(__dirname, "../src/webpackLoader.ts"), so it will go thru the vitest server?
I probably got the reason. Most likely something inside a Webpack is importing a resolver using a require. Require is not hijacked by the Vite and Vitest, that's why nothing from what I've tried worked.
Describe the bug
Dynamically importing files with the ".ts" extension from a package dependency throws
ERR_UNKNOWN_FILE_EXTENSION
when starting a service through a Vitest setup file. Note that both the service and the package dependency are ESM.To demonstrate, I have a dependency that exports the following
importFile
utility:And I have a service that calls the
importFile
function:Running the service (with
ts-node
) works as expected and neither of these errors are thrown. But they are when running the Vitest suite.But when exactly the same function exists directly in the service rather than in a dependency, it works as expected running with
ts-node
or through Vitest:Reproduction
https://stackblitz.com/edit/vitest-dynamic-import-in-node-modules-error?file=readme.md
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: