node Linux/Windows compatibility issues - node.js

This Code works on Windows without any errors.
var express = require('express')
, app = express()
, async = require('async')
, bodyParser = require('body-parser')
, cookies = require('cookies')
, cors = require('cors')
, fileUpload = require('multer')
, fs = require('fs')
, moment = require('moment')
, morgan = require('morgan')
, path = require('path')
, session = require('express-session')
, upload = fileUpload({ dest: './uploads' })
, uuid = require('uuid')
;
var connLaw = require('./nodeHelperFunctions/datasources').service
, connPG = require('./nodeHelperFunctions/datasources').postgis
, headers = require('./helperHtml/headers')
, footers = require('./helperHtml/footers')
, passCheck = require('./nodeHelperFunctions/hashsalt')
;
However when the same code is run on an Amazon Linux AMI box, I get the following error.
module.js:471
throw err;
^
Error: Cannot find module './nodeHelperFunctions/datasources'
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/ec2-user/testbed/js/app.js:17:59)
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)
tried changing my code to
var connLaw = require(path.join(__dirname, 'nodeHelperFunctions/datasources')).service
, connPG = require(path.join(__dirname, 'nodeHelperFunctions/datasources')).postgis
, headers = require(path.join(__dirname, 'helperHtml/headers'))
, footers = require(path.join(__dirname, 'helperHtml/footers'))
, passCheck = require(path.join(__dirname, 'nodeHelperFunctions/hashsalt'))
;
again works perfectly in Windows but got a similar error on Linux:
module.js:471
throw err;
^
Error: Cannot find module '/home/ec2-user/testbed/js/nodeHelperFunctions/datasources'
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/ec2-user/testbed/js/app.js:17:15)
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)
how can I get person scripts I have written myself to work on Linux when they work perfecting fine on Windows?

It's hard to tell with what you posted - but requiring modules on linux is case sensitive to the file name, and windows... ...not so. Try checking the case.
edit: not just the filename - but the entire path.

Related

node.js module not found in subfolder

I am building a basic web scraper using node, express, and puppeteer, and when I try to run node index.js. I get this error
alexskreen#Alexs-MacBook-Air WOD-Scraper2 % node server/index.js
internal/modules/cjs/loader.js:960
throw err;
^
Error: Cannot find module './server/scrapers'
Require stack:
- /Users/alexskreen/Desktop/WOD-Scraper2/server/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
at Function.Module._load (internal/modules/cjs/loader.js:840:27)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/Users/alexskreen/Desktop/WOD-Scraper2/server/index.js:8:18)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/alexskreen/Desktop/WOD-Scraper2/server/index.js' ]
}
Before adding my require statements everything is working:
const express = require("express");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");
const scrapers = require('./server/scrapers');
const db = require('./server/db');
If module is in your subfolder you should use,
const something = require("./subfolder/module");
If it is not a subfolder module then make sure it is installed in node_modules.

cannot read property of 'get' undefined in the express router

My code is as shown below:
app.js
const app = express();
const itemsRouter = require('./routes/items.js');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/items', itemsRouter);
items.js
const router = require('express').router;
router.get('/itemTest', (req, res) => {
res.json({
sucess: true
});
console.log(`the parametrs are ${req.body.item_name} ${req.body.item_post}`);
});
module.exports = router;
But here somehow, I am not able to get the router working and it says that can not read property 'get' of undefined.
the error stack is as shown below:
TypeError: Cannot read property 'get' of undefined
at Object.<anonymous> (C:\Users\anand\quFlipApi\routes\items.js:5:7)
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.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\anand\quFlipApi\app.js:33:19)
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)
at run (bootstrap_node.js:394:7)
There are two problems here:
It's express.Router and not express.router.
You need to create a new instance of express.Router (e.g. var router = new express.Router()) instead of trying to using the constructor directly as an instance.

Cannot node app.js because 'Cannot read property 'MemoryStore' of undefined'

I'm using vagrant and virtualbox with precise32 for my setup. Installed Node.js v0.10 using:
curl -sL https://deb.nodesource.com/setup_0.10 | sudo bash -
sudo apt-get install -y nodejs
and i can't seem to sudo node the following:
var everyauth = require('everyauth');
var nconf = require('nconf');
var Recaptcha = require('recaptcha').Recaptcha;
var crypto = require('crypto');
var connect = require('connect');
var cookie = require('cookie');
var express = require('express');
var models = require('./models/');
var hash = require('./hash');
var config = require('./config');
/* load API keys */
nconf.env().file({file: 'settings.json'});
var session_store = new connect.session.MemoryStore();
I got the following error:
TypeError: Cannot read property 'MemoryStore' of undefined
at Object.<anonymous> (/vagrant/app/auth.js:16:40)
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> (/vagrant/app/app.js:8:12)
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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:945:3
at node.js:945:3
I have to say that 3 years ago when I made the project, I simply did 'sudo npm install' and the tried to run and everythin was fine. I am thinking there is a incompatibility issue between js and the newly installed npm.
Change
var connect = require('connect');
...
var session_store = new connect.session.MemoryStore();
to
let app = express();
let session = require('express-session');
app.use(session({ ... });
Or install old version (2.x, 3.x) of Express

Error while running js file via node

I was trying to run my koa app.js file but I could not get it to work.
Here's my app.js
var koa = require('koa'),
monk = require('monk'),
wrap = require('co-monk');
var db = monk("mongodb url here"),
collection = db.get('mycollection'),
transactions = wrap(collection);
var app = koa();
app.use(function*(){
var res = yield transactions.find({});
console.log(res);
});
app.listen(3000);
and here's the error i got:
TypeError: Cannot read property 'name' of undefined
at makeSkinClass (/home/ric/node_modules/mongoskin/lib/utils.js:33:43)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/grid.js:6:35)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/db.js:22:16)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/mongo_client.js:5:14)
I'm still new to Koa,nodejs so I'm having a hard time figuring this out. Any help would be great!

How avoid Error: Cannot find module ‘express’ [duplicate]

This question already has answers here:
can not find module express --on windows
(2 answers)
Closed 7 years ago.
First I installed express in this way: nmp install -g express
I check my Path variable and it is set "C:\Users\Eventi\AppData\Roaming\npm".
This is the code:
var http = require('http');
var fs=require('fs');
var express = require('express');
var app = express();
and this is the error:
Error: Cannot find module 'express'
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> (C:\Users\Eventi\Desktop\Node.js\Progetti\ProveNodeJS\NodeJSProve\HelloWord.js:7:15)
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 Function.Module.runMain (module.js:501:10)
Anyone can help me?
You have to "require" it in your application. Write var express = require('express'); then initialize express with var app = express();

Resources