3

I have an esm package.json like so:

{
  "main": "config.js",
  "type": "module",
  "scripts": {
    "start": "npm run build && node ./build/config.js",
    "build": "tsc",
    ...

and a tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2015",
    "module": "ES2020",
    "strict": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "outDir": "./build"
  },
  "include": ["./src", "config.ts"],
  "exclude": ["./*/**.spec.js"]
}

When running npm run start I get the following error:

> tsc

internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'F:\Development\test\example\build\src\handlers\index' imported from F:\Development\test\example\build\config.js
    at finalizeResolution (internal/modules/esm/resolve.js:277:11)
    at moduleResolve (internal/modules/esm/resolve.js:658:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:748:11)
    at Loader.resolve (internal/modules/esm/loader.js:97:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:243:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
    at link (internal/modules/esm/module_job.js:41:36) {
  code: 'ERR_MODULE_NOT_FOUND'

The build directory looks like so:
enter image description here

I've tried already changing the type to commonjs and other configuration changes, what am I doing wrong?

1 Answer 1

3

Appearently even though you are using typescript, we need to change the imports to .js, so in my example:

import IndexHandler from "./src/handlers/index";
becomes:
import IndexHandler from "./src/handlers/index.js";.

It isn't considered a bug but part of TypeScript.

Sign up to request clarification or add additional context in comments.

2 Comments

waow. I consider it a bug.
This behaviour definitely wasn't always a thing. Or is my memory that far off? Even if they do not consider it a bug it is at least a drastic change in behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.