node.js cannot find a module in the same folder

Nano

Im trying to use a simple "colors" module to set cli-colors in my logs, nothing special.

Well, i have a module called colors.js in the path ./app/config/colors.js, the content:

var clc = require('cli-color');

var colors = {
  ok: clc.cyan,
  error: clc.red.bold,
  warn: clc.yellowBright,
  high: clc.white.bgGreen
};

module.exports = colors;

Simple. Well, when i require it in the server.js (at the root of the project, above of /app) it works fine, but, when i try to use it in the ./app/config/db.js it throws me an error:

Error: Cannot find module './app/config/colors.js'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/nano/Dev/bears-api/app/config/db.js:3:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
14 Sep 10:21:00 - [nodemon] app crashed - waiting for file changes before starting...

Why if it works in the server.js?

soulcheck

You probably required the module using a relative path.

Relative paths are resolved in relation to the requiring module's location.

Quoting docs

A module prefixed with './' is relative to the file calling require(). That is, circle.js must be in the same directory as foo.js for require('./circle') to find it.

So if you did a

var whatever = require('./app/config/colors.js');

inside a module located in ./app/config/ then node will look for ./app/config/app/config/colors.js and fail.

If both requiring and required module are in the same directory just use:

var whatever = require('./colors.js');

or even shorter:

var whatever = require('./colors');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeScript cannot find js module in same folder

From Dev

Node.js: Cannot find module 'chai'

From Dev

Node.js cannot find an installed module

From Dev

Node js and phantomjs - Cannot find module 'weak'

From Dev

Node js and phantomjs - Cannot find module 'weak'

From Dev

cannot find node.js module

From Dev

Cannot find module './*.env' node js

From Dev

cannot find module chai though it exists in node modules folder

From Dev

Cannot Find Module in the models folder

From Java

Node.js Error: Cannot find module express

From Dev

Seriously debugging node.js 'Cannot find module xyz/abcd'

From Dev

Node.js Error: Cannot find module 'request'

From Dev

node.js Cannot find module './lib/compat'

From Dev

"Error: Cannot find module 'pkginfo'" - node js app deploying to heroku

From Dev

Cannot find module 'extend' Lambda node JS in AWS

From Dev

How to fix Node.js error: Cannot find module 'immer'

From Dev

Node.js - Error: Cannot find module 'config/chgpass'

From Dev

Seriously debugging node.js 'Cannot find module xyz/abcd'

From Dev

Error: Cannot find module 'html' with Node.JS and Express

From Dev

Node.js error: Cannot find module 'firebase/storage'

From Dev

Node.js Express Error: Cannot find module

From Dev

Node.js - "Cannot find module", when the module call other module

From Dev

Cannot find TypeScript library module under a folder

From Java

Python can't find module in the same folder

From Dev

Node.JS cannot find module 'xml2js' (Windows)

From Java

ERROR in Cannot find module 'node-sass'

From Dev

Heroku + Node: Cannot find module error

From Dev

Cannot find module 'node-gcm'

From Dev

Cannot find module 'node_modules/formidable'

Related Related

HotTag

Archive