NodeJs require function not working - node.js

I'm trying to include custom modules in my nodejs application. the problem that I'm facing is that whenever I try to include custom modules it says that it can't find that module.
Error: Cannot find module './queue/queue'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/contrive/tasweel/main.js :5:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
I googled to find some solution but none of them worked. I transfered my project in another pc and there it was not throwing the error. After this I thought maybe there is some problem with node. So I uninstalled it and installed it again but it is still throwing me the same error.
Node Version: v6.9.1
Npm Version: 3.10.8
The way I'm trying to include the module is as followed below
var express = require('express');
var firebase = require("firebase");
var Queue = require('firebase-queue');
var queue = require('./queue/index'); // custom module (throwing error can't find module)
var queue = require('./index'); // custom module (throwing error can't find module)
var app = express();
OS: Ubuntu 16.04 64 bit
Folder structure:
-node-modules
-queue
-index.js
-worker.js
-main.js // entry point for node application
Please tell me why am I facing this error. Thanks

Related

Serverless Node TypeScript app can't import module

I'm tired of this error! This is Serverless app with Lambda in AWS. Locally it works perfectly. Testing locally didn't work for load of other reasons. I'm testing in staging. Yet I'm getting this error:
Unable to import module 'src/engine/handler': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous>(/var/task/src/engine/controllers/image.js:43:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I followed all the steps advised elsewhere including: moving all dependencies to dependencies (out of dev), installing serverless and all. I'm using Node 6.10.3 and TypeScript 3.1. Can you help?
If you're trying host your nodejs projects which has so many dependencies not related to aws services inside, you better zip your projects in S3 first then import lambda from S3.

Cannot find module (a custom module)

I've following folder structure.
I am trying to access my custom module (core_programming/Constants.js) in other files.
I can access it in routes/index.js without any issue using following code.
var Constants = require('../core_programming/Constants.js');
But I am getting error when I try to access it inside core_programming/User.js with following statement.
var Constants = require('Constants.js');
It gives following error:
module.js:338
throw err;
^
Error: Cannot find module 'Constants.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (D:\nodeJsProjects\AutomateBuild\core_programming\User.js:3:18)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
1 Oct 11:56:35 - [nodemon] app crashed - waiting for file changes before starting...
I've tried different ways for defining path in require like ../core_programming/Constants.js and ./core_programming/Constants.js but nothing works out.
What is the correct way for loading custom modules from the same directory.
And, I am on Windows if that helps.
Try to use:
var Constants = require('./Constants.js');
This will force Node to figure out you are looking for a relative path and not a package in node_modules.
On a side note, windows paths use \, so consider trying it as well:
var Constants = require('.\Constants.js');

Mongoose: Error cannot find module debug

I'm building a basic MEAN webapp and am new to the stack. I have the front end running, but as soon as I add the following lines to app.js:
var mongoose = require('mongoose');
require('./models/test');
mongoose.connect('mongodb://localhost:3000/design-data-test');
I get the following error in terminal:
Error: Cannot find module 'debug'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/username/node_modules/mongoose/node_modules/mquery/lib/mquery.js:11:13)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
And all of my front end code stops running. Mongodb is running on the default port.
How would I go about resolving this error?
For future visitors: You are probably missing a dependency. Make sure you run this first:
npm install
... before you run your app with npm start or node <app>
I think this may happen if you have a child dependency on debug through another package (for example express or mongoose) but you did not provide the dependencies' package.json files with the deployed application which makes node.js unable to locate debug.

deployed Nodejs REST API on AWS using elastic beanstalk - Error cannot find custom module

I wrote my first nodejs application.
It works fine on my macbook but when I try to deploy it on AWS elastic beanstalk,I get below error .
/var/log/nodejs/nodejs.log
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
module.js:338
throw err;
^
Error: Cannot find module '**./routes/userprofile**'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/var/app/current/main.js:7:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
It seems nodejs is not able to resolve the path for my custom module
userprofile which resides under Routes directory.I have tried moving userprofile.js to the root, but it can still not resolve that.
This is the code in main.js which loads all the modules
/ Load Our Modules
var express = require('express');
var bodyParser = require('body-parser');
var userprofile = require('./routes/userprofile');
Problem it looks like is at this line require('./routes/userprofile')
Does anyone have an idea , what I could be doing wrong?
Would appreciate any help.
Thanks.

node.js cannot find a module in the same folder

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?
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');
The module should be in the "node_modules" folder to access it like you have described.

Resources