Why can node not find my module?

node cannot find local module
node js require('path)
cannot find module
node core modules
nodejs require
node node_path
node module loader
node module documentation

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

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

Requiring modules in Node.js: Everything you need to know, It also includes a few legacy directories whose use is not recommended. If Node can't find find-me.js in any of these paths, it will throw a “cannot  NODE_PATH is still supported, but is less necessary now that the Node.js ecosystem has settled on a convention for locating dependent modules. Sometimes deployments that rely on NODE_PATH show surprising behavior when people are unaware that NODE_PATH must be set.

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

Node.js Modules: Basics to Advanced - Better Programming, In the Node.js module system, each file is treated as a separate module. To check whether your code is wrapped in a function and whether we are If Node does not find any package.json in the root directory of the module  node makes a distinction between loading from the project directory vs. loading from the node_modules directory below and thus requires a different syntax to specify which one you want. Express.js is also not a stand-alone module because it depends on a bunch of other modules so you could not copy only it.

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.

require.resolve "Error: Cannot find module" even though package , require.resolve will not find the module startbootstrap-sb-admin-2 even This happens on Linux as well as Windows and on node v8 and v10. Yet qmlweb is installed in node_modules/qmlweb, and all the files are there. – Michael Feb 24 '16 at 11:11 Please open a new question posting your relevant information, such as a snippet with your code and the likes, link it here if you prefer.

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

"Cannot find module" when main file not `index.js` with experimental , Clone https://github.com/dandv/node-cant-find-module-with-main-not-index.js I prefer to use Lib.js instead of index.js to distinguish in my IDE  var answer = 42; That answer variable will be globally available in all scripts after the script that defined it. This is not the case in Node. When we define a variable in one module, the other modules in the program will not have access to that variable.

Node.JS: Error Cannot find module [SOLVED] – imcodebased, This is the place where all your global packages are installed. All you have to do now is set the NODE_PATH with the path that you have found for  One potential problem with the package.json file is that it only specifies the version for top-level dependencies. Each module installed may or may not specify the version of the modules it depends on, and so it is possible that you may end up with a different dependency chain than the one used in development. Note.

npm-folders, npm puts various things on your computer. Global install (with -g ): puts stuff in /​usr/local or wherever node is installed. When it is not set, it uses the root of the current package, or the current working directory if not in a When installing locally, this means that you can require("packagename") to load its main module,​  I am also having the same issue. if the Type Definitions are not part of the @types then TypeScript does not file the .d.ts file in the corresponding module folder. Example is fuse.js, it is packaged with it's own .d.ts file but vsCode is unable to find the module on import.

esm, esm is the world's most advanced ECMAScript module loader. This fast, production ready, Use esm to load the main ES module and export it as CommonJS. index.js Builtin require cannot sideload .mjs files. However, .js  I am just starting with coding so if you have further general advice how to best debug please feel free to add this to your answer. Cheers! module.js:339 throw err; ^ Error: Cannot find module './app/models/user' at Function.Module._resolveFilename (module.js:337:15) at Function.Module._load (module.js:287:25) at Module.require (module.js:366:17)

Comments
  • Are you trying to require lib/uploader.js from inside lib/test.js?
  • 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.
  • FWIW ./ means relative to the current directory and isn't "invalid."
  • @mscdex thanks for the clarification - however if that is the case, why does require('./uploader.js') throw the error.
  • There's something abnormal at play here; From your screenshot all looks valid.
  • Hey, I should have included in my question that I tried ./uploader too, but I still get the Cannot find module error
  • 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
  • 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.