1

Here, I'm exporting the countries array in the external javascript file.

const countries = [
  "Albania",
  "Bolivia",
  "Canada",
  "Denmark",
  "Ethiopia",
  "Finland",
  "Germany",
  "Hungary",
  "Ireland",
  "Japan",
  "Kenya",
];
export default countries;

Now, I'm importing and console log it in my main.js file

import countries from "./countries";
console.log(countries);

But, Why I'm receiving the error

node:internal/errors:484
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 
  • 2
    Does this answer your question? [Error \[ERR\_MODULE\_NOT\_FOUND\]: Cannot find module](https://stackoverflow.com/questions/65384754/error-err-module-not-found-cannot-find-module) – Moshe Fortgang Jul 18 '23 at 13:32

1 Answers1

3

If you are using ECMAScript Modules in your project make sure to specify the file type extension.

In your case try changing :

import countries from "./countries";

To :

import { countries } from "./countries.js";

Keep in mind that both are correct when it comes to the common javascript based approach.

Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42