Error pointing to non-existent file when running Node.js app locally - node.js

I was just sent the files for a Node.js app (which is working without issues online) and I'm trying to set it up on my Windows machine.
I have Node server running, but I'm stuck trying to actually load the app. I can navigate to the directory with the Node command prompt, and there's an app.js (plus folders like config, components etc) in there which I've been trying to run, but I am getting the following error:
D:\my-directory\game-master>node app.js
module.js:327
throw err;
^
Error: Cannot find module 'config'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\posao\aktivni projekti\smart rebellion\game-master
\app.js:7:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
The thing is, there is no file titled module.js in the directory. I presume it's a built in Node.js file, but then how do I troubleshoot this?
My best reading of the error is that there should be a config.js file somewhere, but that doesn't make sense because these same exact files work with no hickups on a remote server.

If you want to require('./config'), where config is a directory, the file ./config/index.js should exist.
You can't require('config') however, because node.js will try to find it in node_modules.
Check out node.js modules documentation where the require algorithm is well explained.

Related

Errir with missing module

I have bot in Discord Bot Maker. Its working perfectly on my pc (Windows 10), but when I move it to my VPS (Ubuntu 16.04) and try to start it using node bot.js, I will get error with missing module.
Error:
module.js:550
throw err;
^
Error: Cannot find module './constants'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/justwolf/butter/node_modules/ws/lib/websocket.js:19:19)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
bot.js: https://hastebin.com/koredupoka.js
The error :
Error: Cannot find module './constants'
means that somewhere in your code you have
.... = require('./constants')
but the file constants.js doesn't exist or at least not where it's supposed to be. Maybe when you uploaded your bot script to your VPS you forgot to upload that file.
Sometimes, in ready-made scripts, configuration variables such as database credentials and stuff are stored in a file named constants.js and this file is not versionned (it has en entry in .gitignore). Maybe you have a file named constants.js.dist or constants.js.example. It means you need to edit it and rename it to constants.js

how can i fix this error that nodemon keep on showing

i following an express.js tutorial andd after some code and functionalities with api requests i followed exactly how he changed the code that handles these requests into a seperate js file in another folder just to clean up the code but after i did that this error showed up
i tried changing the file name , i tried removing the node-modules folder and running npm install again and i tried installing the module with the issue
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/routes/api/members'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\Get_Rico\Desktop\crashcoursse\index.js:11:25)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[nodemon] app crashed - waiting for file changes before starting...
i expect nodemon to save the changes and work just fine with no errors just like what happened in the tutorial
It appears the error is probably coming from this line of code and it's telling you that it can't find the '/routes/api/members' module file.
And, this would not be working because you're trying to load a local module without a proper path name. If no path is specified, then it tries to load a global or built-in module. If that module is in the current directory, then you should use the correct path:
const yourModule = './routes/api/members';

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.

How to use node-oracle with iisnode?

I'm having trouble trying to run a Node.js server with node-oracle via iisnode. The iisnode examples all work, and running the server [on a different port] in a Command Window works perfectly. The server setup is based upon "The Node Beginner Book" example (i.e. index.njs -> server.njs -> router.njs -> requesthandlers.njs). I renamed all of my node files as *.njs and currently have my web.config set up for iisnode to handle all files of that extension, with a rewrite rule to forward the incoming requests to index.njs. The problem occurs when node-oracle -- utilized within requesthandlers.njs -- attempts to call oracle_bindings.node:
Application has thrown an uncaught exception and is terminated:
Error: The specified module could not be found.
c:\Program Files\iisnode\www\Reports\node_modules\oracle\build\Release\oracle_bindings.node
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (c:\Program Files\iisnode\www\Reports\node_modules\oracle\lib\oracle.njs:2:16)
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)
I've given read/write/execute permissions to the IIS_IUSRS group on every folder imaginable within the .\iisnode\www\Reports\node_modules\oracle path. I've also attempted the solution in Node cannot find modules in IIS, can find from prompt to no avail. While I could probably just set the server up as a service, I'd prefer to have it through IIS so that I don't have to battle with CORS while supporting IE9. Any help is greatly appreciated, thanks!

Resources