Server is not starting or connecting when using Nodemon - node.js

I just setup a new project using Express, Node and Nodemon. I setup the basics of the file to make sure it is starting but when I'm using npm start in the terminal it appears to start but it doesn't get a message of where the server has been started and when checking the localhost it is not connected. Not sure what the issue is.
below is my package.json
{
"name": "resturant-picker",
"version": "1.0.0",
"description": "Resturant picker app for Alchs",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/connordensmore/resturant-picker.git"
},
"keywords": [
"crud",
"mongodb"
],
"author": "Connor",
"license": "ISC",
"bugs": {
"url": "https://github.com/connordensmore/resturant-picker/issues"
},
"homepage": "https://github.com/connordensmore/resturant-picker#readme",
"dependencies": {
"axios": "^0.27.2",
"body-parser": "^1.20.0",
"dotenv": "^16.0.2",
"ejs": "^3.1.8",
"express": "^4.18.1",
"mongoose": "^6.5.5",
"morgan": "^1.10.0",
"node": "^18.8.0",
"nodemon": "^2.0.19"
}
}
below is my server.js
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
app.get("/", (req, res) => {
res.send("Crud App");
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
});
when i run npm start in the terminal this is the response and then nothing happens or is started from what i can tell.
mopdog#Connors-MacBook-Pro resturant-picker % npm start
> resturant-picker#1.0.0 start
> nodemon server.js
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
[nodemon] clean exit - waiting for changes before restart

You never run listen because it's inside another function.
app.get("/", (req, res) => {
res.send("Crud App");
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

Related

Error deploying Mern Stack application on Heroku

I am trying to deploy a full-stack app on heroku, i have tried couple of things but none of them worked... when i entered "heroku logs" to follow up with the errors it should me code: 'MODULE_NOT_FOUND even though the node_module's can be found in both the Root of the project and the Frontend file as well , how do i fix this error and where does it come from ?
Here's a picture of my projects structure:
server.js:
require('dotenv').config();
const path = require('path');
const express = require("express");
const productRoutes = require("./routes/productRoutes");
const connectDB = require("./config/db");
connectDB();
const app = express();
app.use(express.json());
//--------------- deployment -------
app.use("/api/products", productRoutes);
if(process.env.NODE_ENV === "production") {
app.use(express.static(path.join('../frontend/build')));
app.get('*',(req,res) => {
res.sendFile(path.resolve(__dirname,'../frontend/build','index.html'))
})
} else {
app.get("/", (req, res) => {
res.send("Api running");
})
}
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
.env:
PORT=8080
MONGO_URI=*****
NODE_ENV=production
Package.json:
{
"name": "mern-shopping-build",
"version": "1.0.0",
"description": "Shopping list built with MERN stack",
"main": "server.js",
"engines": {
"node": "16.4.1",
"npm": "7.18.1"
},
"scripts": {
"build": "cd frontend && npm run dev",
"install-client": "cd frontend && npm install",
"heroku-postbuild": "npm run install-client && npm run build",
"start": "node server.js",
"server": "nodemon backend/server.js",
"client": "npm start && cd frontend",
"dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm start --prefix frontend\""
},
"keywords": [],
"author": "Karim ",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^5.13.2"
},
"devDependencies": {
"concurrently": "^6.2.0",
"nodemon": "^2.0.9"
}
}
ProcFile:
web node server.js
I Would appreciate any kind of feedback or help, Thank you!
A Procfile is not technically required to deploy simple apps written in most Heroku-supported languages—the platform automatically detects the language and creates a default web process type to boot the application server.
Just make sure to point your root package.json main key to backend/server.js.
Most likely it would work otherwise share your Build Logs to check it further.
{
"main": "backend/server.js"
}

Nodemon gives UnhandledPromiseRejectionWarning upon saving file

I'm using Windows and I've just installed nodemon 2.0.12 (added to path). Whenever I run a basic application it works it should until I save a file, then I receive the following error
My project is a basic express app:
const express = require('express')
const app = express()
const PORT = 3000
app.use("/", (req, res) => res.send('test'))
app.listen(PORT, function() {
console.log('Server started on port 3000')
})
My package.json is as follows
{
"name": "express.1",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"dev": "nodemon app.j"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.12"
}
}
What might be the reason for this?
Downgrading Nodemon to v2.0.7 seems to work. I don't understand why it broke in the first place.

nodejs server shows starting up but never starts

I just started learning nodejs and there is some weird error coming when I try to run nodemon server.js command.
Here is my server.js
const express = require("express");
const app = express();
const server = require("http").Server(app);
app.get("/", (req, res) => {
res.status(200).send("Hello World");
});
server.listen(3030);
My VScode terminal shows this but the server never starts.
Here is package.json
{
"name": "video-chat-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^3.1.6",
"express": "^4.17.1",
"peer": "^0.6.1",
"socket.io": "^4.1.2",
"uuid": "^8.3.2"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
You don't need to use http, express is already enough to start a server.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.status(200).send("Hello World");
});
app.listen(3030, ()=>{
console.log('Server is starting');
});

Nodemon throwing error: address already in use <port number> on M1 Mac

I'm trying to use nodemon on my macbook and whenever i run
nodemon <script.js>
It gives me this error:
address already in use <PORT NUMBER HERE>
Even if i'm running the script for the very first time. I've tried
npx kill-port <port>
But it doesn't work: it shows that a process was killed but when i try to run nodemon again, i get the same error.
Here's my package.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^10.0.0",
"express": "^4.17.1",
"nodemon": "^2.0.7"
}
}
My server.js:
const express = require("express");
const dotenv = require("dotenv");
const app = express();
dotenv.config();
const PORT = process.env.PORT;
app.get("/getRestaurants", (req, res) => {
console.log("Get All Restaurants");
});
app.listen(PORT, () => {
console.log(`Server is up on port ${PORT}`);
});
And my .env:
PORT = 4000;
What could be causing this? I'm on a M1 MBA with bigsur.

How to run ECMAScript (mjs files) with nodemon?

I am able to run mjs files with nodejs using --experimental-modules flag.
node --experimental-modules index.mjs
package.json:
{
"name": "mjs-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^2.4.2",
"uuid": "^3.3.2"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
And index.mjs
import http from 'http'
const server = http.createServer((req, res) => {
res.end('hello')
})
const PORT = 5000
server.listen(PORT, () => {
console.log(`🏃‍♀️ Server is running at http://localhost:${PORT}`)
})
But if I try to
npm run dev
or (with nodemon installed globally)
nodemon index.mjs
I get this error
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.mjs`
internal/modules/cjs/loader.js:821
throw new ERR_REQUIRE_ESM(filename);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
So, How I can enable support for ECMAScript in nodemon? Or should I use something like esm?
Offcourse yes, All you need to modify your package.json a bit
"scripts": {
"dev": "nodemon --experimental-modules index.mjs"
},

Resources