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();
Related
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.
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
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.
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
I am trying to make a request to the Twitter API and respond with the JSON.
This is my code.
var http = require('http');
var request = require('request');
var url = require('url');
app.get('/twwets/:username', function(req, response) {
var username = req.params.username;
options = {
protocol: "http:",
host: 'api.twitter.com',
pathname: '/1/statuses/user_timeline.json',
query: { screen_name: username, count: 10}
}
var twitterUrl = url.format(options);
request(twitterUrl).pipe(response);
}).listen(8080);
And this is what I get when I run node app.js in my terminal:
module.js:340
throw err;
^
Error: Cannot find module 'require'
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/geosh/node.js_practice/express_practice/app.js:2:15)
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)
I need to know what do I do wrong.
[My code may seem useless, but it's just for educational purposes. Please excuse any mistake.]
EDIT: I still get the same error after I saved and run the code in my shell.
Typo;
var request = require('require');
should most likely be
var request = require('request');
EDIT: You're also missing express, I suspect you'll want to add these lines;
var express = require('express');
var app = express();
...and npm install request express to get the modules installed.
Refer the following links may use
http://expressjs.com/
For Twitter Authentication
http://passportjs.org/