user1592380
user1592380

Reputation: 36205

Error [ERR_MODULE_NOT_FOUND]: Cannot find module

enter image description here

I'm working on a node project (screenshot). I have a single function (urls) in helpers.js which I'm exporting at the bottom as:

module.exports = {
urls: urls,
};

In my index.js I'm trying to import it with:

import { urls } from './helpers';
const myUrls = urls(1,3);

When I run it I get

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/optionhomes11/nodeprojects/hayes/helpers' imported from /home/optionhomes11/nodeprojects/hayes/index.js Did you mean to import ../helpers.js?

What am I doing wrong?

Upvotes: 118

Views: 315890

Answers (14)

Dr NVS
Dr NVS

Reputation: 231

In my case some modifications made by my collogue (coder) to solve TooLarge error. I got this error when tried to git pull with the modifications. I tried many things but finally the below command worked for me.

npm install --legacy-peer-deps -f

Upvotes: 1

Mayana
Mayana

Reputation: 15

If you're using any other js framework and experiencing the same or similar error to this one you can just delete the node_modules folder and do npm install. In many cases, that will fix the problem.

Upvotes: 0

chathudu sahassara
chathudu sahassara

Reputation: 21

use ".js" for all imports

Upvotes: 0

Martins
Martins

Reputation: 15

I have faced this issue, I fixed it by adding dot(.) to the imported file import { connect } from "./dir/file.js";

instead of import { connect } from "./dir/file"; without the .js extention

Upvotes: 0

Craig  Hicks
Craig Hicks

Reputation: 2528

Answer 1

This answer does not require using a runtime flag --es-module-specifier-resolution=node at execution time

However, you have to modify your ts source code, which is a pain if there is are a lot of files. And, the modified files will no longer compile in "commonjs" mode, if you want to go back or use dual "commonjs"/"module" modes.

Modify your tsconfig.json to ensure at least these setting versions:

   compilerOptions:{
    "lib": ["es2020"],
    "module": "ES2022",
    "moduleResolution": "node",
    "target": "es2022",
   }

Works with typescript 4.6.3. (Note sure about 4.6.1 or lower).*

Modify index.js

import {urls} from "#helpers";

Modify package.json

  "imports": {
    "#helpers": "./helpers.js"
  }

The leading "#" is mandatory

Answer 2

In addition to not requiring the node runtime execution flag, this answer also satisifes:

  1. does not require changing your *.*ts source code (thus leaving it compilable under commonjs if you ever chose to do so(*note))
  2. In case you are producing a library, it produces output which can be consumed by either "commonjs" or "module" clients.

(*note) When using rollup, inline maps are required - so there may sometimes be advantage to using commonjs during development and switching to "module" for release.

First modify package.json, create rollup.config.js, and then perform a post tsc action using rollup.

package.json

...
"exports":{
  "require":"./index.cjs",
  "import":"./index.js"
},
"types": "./index.d.ts",
"type": "module"    // you already had this

rollup.config.js

// import resolve from "@rollup/plugin-node-resolve";
import dts from "rollup-plugin-dts";
import commonjs from "@rollup/plugin-commonjs";
import * as path from "path";
import pkg from "./package.json";

export default [
  {
    input: "index.js",
    external:[], // you may quash 'unresolved' warning by adding here
    output: [
      { file: pkg.exports.require, format: "cjs" },
      { file: pkg.exports.import, format: "es" },
    ],
    plugins: [
      commonjs(),
    ],
  },
  {
    input: "./index.d.ts",
    output: [
      { file: pkg.types, format: "es" }, 
    ],
    plugins: [dts()],
  },
];

Call tsc then rollup:

npx tsc 
npx rollup -c

Upvotes: 3

Jayson LP Chen
Jayson LP Chen

Reputation: 131

Try this as below

search in your "/home/optionhomes11/nodeprojects/hayes/index.js"

And looking for "/home/optionhomes11/nodeprojects/hayes/helpers"

change

"/home/optionhomes11/nodeprojects/hayes/helpers"

to

"/home/optionhomes11/nodeprojects/hayes/helpers.js"

Upvotes: 11

maximshurin
maximshurin

Reputation: 5

//if you change package.json file, you should try to reload npm. Write this in terminal

npm i

Upvotes: -7

Islam Hanafi Mahmoud
Islam Hanafi Mahmoud

Reputation: 1190

This Happens because when using ES Modules we are enforced to specify the file extension in the import statement

import * from "./demo.js" // Works fine
import * from "./demo" // Will throw error as you see

Note that : The above two options are both valid when using commonJs instead

Upvotes: 84

Francis  Ade
Francis Ade

Reputation: 398

//helpers.js

export default urls;

//index.js

import urls from './helpers.js';

//package.json (under name)

"type": "module"

Upvotes: 8

Danielo515
Danielo515

Reputation: 7051

When you are using ECMAScript modules you are forced to provide the file extension: https://nodejs.org/api/esm.html#esm_mandatory_file_extensions

So, on top of what other suggested of using "type": "module" on package.json you also need to specify the file extension import {urls} from './helpers.js'. You can also use the flag --es-module-specifier-resolution=node to make it resolve js files as modules just like it did before with require

Upvotes: 212

odili
odili

Reputation: 615

You're not doing anything wrong. The current resolution algorithm for EcmaScript Modules of file extensions and the ability to import directories that have an index file requires using an experimental flag. see esm, the bottom part.

so to make your work as it is, instead of

$ node index.js

you do:

$ node --experimental-specifier-resolution=node index.js

You can also create a script in your package.json

like so:

     "scripts": {
  "start": "NODE_OPTIONS='--experimental-specifier-resolution=node' node src/index.js

Upvotes: 36

Alamin
Alamin

Reputation: 2165

You have used

"type":"module"

then make sure you have import file must be .js extension

Upvotes: 73

wisdom
wisdom

Reputation: 230

I would also recommend you install the Path Intellisense VS code extension. It will really help when handling nested paths.

Upvotes: 2

rav2040
rav2040

Reputation: 111

You should be importing from './helpers', not '.helpers'.

Upvotes: 7

Related Questions