wallabyjs / quokka

Repository for Quokka.js questions and issues
https://quokkajs.com
1.17k stars 31 forks source link

Cannot find module #948

Closed devastion closed 2 months ago

devastion commented 2 months ago

Issue description or question

Importing directory,, doesn't catch index.ts. Example - we have

- src
- - a
- - - fn
- - - - fn.ts index.ts
- - b
- - - file.js > import fn from "@a/fn" 

@a is ts path alias.

Quokka.js Console Output

Cannot find module '<rootDir>/src/a/fn' imported from <rootDir>/src/b/file.js 
  ​​​​​at ​​​​​​​​finalizeResolution​​​ ​/Users/<user>/.quokka/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:352​
  ​​​​​at ​​​​​​​​moduleResolve​​​ ​/Users/<user>/.quokka/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801​
  ​​​​​at ​​​​​​​​Object.defaultResolve​​​ ​/Users/<user>/.quokka/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912​

Code editor version Visual Studio Code v1.88.1

OS name and version MacOS Sonoma 14.4.1

smcenlly commented 2 months ago

We don't have enough detail to create a reproducible sample of your issue, but it sounds like your tsconfig.json needs some additional configuration.

You will need to explicitly add a path to resolve your index.ts. For example:

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@a/fn/*": ["./a/fn/*"],
+     "@a/fn": ["./a/fn/index"]
    }
  },
}

If that doesn't work for you, can you please provide us with a sample project?

devastion commented 2 months ago

Hi, @smcenlly! You can see example here. I am importing from a directory too (relative path), but quokka doesn't pick up automatically index. It may be issue with ts-node or something else. I am not sure.
EDIT: I am exporting in this case, but even when use import * as varName from "./plugins" doesn't work.
ERR_UNSUPPORTED_DIR_IMPORT <rootDir>/src/plugins <rootDir>/src/index.js

smcenlly commented 2 months ago

This is actually node.js expected behavior when you use ESM (i.e. "type": "module" in your package.json).

You will need to configure Quokka to run node with an option to allow importing directories with the experimental-specifier-resolution setting via Quokka configuration:

{
  "env": {
     "params": {
        "runner": "--experimental-specifier-resolution=node"
     }
  }
}
devastion commented 2 months ago

Thanks! It resolved my problem.