Node.js "module not found"

So, I want to require config.js which is located a directory below.

- config.js
- lib /
         - adduser.js

When I require it as ../config.js from lib/adduser.js, it returns this:

Error: Cannot find module '../config.js'
Require stack:
- /app/api/json/index.js
- /app/lib/adduser.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
    at Function.Module._load (internal/modules/cjs/loader.js:526:27)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/app/api/json/index.js:2:16)
    at Module._compile (internal/modules/cjs/loader.js:759:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Module.require (internal/modules/cjs/loader.js:666:19)

This is lib/adduser.js

const bcrypt = require('bcryptjs');
const config = require('../config.js');
const api = require('../api/' + config.apifolder + '/index.js');

// Run Checks and stuff
var args = process.argv.slice(2);
if (!args[0]){
  console.error('The Username is required!');
  process.exit(1)
} else if (!args[1]){
  console.error('The Password is required!');
  process.exit(1)
} else {

// Encrypt on Passing checks.
var salt = bcrypt.genSaltSync(10);

console.log(api.adduser(args[0], bcrypt.hashSync("password", salt)));
}

Try doing

const config = require(__dirname + “/config.js”) or remove .js if that didn’t work.

2 Likes

It didn’t work with either, but, I added “…/” to it and it worked! Thanks!

onst config = require(__dirname + "/../config.js")

Error: Cannot find module ‘array-flatten’
Require stack:

  • C:\Users\user1\Documents\mycelium1\node_modules\express\lib\router\route.js
  • C:\Users\user1\Documents\mycelium1\node_modules\express\lib\router\index.js
  • C:\Users\user1\Documents\mycelium1\node_modules\express\lib\application.js
  • C:\Users\user1\Documents\mycelium1\node_modules\express\lib\express.js
  • C:\Users\user1\Documents\mycelium1\node_modules\express\index.js
  • C:\Users\user1\Documents\mycelium1\app.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Module.Hook._require.Module.require (C:\Users\user1\AppData\Roaming\npm\node_modules\pm2\node_modules\require-in-the-middle\index.js:61:29)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object. (C:\Users\user1\Documents\mycelium1\node_modules\express\lib\router\route.js:17:15)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions…js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at Module.Hook._require.Module.require (C:\Users\user1\AppData\Roaming\npm\node_modules\pm2\node_modules\require-in-the-middle\index.js:80:39) {
    code: ‘MODULE_NOT_FOUND’,
    requireStack: [
    ‘C:\Users\user1\Documents\mycelium1\node_modules\express\lib\router\route.js’,
    ‘C:\Users\user1\Documents\mycelium1\node_modules\express\lib\router\index.js’,
    ‘C:\Users\user1\Documents\mycelium1\node_modules\express\lib\application.js’,
    ‘C:\Users\user1\Documents\mycelium1\node_modules\express\lib\express.js’,
    ‘C:\Users\user1\Documents\mycelium1\node_modules\express\index.js’,
    ‘C:\Users\user1\Documents\mycelium1\app.js’
    ]
    }

could anyone help me to solve this :slight_smile: pls

The error is caused by Node.js not being able to find a module (in this case, array-flatten)

npm install array-flatten --save should solve your problem