node.js module not found in subfolder - node.js

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.

Related

Node script "Cannot find module"

I have the following file structure:
/avro.js
/node_modules/...
/package.json
/package-lock.json
I have run:
npm install javro yargs
In my avro.js I do:
#!/usr/bin/env node
const { javro, SchemaRegistryAvroFetcher } = require('javro');
const yargs = require("yargs");
const options = yargs
.scriptName("javro")
.positional('inpath', {
type: 'string',
describe: 'path to JSON Schema file to convert'
})
.positional('outpath', {
type: 'string',
describe: 'path to save converted Avro Schema'
})
.help()
.argv;
When I try to run the script I get:
./avro.js --help
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'javro'
Require stack:
- /Users/anentropic/Documents/Dev/Personal/myproj/avro.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/Users/anentropic/Documents/Dev/Personal/myproj/avro.js:2:46)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/anentropic/Documents/Dev/Personal/myproj/avro.js' ]
}
Node.js isn't my first language, but I thought this should work?

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

I've been using Firebase Cloud Storage for my app, and so I required 'firebase/storage' in my server.js file, as shown below. The code was working fine last night, but when I tried "node server.js" this morning, I keep getting the module error:
Error: Cannot find module 'firebase/storage'
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> (/home/ubuntu/workspace/app.js:10:15)
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)
Here is a snippet of the server.js code:
var express = require('express');
var logger = require('morgan')
var bodyParser = require('body-parser')
var app = express();
var path = require('path');
var formidable = require('formidable');
var fs = require('fs');
var admin = require('firebase-admin')
var storage = require('firebase/storage')
I've initialized in the fireabase app and made correct references to the firebase storage in my html and js, or else my code would not have worked last night. Why am I getting this error now?
You should use this lib to use storage.
npm install firebase
https://firebase.google.com/docs/storage/web/start

node Linux/Windows compatibility issues

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.

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