Skip to content

Error [ERR_MODULE_NOT_FOUND] when using tsc #41887

@agamm

Description

@agamm

TypeScript Version: 4.1.2

Search Terms: esm, Error [ERR_MODULE_NOT_FOUND], internalBinding('errors')

Code

Where the directory strcture is:

src/
  handlers/
    index.ts
config.ts

config.ts

import IndexHandler from "./src/handlers/index";
IndexHandler()

index.ts

const IndexHandler = () => {
  console.log(1);
};

export default IndexHandler;

tsconfig.json:

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

package.json:

{
  "name": "example2",
  "version": "1.0.0",
  "description": "",
  "main": "build/config.js",
  "type": "module",
  "scripts": {
    "start": "npm run build && node ./build/config.js",
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Then run npm run start

Expected behavior:
A console.log(1) in stdout.

Actual behavior:

> [email protected] start F:\Development\test\example2
> npm run build && node ./build/config.js


> [email protected] build F:\Development\test\example2
> tsc

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'F:\Development\test\example2\build\src\handlers\index' imported from F:\Development\test\example2\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'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `npm run build && node ./build/config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.

Playground Link:
This is related to the files and imports, no way that I saw to do it in the playground.

Related Issues:
Nope.

** Extra info **

$ node --version
v14.15.1
$ tsc -v
Version 4.1.2

Windows 10 64 bit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions