-
Notifications
You must be signed in to change notification settings - Fork 2
Defining lme globally
🦀 Not officially recommended.
Normally, if you want to use lme
in a file, you have to first require
it in the top of the same file.
const lme = require('lme');
...
But we don't have to require(console);
to use console.log
in any file. To get a similar experience with lme
, you can define it globally. (am I polluting the namespace?)
Define lme
by require
ing it at the top of the very first file in which node starts execution.
For example, to start my application, I use the following command.
$ node server.js
Then my server.js
file goes like this (Note: should not use var
/ let
/ const
to define it. Simply require
it):
lme = require('lme');
By doing this, we're exposing lme
to the global namespace. So it will be available to every file afterwards inside the application.
greet.js
function greet(name){
lme.s("hi " + name + "!");
}
module.exports = greet;
app.js
// globally define lme
lme = require('lme');
// get greet function
const greet = require('./greet');
let name = "Divya"
greet(name);
and run
$ node app.js
Strictly speaking, you are polluting the global namespace. But it can also be seen in a way that you are harnessing a JavaScript feature.
- How to change color of texts or lines?
- How to adjust level of logging?
- I'm in a project team. Every people uses different color schemes. How to manage it? Add
lmeconfig.json
to.gitignore
file.