ReferenceError: require is not defined (node.js) - node.js

I'm trying to run a node.js script from my node.js server. The problem is that the modules I import with require () do not work anymore while all the modules and files are in the same folder.
var mysql = require('mysql');
^
ReferenceError: require is not defined
at evalmachine.<anonymous>:3:13
at ContextifyScript.Script.runInThisContext (vm.js:25:33)
at Object.runInThisContext (vm.js:97:38)
at Object.<anonymous> (D:\wamp\www\node_server\test_server.js:8:14)
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)
at Module.runMain (module.js:604:10)
Thank you in advance,

I had a similar problem - server-side nodejs thinking that require is not defined.
It turns out the line "type": "module" in my package.json was causing this error, and once I removed that require worked as expected.

Related

Including node modules in AWS Lambda

I am trying to create a nodejs lambda function with the web3 module.
However, when zipping my handler.js and the entire node_modules directory and testing the lambda, the function cannot find web3.
Structure of the zip file:
handler.js
node_modules/
-web3_module_1/
-web3_module_2/
-other_modules_used_by_web3/
According to this source, the zip file structure should be the following:
handler.js
web3_module_1/
web3_module_2/
other_module/
But even with this structure, I get an error:
Unable to import module 'handler': Error
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/handler.js:3:14)
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)
What is the correct way to achieve this?

throw new TypeError('The super constructor to "inherits" must not ' +

I am trying to build my nodejs application. but i am always getting this error.
util.js:988
throw new TypeError('The super constructor to "inherits" must not ' +
^
TypeError: The super constructor to "inherits" must not be null or undefined
at Object.inherits (util.js:988:11)
at Object.<anonymous> (D:\CIDE-Backend\CIDE-BACKEND\node_modules\zetta\node_modules\spdy\lib\spdy\connection.js:86:6)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Module.require (module.js:517:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (D:\CIDE-Backend\CIDE-BACKEND\node_modules\zetta\node_modules\spdy\lib\spdy.js:20:19)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Module.require (module.js:517:17)
[nodemon] app crashed - waiting for file changes before starting...
i followed this solution but still i am getting same error.
my node version is 8.4.0
my npm version is 5.5.1
Can you tell me how can i solve this error.?
Your dependency zetta is including an ancient version of spdy which is not compatible with you current version of Node. There is currently an open issue on zetta's github about this, which has a work around:
process.EventEmitter = require('events').EventEmitter
const zetta = require('zetta')
Your mileage may vary on that hack, I have not tested it myself. Alternatively, you could wait for the zetta team to fix their dependency or downgrade your node installation.

Node.js SteamBot not working

my Node.js Steam-Bot isnt working. When i try starting him it always gives a Syntax Error:
/root/bot/node_modules/steam-tradeoffer-manager/lib/index.js:3
const SteamCommunity = require('steamcommunity');
^^^^^
SyntaxError: Use of const in strict mode.
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
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)
at Object.<anonymous> (/root/bot/tradebot.js:20:25)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
Iam using Debian 8, Node Version 0.12.
Thank you!
The error says it all. Replace const with var.
Also, you should update your nodeJS version as mentioned in the comments. This is really important.

Type Error: require(...) is not a function in SendGrid NodeJS

I am trying to send emails using the following doc on sendrid:
https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/nodejs.html
My app.js looks like this:
var sendgrid = require('sendgrid')('SENDGRID_APIKEY');
When runnig npm start I get the following error:
var sendgrid = require('sendgrid')(conf);
^
TypeError: require(...) is not a function
at Object.<anonymous> (C:\Users\Grandullon\Desktop\angular\app.js:8:36)
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\Grandullon\Desktop\angular\bin\www:7:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
Any ideas what I am doing wrong? conf is a variable where I store my API key.
I am using a clean express installation.
THANKS
My package.json can be looked here as requested:
http://pastebin.com/pGKWFReX
The latest documentation shows a different structure for initialization.
You should try doing
var sg = require('sendgrid').SendGrid(conf);

Code of nodejs using mongoose works on local machine but on VPS it doen't

This works on windows 7 localhost:
var User = mongoose.model(userDBName, userSchema);
But it gives this error for Debian VPS:
/home/node_modules/mongoose/lib/utils.js:28
return pluralize(name.toLowerCase());
^
TypeError: Cannot call method 'toLowerCase' of undefined
at exports.toCollectionName (/home/node_modules/mongoose/lib/utils.js:28:25)
at Mongoose.model (/home/node_modules/mongoose/lib/index.js:361:46)
at Object.<anonymous> (/home/XXX/nodejs/js/dbbase.js:54:21)
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)
at require (module.js:380:17)
at Object.<anonymous> (/home/XXX/nodejs/js/db_read.js:1:76)
How to fix it?
The error was the name is really undefined, due to userDBName is undefined. It relative with 'nconf' and config.json. Just incorrect way to this file.

Resources