目前我正在做这个:

foo.js

const FOO = 5;

module.exports = {
    FOO: FOO
};

并在bar.js中使用它:

var foo = require('foo');
foo.FOO; // 5

还有更好的办法吗?在exports对象中声明常量会让人感到尴尬。


当前回答

我发现Dominic建议的解决方案是最好的,但它仍然缺少“const”声明的一个特性。当你在JS中使用"const"关键字声明一个常量时,常量的存在是在解析时检查的,而不是在运行时。因此,如果你在后面的代码中拼写错了常量的名字,当你尝试启动node.js程序时,你会得到一个错误。这是一个更好的拼写错误检查。

如果您像Dominic建议的那样使用define()函数定义常量,那么即使拼写错误也不会得到错误,并且拼写错误的常量的值将是未定义的(这可能导致调试头痛)。

但我想这是我们能得到的最好的了。

此外,在constans.js中,这是对Dominic函数的改进:

global.define = function ( name, value, exportsObject )
{
    if ( !exportsObject )
    {
        if ( exports.exportsObject )
            exportsObject = exports.exportsObject;
        else 
            exportsObject = exports;        
    }

    Object.defineProperty( exportsObject, name, {
        'value': value,
        'enumerable': true,
        'writable': false,
    });
}

exports.exportObject = null;

通过这种方式,你可以在其他模块中使用define()函数,它允许你在constants.js模块中定义常量,也允许你在调用该函数的模块中定义常量。声明模块常量有两种方式(在script.js中)。

第一:

require( './constants.js' );

define( 'SOME_LOCAL_CONSTANT', "const value 1", this ); // constant in script.js
define( 'SOME_OTHER_LOCAL_CONSTANT', "const value 2", this ); // constant in script.js

define( 'CONSTANT_IN_CONSTANTS_MODULE', "const value x" ); // this is a constant in constants.js module

第二:

constants = require( './constants.js' );

// More convenient for setting a lot of constants inside the module
constants.exportsObject = this;
define( 'SOME_CONSTANT', "const value 1" ); // constant in script.js
define( 'SOME_OTHER_CONSTANT', "const value 2" ); // constant in script.js

同样,如果你想要define()函数只从constants模块调用(而不是膨胀全局对象),你可以在constants.js中这样定义它:

exports.define = function ( name, value, exportsObject )

并在script.js中像这样使用它:

constants.define( 'SOME_CONSTANT', "const value 1" );

其他回答

作为一种替代方法,您可以将“常量”值分组在一个局部对象中,并导出一个返回该对象的浅克隆的函数。

var constants = { FOO: "foo" }

module.exports = function() {
  return Object.assign({}, constants)
}

然后,如果有人重新分配FOO就无关紧要了,因为它只会影响他们的本地副本。

ES6路。

在foo.js中导出

const FOO = 'bar';
module.exports = {
  FOO
}

导入到bar.js中

const {FOO} = require('foo');

我不认为从模块中入侵GLOBAL空间是一个好的实践,但在某些情况下,可能严格需要实现它:

Object.defineProperty(global,'MYCONSTANT',{value:'foo',writable:false,configurable:false});

必须考虑到这种资源的影响。如果没有正确命名这些常量,重写已经定义的全局变量的风险是真实存在的。

在我看来,使用Object.freeze可以实现dry和更声明性的风格。我喜欢的模式是:

/ lib / constants.js

module.exports = Object.freeze({
    MY_CONSTANT: 'some value',
    ANOTHER_CONSTANT: 'another value'
});

/ lib / some-module.js

var constants = require('./constants');

console.log(constants.MY_CONSTANT); // 'some value'

constants.MY_CONSTANT = 'some other value';

console.log(constants.MY_CONSTANT); // 'some value'

我发现Dominic建议的解决方案是最好的,但它仍然缺少“const”声明的一个特性。当你在JS中使用"const"关键字声明一个常量时,常量的存在是在解析时检查的,而不是在运行时。因此,如果你在后面的代码中拼写错了常量的名字,当你尝试启动node.js程序时,你会得到一个错误。这是一个更好的拼写错误检查。

如果您像Dominic建议的那样使用define()函数定义常量,那么即使拼写错误也不会得到错误,并且拼写错误的常量的值将是未定义的(这可能导致调试头痛)。

但我想这是我们能得到的最好的了。

此外,在constans.js中,这是对Dominic函数的改进:

global.define = function ( name, value, exportsObject )
{
    if ( !exportsObject )
    {
        if ( exports.exportsObject )
            exportsObject = exports.exportsObject;
        else 
            exportsObject = exports;        
    }

    Object.defineProperty( exportsObject, name, {
        'value': value,
        'enumerable': true,
        'writable': false,
    });
}

exports.exportObject = null;

通过这种方式,你可以在其他模块中使用define()函数,它允许你在constants.js模块中定义常量,也允许你在调用该函数的模块中定义常量。声明模块常量有两种方式(在script.js中)。

第一:

require( './constants.js' );

define( 'SOME_LOCAL_CONSTANT', "const value 1", this ); // constant in script.js
define( 'SOME_OTHER_LOCAL_CONSTANT', "const value 2", this ); // constant in script.js

define( 'CONSTANT_IN_CONSTANTS_MODULE', "const value x" ); // this is a constant in constants.js module

第二:

constants = require( './constants.js' );

// More convenient for setting a lot of constants inside the module
constants.exportsObject = this;
define( 'SOME_CONSTANT', "const value 1" ); // constant in script.js
define( 'SOME_OTHER_CONSTANT', "const value 2" ); // constant in script.js

同样,如果你想要define()函数只从constants模块调用(而不是膨胀全局对象),你可以在constants.js中这样定义它:

exports.define = function ( name, value, exportsObject )

并在script.js中像这样使用它:

constants.define( 'SOME_CONSTANT', "const value 1" );