Reputation: 4079
I am using node v0.12.5
with nwjs and I have defined my own custom module inside of my project so that I can modularise my project (obviously).
I am trying to call my module from another module in my project but every time I attempt to require it I get the error could not find module 'uploader'
.
My uploader module is currently very simple and looks like:
function ping_server(dns, cb) {
require('dns').lookup(dns, function(err) {
if (err && err.code == "ENOTFOUND") {
cb(false);
} else {
cb(true);
}
})
}
function upload_files()
{
}
module.exports.ping_server = ping_server;
module.exports.upload_files = upload_files;
With the idea that it will be used to recursively push files to a requested server if it can be pinged when the test device has internet connection.
I believe I have exported the methods correctly here using the module.exports
syntax, I then try to include this module in my test.js
file by using:
var uploader = require('uploader');
I also tried
var uploader = require('uploader.js');
But I believe node will automatically look for uploader.js
if uploader
is specified.
The file hierarchy for my app is as follows:
package.json
public
|-> lib
|-> test.js
|-> uploader.js
|-> css
|-> img
The only thing I am thinking, is that I heard node will try and source the node_modules folder which is to be included at the root directory of the application, could this be what is causing node not to find it? If not, why can node not see my file from test.js given they exist in the same directory?
UPDATE Sorry for the confusion, I have also tried using require('./uploader')
and I am still getting the error: Uncaught Error: Cannot find module './uploader'
.
UPDATE 2 I am normally completely against using images to convey code problems on SO, but I think this will significantly help the question:
It's clear here that test.js and uploader.js reside in the same location
Upvotes: 0
Views: 556
Reputation: 1342
If you're developing on a mac, check your file system case sensitivity. It could be that the required filename is capitalized wrong.
Upvotes: 0
Reputation: 4079
This problem stemmed from using Node Webkit instead of straight Node, as stated in their documentation all modules will be source from a node_modules directory at the root of the project.
Any internal non C++ libraries should be placed in the node_modules directory in order for Node webkit to find them.
Therefore to fix, I simply added a node_modules
directory at the root of my project (outside of app and public) and placed the uploader.js
file inside of there. Now when I call require('uploader')
it works as expected.
Upvotes: 0
Reputation: 106698
When you don't pass a path (relative or absolute) to require()
, it does a module lookup for the name passed in.
Change your require('uploader')
to require('./uploader')
or require(__dirname + '/uploader')
.
Upvotes: 1
Reputation: 17522
To load a local module (ie not one from node_modules
) you need to prefix the path with ./
. https://nodejs.org/api/modules.html#modules_modules
So in your case it would be var uploader = require('./uploader');
Upvotes: 0