I have an Express.js server currently set up like this:
- index.js
const server = require('./server');
const port = process.env.PORT || 5000;
console.log("index.js is called")
server.listen(port, () =>
console.log(`Server is listening on http://localhost:${port}`)
);
- server.js
const express = require('express');
const routes = require('./routes');
const server = express();
server.use(express.json());
server.use(routes);
console.log("server.js is called")
module.exports = server;
- part of package.json
{ ...
"main": "index.js",
"scripts": {
"test": "jest",
"startDev": "nodemon index.js",
"start": "node server.js"
},
...
}
Last night I ran npm install --save-dev nodemon
and after an error message; npm install --save-dev dotenv
(we do have a .env
file), both in the root of my project directory. Afterwards I was able to use npm start
to successfully start the server, and nodemon worked properly (restarting upon crash). After git push
-ing to GitHub and opening a PR to merge with the main branch, a team member was forced to delete the server's package-lock.json
due to merge conflicts, and merged the branch. We figured this was ok since package-lock.json
will be rebuilt with npm install
. After git pull
-ing in my main
branch this morning, and running npm start
, I got only the following output:
$ npm start
app@1.0.0 start node server.js
$
Instead of the expected:
$ npm start
> app@1.0.0 start
> nodemon index.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 index.js`
Server is listening on http://localhost:5000
$
When I put a console.log()
statement in both index.js
and server.js
I only see the text from server.js
. Trying various solutions from this stackoverflow topic hasn't solved the issue. I've tried various other "start" commands like npm startDev
, npm server.js
, npm index.js
, node start
, nodemon
, nodemon start
, nodemon server.js
, nodemon server.js
, and likely some I'm omitting. I've tried changing "startDev"
to "node ./bin/www"
and "nodemon ./bin/www"
without luck.
Anyone have any ideas as to why the server isn't starting, or any troubleshooting suggestions? My teammate isn't having any issues on his Mac (I'm running Linux Mint 20.2).