admin管理员组文章数量:1396763
What are my options for sharing constants between different files with the same NodeJS project?
I'm following, /
$ 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 mon log facility using this package,
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 mon 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 monJS just for this.
What are my options for sharing constants between different files with the same NodeJS project?
I'm following, https://www.reddit./r/javascript/ments/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 mon log facility using this package, https://www.npmjs./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 mon 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 monJS just for this.
Share Improve this question edited Jul 15, 2018 at 22:57 xpt asked Jul 15, 2018 at 22:33 xptxpt 23.2k45 gold badges154 silver badges245 bronze badges 4-
Node doesn't support
import
andexport
syntax (from ES6 modules) by default yet. If you're using Node 10, you can rename your file to have the extensionmjs
and execute vianode foobar.mjs --experimental-modules
. – Andrew Li Commented Jul 15, 2018 at 22:35 - @Jankapunkt No, the OP is correct. – Andrew Li Commented Jul 15, 2018 at 22:36
- OK. Let me remove ES6 modules from the question as focus on JS instead then... and ... UPDATED. – xpt Commented Jul 15, 2018 at 22:40
-
@xpt Maybe use a singleton for
brolog
? i.e. have one file that creates an instance of the logger, then exports it so that you can import it everywhere and use it? – Andrew Li Commented Jul 15, 2018 at 22:56
2 Answers
Reset to default 6A mon 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.:
mon.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 mon constant and/or utility function or class:
app.js:
const mon = require('mon')
if (mon.logLevel) {
mon.log.level(mon.logLevel)
mon.log.verbose('Config', 'Log Level set to %s', mon.logLevel, mon.log.level())
}
And of course you can and it's monly encouraged to simplify your utility functions to make them more convenient to use (but no simpler, than necessary):
more customized mon.js:
'use strict'
const logger = new brolog.Brolog()
module.exports = {
/*
... constants ...
*/
// more describing
isLogging: process.env['MY_LOG'],
// shorthand for leveling
level: mon.log.level,
// shorthand for verbose logging
verbose: mon.log.verbose,
// shorthand for warn logging
warn: mon.log.warn,
// shorthand for error logging
error: mon.log.error
}
using in app.js:
const mon = require('mon')
if (mon.isLogging) {
mon.verbose('...')
mon.warn('...')
mon.error('...')
}
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())
}
Footnote
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!
本文标签: javascriptSharing common constants and log function in separate filesStack Overflow
版权声明:本文标题:javascript - Sharing common constants and log function in separate files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744143426a2592721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论