node.js local modules :cannot find module error

12,364

you can provide a path to a local directory that contains a package

{
  "name": "baz",
  "dependencies": {
    "bar": "file:../foo/bar"
  }
}

and perform npm install -s or npm install --save reference

Share:
12,364
shellakkshellu
Author by

shellakkshellu

stay hungry stay foolish

Updated on June 17, 2022

Comments

  • shellakkshellu
    shellakkshellu almost 2 years

    I am trying to implement local modules in my application

    1.Project root folder i have created folder named test with a file named index.js

          module.exports  = {
    
         myFunction:function(){
           console.log('ok');
         }
    }
    

    2.Added the following in package.json in the root folder

    "dependencies": { 
        "test-module": "file:test"
      }
    

    3.When i try to import var module = require('test-module'); in app.js i got this error

    Cannot find module 'test-module'

  • shellakkshellu
    shellakkshellu about 6 years
    Yes i have the same
  • shellakkshellu
    shellakkshellu about 6 years
    Do i need to install anyother packages??
  • Soubhik Mondal
    Soubhik Mondal about 6 years
    No, you don't need any other packages. I tried exactly your code, and it worked for me. The only issue I got was when the name field was different.
  • shellakkshellu
    shellakkshellu about 6 years
    Now the module not found error is gone but i got another error app.set not a function
  • Daniel Conde Marin
    Daniel Conde Marin about 6 years
    @iambatman: That would be a completely different problem :) I suggest you open another question for that one.
  • shellakkshellu
    shellakkshellu about 6 years
    If i removed the import its gone
  • Daniel Conde Marin
    Daniel Conde Marin about 6 years
    What's on your test-module? Sounds like there is an app.set which is not in the original question
  • shellakkshellu
    shellakkshellu about 6 years
    It contains a index.js file with a function i 've included that in the question
  • Soubhik Mondal
    Soubhik Mondal about 6 years
    I updated my answer to show all the files in my example. Can you show us the file structure inside node_modules folder?
  • Daniel Conde Marin
    Daniel Conde Marin about 6 years
    Where is app.set? Please update your question with the rest of the code, otherwise will be hard to help.
  • shellakkshellu
    shellakkshellu about 6 years
    my test folder is not in the node_modules folder do i need to add manually in it??
  • Soubhik Mondal
    Soubhik Mondal about 6 years
    no, you have to npm install or npm i from the root folder. That will automatically generate the node_modules folder with your test-module inside it.
  • shellakkshellu
    shellakkshellu about 6 years
    I have deleted the entire node_modules and removed the test module after that npm install npm ERR! Could not install from "test" as it does not contain a package.json file
  • shellakkshellu
    shellakkshellu about 6 years
    But my package.json doesn't contain the local modules