Why I'm getting Error [ERR_MODULE_NOT_FOUND]: Cannot find module when running the server
Asked Answered
G

2

6

I'm doing a startup server. Basically It has initial setup with express and nodemon dependencies. Unfortunately, I'm getting ERR_MODULE_NOT_FOUND when running yarn dev or npm run dev Here is the code and file structure I have. enter image description here

Gottfried answered 1/2, 2022 at 9:21 Comment(0)
H
3

You need to add .js extension index.js:

import { SampleExport } from "../path/file.js"

In my opinion, it is better to use .env files together with some npm package or even a json file.

Hephzipa answered 1/2, 2022 at 10:55 Comment(4)
Thank you, yes, I will use .env but I need env.js for my logic to get prod or dev env. I'm confused why it requires the file extension. I'm currently having react project and importing just the file name at the end.Gottfried
I am glad that my answer will help you, I would be grateful if you would accept it.Hephzipa
Adding .js to my import worked for me too. Is there a linter that actually warns for that behaviour? @JorgeMontejoBalmy
@Balmy I don't know of any extension that would work for you, just keep in mind what CherryDT says, when using import instead of require, you should use the .js extension (unless you are using type script of course).Hephzipa
M
16

The issue is a missing file extension. The import needs to look like this:

import { sampleExport } from "../config/env.js";
//                                         ^^^

import doesn't use the same algorithm that require used. Instead, it works according to the ECMAScript Modules specification, which requires a definite extension.

This is also explained in the node.js docs for ECMAScript modules:

A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. './startup/index.js') must also be fully specified.

This behavior matches how import behaves in browser environments, assuming a typically configured server.

Matsuyama answered 1/2, 2022 at 11:18 Comment(2)
Thank you.I'm working on react frontend and little knowledge on backend How it differ from react project when importing? I import as usual without file extension.Gottfried
React uses Webpack as bundler configured in such a way that it resolves imports in a more relaxed fashion. If you'd use the browsers' native ESM implementation, import would also require a file extension.Matsuyama
H
3

You need to add .js extension index.js:

import { SampleExport } from "../path/file.js"

In my opinion, it is better to use .env files together with some npm package or even a json file.

Hephzipa answered 1/2, 2022 at 10:55 Comment(4)
Thank you, yes, I will use .env but I need env.js for my logic to get prod or dev env. I'm confused why it requires the file extension. I'm currently having react project and importing just the file name at the end.Gottfried
I am glad that my answer will help you, I would be grateful if you would accept it.Hephzipa
Adding .js to my import worked for me too. Is there a linter that actually warns for that behaviour? @JorgeMontejoBalmy
@Balmy I don't know of any extension that would work for you, just keep in mind what CherryDT says, when using import instead of require, you should use the .js extension (unless you are using type script of course).Hephzipa

© 2022 - 2024 — McMap. All rights reserved.