Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_MODULE_NOT_FOUND] when using tsc #41887

Closed
agamm opened this issue Dec 9, 2020 · 10 comments
Closed

Error [ERR_MODULE_NOT_FOUND] when using tsc #41887

agamm opened this issue Dec 9, 2020 · 10 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@agamm
Copy link

agamm commented Dec 9, 2020

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:

> example2@1.0.0 start F:\Development\test\example2
> npm run build && node ./build/config.js


> example2@1.0.0 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! example2@1.0.0 start: `npm run build && node ./build/config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the example2@1.0.0 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.

@xgqfrms
Copy link

xgqfrms commented Dec 9, 2020

you may have the wrong import path

in the experimental mode you need to define the full path with extension.

old ❌

config.ts

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

new ✅

config.ts

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

Why you need ES2020 as your build target, it just useful for testing new features, do not use it in your production.

@agamm
Copy link
Author

agamm commented Dec 9, 2020

you may have the wrong import path

in the experimental mode you need to define the full path with extension.

old ❌

config.ts

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

new ✅

config.ts

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

Why you need ES2020 as your build target, it just useful for testing new features, do not use it in your production.

Thanks for the quick reply, it seems really werid to reference a file which really uses .ts as .js, will this be fixed?

@RyanCavanaugh
Copy link
Member

This is the intended behavior; because TypeScript doesn't modify JavaScript code you write, the import path you write should be the one you want to appear in the output .js file

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Dec 9, 2020
@agamm
Copy link
Author

agamm commented Dec 9, 2020

This is the intended behavior; because TypeScript doesn't modify JavaScript code you write, the import path you write should be the one you want to appear in the output .js file

I don't know if this is something I can relate to, but at least can there be a more indicative error/warning during the compiling process?

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@karlhorky
Copy link
Contributor

This is the intended behavior; because TypeScript doesn't modify JavaScript code you write, the import path you write should be the one you want to appear in the output .js file

Very interesting, we rewrote a package to use the fully-specified ESM paths with .js and everything worked well!

...until we had to import some of these files in a second package that was consuming TypeScript via webpack (tried with both babel-loader and ts-loader):

gatsbyjs/gatsby#31077 (comment)

Then we got the opposite error, webpack + babel-loader could not resolve the .js file, (because there is no file on the filesystem?):

ERROR #98124  WEBPACK

Can't resolve './js-functions.js' in 'D:\upleveled-softwork\courses\packages\quizzes'

If you're trying to use a package make sure that './js-functions.js' is installed. If you're trying to use a local file make sure that the path is correct.

failed Building development bundle - 30.415s
ERROR in ../quizzes/index.ts 4:0-62
Module not found: Error: Can't resolve './js-functions.js' in 'D:\upleveled-softwork\courses\packages\quizzes'
...

@karlhorky
Copy link
Contributor

Maybe this is actually a bug in either webpack itself or babel-loader / ts-loader:

webpack/webpack#13252

@semslam
Copy link

semslam commented Mar 1, 2022

I was trying to import RockPaperScissor class in my controller and I was getting an error

RockPaperScissor.js

class RockPaperScissor{

constructor(gameMode) {
this.gameMode = gameMode;
this.API_RESPONSE = null;
this.consoleMode = new ConsoleMode();
this.gameComponents = new GameComponents();
}

}
export default RockPaperScissor;

humanMoves.controllers.js

import RockPaperScissor from "../../services/RockPaperScissor.js";

const rockPaperScissor = new RockPaperScissor("api");

// this is my configuration in package.json
package.json

"main": "server.js",
"type": "module",
"scripts": {
"dev": "node --experimental-specifier-resolution=node nodemon server"
},

// getting error

internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/ibrahim/test/rock-paper-cissors-game/services/GameComponents' imported from /Users/ibrahim/test/rock-paper-cissors-game/services/RockPaperScissor.js
at finalizeResolution (internal/modules/esm/resolve.js:276:11)
at moduleResolve (internal/modules/esm/resolve.js:699:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
at Loader.resolve (internal/modules/esm/loader.js:88:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
at ModuleWrap. (internal/modules/esm/module_job.js:56:40)
at link (internal/modules/esm/module_job.js:55:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

@emeryao
Copy link

emeryao commented Aug 31, 2022

node --experimental-specifier-resolution=node ./build/config.js

works for me with node v18 and ts v4.8

@guotie
Copy link

guotie commented Aug 18, 2023

this is really weird

@microsoft microsoft locked as resolved and limited conversation to collaborators Aug 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

8 participants