0

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:

enter image description here

It's clear here that test.js and uploader.js reside in the same location

6
  • Are you trying to require lib/uploader.js from inside lib/test.js?
    – rrowland
    Commented Aug 7, 2015 at 15:28
  • No I am using require('./uploader.js') from inside lib/test.js, this screams invalid to me because ./ normally dictates root directory. If I try to use lib/uploader.js or public/lib/uploader.js I still get errors.
    – Halfpint
    Commented Aug 7, 2015 at 15:31
  • FWIW ./ means relative to the current directory and isn't "invalid."
    – mscdex
    Commented Aug 7, 2015 at 15:32
  • @mscdex thanks for the clarification - however if that is the case, why does require('./uploader.js') throw the error.
    – Halfpint
    Commented Aug 7, 2015 at 15:33
  • There's something abnormal at play here; From your screenshot all looks valid.
    – rrowland
    Commented Aug 7, 2015 at 15:44

4 Answers 4

1

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').

3
  • Hey, I should have included in my question that I tried ./uploader too, but I still get the Cannot find module error
    – Halfpint
    Commented Aug 7, 2015 at 15:26
  • I would then think to use ./public/lib/uploader to specify the relative path to the library directory, but my IDE complains about the syntax
    – Halfpint
    Commented Aug 7, 2015 at 15:29
  • 1
    Which file are you trying to require('./uploader') from? If it is the test.js script that is in the same directory as uploader.js, there should not be a problem. You could also try require(__dirname + '/uploader') too, but it should be equivalent.
    – mscdex
    Commented Aug 7, 2015 at 15:32
0

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');

0

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.

0

If you're developing on a mac, check your file system case sensitivity. It could be that the required filename is capitalized wrong.

Not the answer you're looking for? Browse other questions tagged or ask your own question.