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 and export syntax (from ES6 modules) by default yet. If you're using Node 10, you can rename your file to have the extension mjs and execute via node 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
Add a ment  | 

2 Answers 2

Reset to default 6

A 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