10

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'

2
  • I'm having the very same issue right now. It's weird because the module sits there in node_modules folder. Not sure why. Commented Aug 5, 2019 at 5:42
  • I think you missed the main section in your package.json inside the test-module package. Commented Aug 5, 2019 at 5:51

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

To add on @Blaze answer, if you follow the steps (Local Paths) to install a local module, it will sort out for you the local dependency in your package.json:

npm i ./test --save

That will produce the correct local dependency entry in your dependencies in the root package.json:

"test-module": "file:test"

assuming test-module is the name in the local dep package.json.

This is how it should look like:

enter image description here

6 Comments

Now the module not found error is gone but i got another error app.set not a function
@iambatman: That would be a completely different problem :) I suggest you open another question for that one.
If i removed the import its gone
What's on your test-module? Sounds like there is an app.set which is not in the original question
It contains a index.js file with a function i 've included that in the question
|
0

Make sure your test folder has a package.json.

test/package.json should have a "name" field with the value "test-module" (ie, same name as the dependency in your root package.json.

My files:

test/package.json

{
  "name": "test-module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

test/index.js

module.exports = {
  t:() => console.log('t')
};

package.json

{
  "name": "t",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "test-module": "file:test"
  }
}

app.js

t = require('test-module');
t.t();

This is working for me.

7 Comments

Do i need to install anyother packages??
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.
I updated my answer to show all the files in my example. Can you show us the file structure inside node_modules folder?
my test folder is not in the node_modules folder do i need to add manually in it??
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.