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
Comments
-
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 namedindex.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');
inapp.js
i got this errorCannot find module 'test-module'
-
shellakkshellu about 6 yearsYes i have the same
-
shellakkshellu about 6 yearsDo i need to install anyother packages??
-
Soubhik Mondal about 6 yearsNo, 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 about 6 yearsNow the module not found error is gone but i got another error
app.set
not a function -
Daniel Conde Marin about 6 years@iambatman: That would be a completely different problem :) I suggest you open another question for that one.
-
shellakkshellu about 6 yearsIf i removed the import its gone
-
Daniel Conde Marin about 6 yearsWhat's on your test-module? Sounds like there is an
app.set
which is not in the original question -
shellakkshellu about 6 yearsIt contains a
index.js
file with a function i 've included that in the question -
Soubhik Mondal about 6 yearsI 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 about 6 yearsWhere is
app.set
? Please update your question with the rest of the code, otherwise will be hard to help. -
shellakkshellu about 6 yearsmy
test
folder is not in thenode_modules
folder do i need to add manually in it?? -
Soubhik Mondal about 6 yearsno, you have to
npm install
ornpm i
from the root folder. That will automatically generate thenode_modules
folder with yourtest-module
inside it. -
shellakkshellu about 6 yearsI 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 about 6 yearsBut my package.json doesn't contain the local modules