Reputation: 23054
What are my options for sharing constants between different files with the same NodeJS project?
I'm following, https://www.reddit.com/r/javascript/comments/3bo42p/sharing_constants_in_es6_modules/
$ cat constants.js
const ALABAMA = 1;
const ALASKA = 3;
const ARIZONA =4;
UPDATE, Other details removed as it is not a good example for me to follow for my specific question.
Also, along the same line,
I want to have the common log facility using this package, https://www.npmjs.com/package/brolog
However, I found initializing it in index.js
file is not enough for all other files to use it.
const brolog = require('brolog')
const log = new brolog.Brolog()
const logLevel = process.env['MY_LOG']
if (logLevel) {
log.level(logLevel)
log.verbose('Config', 'Log Level set to %s', logLevel, log.level())
}
I don't want to repeat all above setup among all my NodeJS files.
So, how to sharing common constants and log functionality between different files within the same NodeJS project?
PS. This is only a small & simple project, and I don't want to pull-in/use big npm modules like commonJS just for this.
Upvotes: 1
Views: 4193
Reputation: 1622
You can use globals to share objects and constants across an entire NodeJS project.
constants.js
const brolog = require("brolog");
global.log = new brolog.Brolog();
global.logLevel = process.env['MY_LOG'];
index.js
require("./constants");
require("./other");
other.js
if (logLevel) {
log.level(logLevel)
log.verbose('Config', 'Log Level set to %s', logLevel, log.level())
}
This approach can cause problems, as it pollutes the global namespace. It is one of the simplest approaches to solve your problem, but it's worth creating global constants in this way as sparingly as possible. It makes your code harder to test, maintain, reuse, and generally reason about. As long as you know what you're doing, you'll be fine!
Upvotes: 3
Reputation: 4124
A common pattern for your app-wide constants and utility functions in Node.js is to create a module for them, instantiate/setup whatever you need and use that module where you need, e.g.:
common.js:
'use strict'
module.exports = {
/**
* The const of Alabama.
*
* @const {number}
*/
ALABAMA: 1,
/**
* The const of Alaska.
*
* @const {number}
*/
ALASKA: 3,
/**
* The const of Arizona.
*
* @const {number}
*/
ARIZONA: 4,
logLevel: process.env['MY_LOG'],
log: new brolog.Brolog()
}
Then require
it wherever you need to use a common constant and/or utility function or class:
app.js:
const common = require('common')
if (common.logLevel) {
common.log.level(common.logLevel)
common.log.verbose('Config', 'Log Level set to %s', common.logLevel, common.log.level())
}
And of course you can and it's commonly encouraged to simplify your utility functions to make them more convenient to use (but no simpler, than necessary):
more customized common.js:
'use strict'
const logger = new brolog.Brolog()
module.exports = {
/*
... constants ...
*/
// more describing
isLogging: process.env['MY_LOG'],
// shorthand for leveling
level: common.log.level,
// shorthand for verbose logging
verbose: common.log.verbose,
// shorthand for warn logging
warn: common.log.warn,
// shorthand for error logging
error: common.log.error
}
using in app.js:
const common = require('common')
if (common.isLogging) {
common.verbose('...')
common.warn('...')
common.error('...')
}
Upvotes: 6