I have a node server. This is what my init file looks like:
require("variables");
require("constants");
require("index");
app.get('/', function (req, res, next) {
res.sendFile(__dirname + '/index.html');
});
httpServer.listen(port, () => {
console.log(`server running`);
});
My variables file looks like this:
file = "file.json";
dataopen = "stuff";
userdata = "user";
module.exports={file,dataopen,userdata}
whereas my constants file:
const button = "<div></div>";
const title = "Page Title";
const input = "submit";
module.exports={button,title,input}
The thing here is that if I call for a variable inside index.js it will read it just fine:
console.log(dataopen); //it will output 'stuff'
but not so with any constants which will output undefined unless I include this line on top:
const { button,title,input } = require("constants");
Why is that? Why do I need the extra line for constants but not for variables as well? I also noticed that by removing the prefix const
before declaring then I won't need the line, but why is that?