How do you share constants in NodeJS modules?

前端 未结 13 1459
庸人自扰
庸人自扰 2020-12-12 08:56

Currently I\'m doing this:

foo.js

const FOO = 5;

module.exports = {
    FOO: FOO
};

And using it in bar.js:<

13条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 09:04

    I recommend doing it with webpack (assumes you're using webpack).

    Defining constants is as simple as setting the webpack config file:

    var webpack = require('webpack');
    module.exports = {
        plugins: [
            new webpack.DefinePlugin({
                'APP_ENV': '"dev"',
                'process.env': {
                    'NODE_ENV': '"development"'
                }
            })
        ],    
    };
    

    This way you define them outside your source, and they will be available in all your files.

提交回复
热议问题