Reputation: 105
When I'm importing modules using require, it throws a MODULE_NOT_FOUND error. (mymain.js)
var counter = require(".\count");
var array=[10,20,30];
console.log(counter(array));
Here is the module that I'm trying to import(count.js)
var counter = function(arr){
return 'length= '+arr.length;
};
module.exports = counter ;
Both of these codes are in the same directory. Node mymain throws the following error.
D:\Programming\NodeJS>node mymain
internal/modules/cjs/loader.js:1017
throw err;
^
Error: Cannot find module '.count'
Require stack:
- D:\Programming\NodeJS\mymain.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1014:15)
at Function.Module._load (internal/modules/cjs/loader.js:884:27)
at Module.require (internal/modules/cjs/loader.js:1074:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (D:\Programming\NodeJS\mymain.js:1:15)
at Module._compile (internal/modules/cjs/loader.js:1185:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
at Module.load (internal/modules/cjs/loader.js:1034:32)
at Function.Module._load (internal/modules/cjs/loader.js:923:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'D:\\Programming\\NodeJS\\mymain.js' ]
}
Upvotes: 1
Views: 6418
Reputation: 1152
You have used backward slash in the require module statement, that won't work. The correct way is to use forward slash like this:
var counter = require("./count");
The backward slash works as an escape character.
Upvotes: 1