Reputation: 1729
I am trying to understand as how to make a local module. At the root of node application, I have a directory named lib
. Inside the lib directory I have a .js
file which looks like:
var Test = function() {
return {
say : function() {
console.log('Good morning!');
}
}
}();
module.exports = Test;
I have modified my package.json
with an entry of the path to the local module:
"dependencies": {
"chat-service": "^0.13.1",
"greet-module": "file:lib/Test"
}
Now, if I try to run a test script like:
var greet = require('greet-module');
console.log(greet.say());
it throws an error saying:
Error: Cannot find module 'greet-module'
What mistake am I making here?
Upvotes: 1
Views: 13304
Reputation: 1753
modules.export
is incorrect. It should be module.exports
with an s
.
Also, make sure after you add the dependency to do an npm install
. This will copy the file over to your node_modules
and make it available to the require
function.
See here for a good reference.
Update:
After going through some examples to figure this out I noticed, most projects have the structure I laid out below. You should probably format your local modules to be their own standalone packages. With their own folders and package.json
files specifying their dependencies and name. Then you can include it with npm install -S lib/test
.
It worked for me once I did it, and it'll be a good structure moving forward. Cheers.
See here for the code.
Upvotes: 2