Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found error in node.js

I am new to node.js, this is my first node application so, please excuse me if I'm asking obvious question. I have a file called utils.js and I need to have functions defined in that file to be available in main.js. So I tried giving

require(utils.js)

But it is throwing me this error:

Error: Cannot find module 'utils.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)

My main.js is under c:\demo\proj\src\main\main.js and utils.js is under c:\demo\proj\src\utils\utils.js.

I tried below require combinations, but still I am getting cannot find module error:

  • require(/proj/src/utils/utils.js);

  • require(/utils.js);

  • require(c:/demo/proj/src/utils/utils.js);

Even I tried to put it under node_modules folder, but still same error. Can you kindly guide me what I am doing mistake here?

Edit:

I tried putting changing my folder structure as pointed out by @mithunsatheesh as below:

  • project
    • src
      • utils - utils.js
  • main.js

My require is as follows: require('./src/utils/utils.js')

But when I execute node main.js still I am getting below error:

Error: Cannot find module './src/utils/utils.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
like image 204
Pradeep Simha Avatar asked Apr 23 '13 07:04

Pradeep Simha


People also ask

How do I resolve Cannot find module error using node JS?

To solve the "Cannot find module" error in Node. js, make sure to install the package from the error message if it's a third party package, e.g. npm i somePackage . If you get the error with a local module, make sure to point the node command to a file that exists.

Can not find module in node JS?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

Can not find module npm?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.


1 Answers

according to the folder structure you mentioned in the question, you have to try

require('../utils/utils.js')

This is the case if you have your project folder structured like

  • proj
    • src
      • utils
        • utils.js
      • main
        • main.js

and you are doing node main.js

To comment on the details provided in your question.

  1. please dont use require(c:/demo/proj/src/utils/utils.js); as you are tried out. imagine like you are exporting the proj folder with your project files then the mentioned require will be an error.

  2. Also the folder structure could be made to something like

    • proj
      • src
        • utils - utils.js
      • main.js
      • package.json

so that you keep the main file in root of project folder. and require the utils.js like

 require('./src/utils/utils.js')

UPDATE

As far as ican see from the updated error message. Its still the issue with the path of 'utils.js' in require. From your updated folder structre It seems that main.js is in same level as proj folder, see that the proposed folder structure had main.js and src folder in same level inside proj folder.

Even that was a suggestion that I made as you were following a folder structure that dint make any sense. Simply require('../utils/utils.js') would have solved your issue without even altering the folder structure you mentioned in the beginning.

like image 63
Mithun Satheesh Avatar answered Sep 22 '22 08:09

Mithun Satheesh