I have a simple js file:
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
// Set bodyparser
app.use(bodyParser.urlencoded({ extended : true }));
app.use(express.static('public'));
// Set ejs
app.set('view engine', 'ejs');
app.get("/", (req, res)=>{
console.log("an incoming request...");
res.render("index");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, ()=>{
console.log("Listening...");
});
When I try to execute it with node server.js, this appears:
SyntaxError: Invalid or unexpected token
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
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)
at internal/main/run_main_module.js:17:47
and with npm start this happens:
> practice#1.0.0 start C:\Users\Youssef\Documents\Practice
> node server.js
C:\Users\Youssef\Documents\Practice\server.js:1
��c
SyntaxError: Invalid or unexpected token
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
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)
at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! practice#1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the practice#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Youssef\AppData\Roaming\npm-cache\_logs\2020-12-31T21_42_52_634Z-debug.log
I don't understand why this happens, there isn't a syntax error in the code and i tested removing the get route.
I cleaned npm cache, reinstalled node modules like in other stackoverflow questions: How to solve npm error "npm ERR! code ELIFECYCLE"
I even reinstalled nodejs and no good.
Thanks in advance.
Related
When I start my project via Heroku I am getting an error saying that "app" is not defined. I have logged "app" and get data back so I do not know why this is happening. Has anyone had this issue before? Here are the errors and the code:
State changed from starting to crashed
2021-05-05T22:16:42.626715+00:00 app[web.1]: /app/listen.js:3
2021-05-05T22:16:42.626744+00:00 app[web.1]: app.listen(PORT, () => console.log(`Listening on ${PORT}...`));
2021-05-05T22:16:42.626745+00:00 app[web.1]: ^
2021-05-05T22:16:42.626745+00:00 app[web.1]:
2021-05-05T22:16:42.626751+00:00 app[web.1]: ReferenceError: app is not defined
2021-05-05T22:16:42.626752+00:00 app[web.1]: at Object.<anonymous> (/app/listen.js:3:1)
2021-05-05T22:16:42.626752+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)
2021-05-05T22:16:42.626758+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-05-05T22:16:42.626758+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-05-05T22:16:42.626759+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-05-05T22:16:42.626764+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
2021-05-05T22:16:42.626765+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-05-05T22:16:42.652119+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-05-05T22:16:42.652547+00:00 app[web.1]: npm ERR! errno 1
2021-05-05T22:16:42.661700+00:00 app[web.1]: npm ERR! be-nc-news#1.0.0 start: `node listen.js`
2021-05-05T22:16:42.661926+00:00 app[web.1]: npm ERR! Exit status 1
2021-05-05T22:16:42.662168+00:00 app[web.1]: npm ERR!
2021-05-05T22:16:42.662387+00:00 app[web.1]: npm ERR! Failed at the be-nc-news#1.0.0 start script.
2021-05-05T22:16:42.662587+00:00 app[web.1]: npm ERR! This is probably not a problem with npm.
app.js:
const express = require("express");
const apiRouter = require("./routes/apiRouter");
const app = express();
const {
handlePsqlErrors,
handleServerErrors,
} = require("./errorHandlingFunctions/errorFunctions");
app.use(express.json());
app.use("/api", apiRouter);
app.use(handlePsqlErrors);
app.use(handleServerErrors);
module.exports = app;
listen.js:
const app = require("./app");
console.log(app);
const { PORT = 9090 } = process.env;
app.listen(PORT, () => console.log(`Listening on port ${PORT}...`));
I've been trying to import a js file in the same folder using require but I keep getting this error:
Error: Cannot find module 'player'
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:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/Users/shaham/Code/Typersion/server/items.js:1:14)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789: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)
Here's the code:
// server.js
var Player = require("./player")
//player.js
A bunch of functions...
module.exports = {
functionNames...
}
File architecture:
server/
-server.js
-player.js
I even get this error when trying to run node player.js
What am I doing wrong?
If you want a deeper look: here's the repo: https://github.com/ShahamNoorani/Typersion (you'll be looking for the refactoring branch).
I've tried to launch your code and there's my error message:
user#user ~/git/Typersion (git)-[refactoring] % npm start
> typersion#1.0.0 start /home/user/git/Typersion
> node server/server.js
internal/modules/cjs/loader.js:1023
throw err;
^
Error: Cannot find module 'player'
Require stack:
- /home/user/git/Typersion/server/items.js
- /home/user/git/Typersion/server/enemy.js
- /home/user/git/Typersion/server/server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1020:15)
at Function.Module._load (internal/modules/cjs/loader.js:890:27)
at Module.require (internal/modules/cjs/loader.js:1080:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/user/git/Typersion/server/items.js:1:14)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
at Module.load (internal/modules/cjs/loader.js:1040:32)
at Function.Module._load (internal/modules/cjs/loader.js:929:14)
at Module.require (internal/modules/cjs/loader.js:1080:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/user/git/Typersion/server/items.js',
'/home/user/git/Typersion/server/enemy.js',
'/home/user/git/Typersion/server/server.js'
]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! typersion#1.0.0 start: `node server/server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the typersion#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/user/.npm/_logs/2020-05-06T21_25_55_323Z-debug.log
It clearly says that there is a problem in the server/items.js file, even the lineno and colno is specified, so basically by looking into that file I can tell you forgot to add ./ before your imprt.
TLDR
change var Player = require("player") to var Player = require("./player") in server/items.js
I just got this error in my terminal - can someone explain me where I should be looking for the error? I have no idea where I should be looking...
charl#DESKTOP-PDIRO0T MINGW64 ~/Desktop/IRONHACK/Week-9/finalproject/api (master)
$ npm start
> api#0.0.0 start C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api
> node ./bin/www
C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api\node_modules\mongoose\lib\connection.js:579
throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
^
Error [MongooseError]: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
at new MongooseError (C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api\node_modules\mongoose\lib\error\mongooseError.js:10:11)
at NativeConnection.Connection.openUri (C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api\node_modules\mongoose\lib\connection.js:579:11)
at Mongoose.connect (C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api\app.js:22:4)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\charl\Desktop\IRONHACK\Week-9\finalproject\api\bin\www:7:11)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
message: 'The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.',
name: 'MongooseError'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api#0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\charl\AppData\Roaming\npm-cache\_logs\2020-05-09T14_44_59_447Z-debug.log
So I'm trying to deploy my vue app and for testing I want to switch my port 80 so I can see links without the :8080 extension. When I do this I get a fatal error
> node build/dev-server.js
> Starting dev server...
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Server.setupListenHandle [as _listen2] (net.js:1343:19)
at listenInCluster (net.js:1401:12)
at Server.listen (net.js:1485:7)
at Function.listen (/Users/Maceto/Desktop/sts/StsSnag/client/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/Users/Maceto/Desktop/sts/StsSnag/client/build/dev-server.js:85:18)
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)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! client#1.0.0 start: `node build/dev-server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the client#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/Maceto/.npm/_logs/2019-02-28T21_07_27_188Z-debug.log
I'm not sure what I'm doing wrong I changed the port in the config file but I get this. This also only happens on port 80.
You have to either do
sudo node build/dev-server.js
or
sudo setcap 'cap_net_bind_service=+ep' /path/to/nodejs
and then you'll be able to do it without sudo
I am following this https://developers.facebook.com/docs/messenger-platform/quickstart tutorial to add a web hook.
It's written to use this app https://github.com/fbsamples/messenger-platform-samples
that app is a node js app.
in the app.js, there is this:
const
bodyParser = require('body-parser'),
config = require('config'),
crypto = require('crypto'),
express = require('express'),
https = require('https'),
request = require('request');
when i run the app like this:
node app.js
i get the following error:
module.js:327
throw err;
^
Error: Cannot find module 'body-parser'
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> (C:\Program Files\nodejs\node_modules\npm\MyApps\messe
nger-platform-samples-master\node\app.js:14:16)
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)
how can I solve it please ?
Open terminal (or windows command line), go to messenger-platform-samples/node directory and install messenger-platform-samples by running this command:
npm install
after that you can run the app:
node app.js
First run npm install and this will rectify all errors
Run this command first
npm install -g body-parser
then create const bodyParser = require('body-parser')