#ERR_MODULE_NOT_FOUND

20 messages · Page 1 of 1 (latest)

sleek pike
#

I got this error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/my-project/dist/structs/Handler imported from /my-project/dist/handlers/CommandHandler.js

I now it is because imports for JS need the .js extension but it is written in TS and compiled to JS.

I either need to solve this error OR find a way to run TS natively (without compiling to JS)

My files:

// Path to file: src/handlers/CommandHandler.ts
import Handler from "../structs/Handler";

export default class CommandHandler extends Handler {
    constructor() {
        super();
    }
}

const test = new CommandHandler();

// Path to file: src/structs/Handler.ts
import { dirname } from 'path';

export default class Handler {
    constructor() {
        console.log("handler test")
    }
}

script: tsc && node ./dist/handlers/CommandHandler.js

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "ES2022",
    "outDir": "dist",
    "rootDir": "src",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
  }
}
serene folio
#

are you actually using ESM on the node side of things?

sleek pike
#

Oh I fixed it, I had to remove "type": "module" in the package.json and change tsconfig.json to "module": "CommonJS"

serene folio
#

perfect

sleek pike
serene folio
#

no it will not

sleek pike
#

than it is not the correct solution xD

#

I need await import()

novel notch
sleek pike
serene folio
#

yes.

#

typescript doesn't ever rewrite import paths, so you have to make it correct initially

sleek pike
#

A little bit strange but I guess it's the only solution. Because I need the dynamic imports

serene folio
#

(and you have "type": "module" set in your package.json)

rich rivet
#

I'm done both of the above and am having this exact issue on a node 16.17.x typescript 4.7-4.9 project trying to import or export esm.

serene folio
#

you're putting the .js suffix onto the import, or is it just not printing an error if you omit it?

rich rivet
#

Like OP, the error I was getting was Error [ERR_MODULE_NOT_FOUND]: Cannot find module

  1. I was indeed putting the .js suffix onto the import,
  2. I was importing ts definitions from an index.d.ts file but called it from index.js
    tsconfig.json
    Was trying TS 4.7, 4.9
{
    "compilerOptions": {
        "target": "esnext",
        "module": "esnext",
        "lib": [
            "es2022"
        ],
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "moduleResolution": "node", // was swapping out node16 and nodenext
        "typeRoots": [
            "node_modules/@types",
            "types/"
        ],
        "types": [
            "node"
        ],
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true
    },
  "ts-node": {
    "esm": true
  }
...
}

Tried babel, then restarted my editor, removed a lot of dependencies... got it running

rich rivet
#

I was stuck on this for 2 days... almost went back to module:"commonjs"