The process
module in NodeJS
doesn’t seem to be global. In other words, the changes I make to it in one module don’t show up in other modules.
I wrote a small piece of code to confirm demo my findings. Here is the code:
server.js
import app from "./app.js";
process.on("uncaughtException", (err) => {
console.log("UNCAUGHT EXCEPTION: shutting down ...");
console.log(err.name, err.message);
process.exit(1);
});
// Server
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
app.js
import express from 'express'
const app = express();
console.log(y)
export default app;
When I run the code with node server.js
, I get a following error:
file:///tmp/node-test/app.js:5
console.log(y);
^
ReferenceError: y is not defined
at file:///tmp/node-test/app.js:5:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
However when I move a line console.log(y)
into server.js
I get an error that looks like this:
UNCAUGHT EXCEPTION: shutting down ...
ReferenceError y is not defined
Which is what I expected to see when console.log(y)
was in app.js
.
It looks like changes I made in server.js
with respect to process don’t show up in app.js
. I tried to find an answer online, but came up empty. My search foo wasn’t good enough.
Does anyone know what’s going on and how do I fix it? Because of coding standards I cannot use require
, I must use import