I’m using VSCode and created a small vue / express / node.js project.
While running my server I noticed that when I close it using ctrl+c in the VSCode terminal (power shell), there is a red circle with a cross next to the initial command line that when hovered tells me the command exited with an exit code of 1.
My assumption is that it should exit with a code of 0. Here is my code, run with node server.js
import express from "express";
import path from "path";
import { fileURLToPath } from 'url';
import http from 'http';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Our express app
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '../dist')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '../dist/index.html'));
});
const server = http.createServer(app);
server.listen(8080, () => {
console.log("Listening on port 8080...");
});
function shutdown() {
console.log('Shutdown initiated...');
server.close((err) => {
if (err) {
console.error('Error during server close:', err);
process.exit(1);
} else {
console.log('Server closed successfully');
process.exit(0);
}
});
// Force shutdown after 5 seconds if not closed
setTimeout(() => {
console.error('Force shutting down after timeout...');
process.exit(1);
}, 5000);
}
// Handle termination signals
process.on('SIGTERM', () => {
console.log('Received SIGTERM');
shutdown();
});
process.on('SIGINT', () => {
console.log('Received SIGINT');
shutdown();
});
It logs:
Received SIGINT
Shutdown initiated...
Server closed successfully
When I run it in a terminal outside of VSCode, it just shows the above same logs but no error code mentioned.
Also, when, in VSCode, I add a Shutdown()
right after the log of listening
, it exits with a code of 0 properly.
I don’t have any other interaction with my server (like asking for files, opened DB connection or anything else). I just open it / close it with ctrl+c.
Could it be just a VSCode issue or am I missing something? I’m not sure what else I could try to narrow down the issue.
I also added a block of code to display exit code used:
process.on('exit', (code) => {
console.log(`Process exited with code: ${code}`);
});
And it displayed 0 both in VSCode (despite the red circle/arrow saying exit 1) and outside.
Mydi – StudioGaming is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.