Node.jsモジュールで定数を共有する
Node.jsモジュールで定数を共有する方法
モジュールスコープ変数を使用する
// my-module.js
const API_KEY = 'your_api_key';
module.exports = {
API_KEY,
// ...
};
// main.js
const myModule = require('./my-module');
console.log(myModule.API_KEY); // 'your_api_key'
利点:
- シンプルで分かりやすい
- モジュール内でのみ定数を公開・非公開の切り替えが容易
- モジュールごとに個別に定義する必要がある
- モジュール間で名前空間が衝突する可能性がある
共有オブジェクトを使用する
// constants.js
const constants = {
API_KEY: 'your_api_key',
// ...
};
module.exports = constants;
// my-module.js
const constants = require('./constants');
console.log(constants.API_KEY); // 'your_api_key'
- モジュール間で定数を一元管理できる
- 名前空間の衝突を回避できる
- 少し複雑なコードになる
環境変数を使用する
// .env
API_KEY=your_api_key
// my-module.js
const API_KEY = process.env.API_KEY;
console.log(API_KEY); // 'your_api_key'
- 環境によって定数を簡単に切り替えられる
- コードがシンプルになる
- 環境設定ファイルの管理が必要
クラスを使用する
// constants.js
class Constants {
static get API_KEY() {
return 'your_api_key';
}
// ...
}
module.exports = Constants;
// my-module.js
const Constants = require('./constants');
console.log(Constants.API_KEY); // 'your_api_key'
- オブジェクト指向的なコードを書ける
- 定数にメソッドを追加できる
どの方法を選択するかは、プロジェクトの規模や要件によって異なります。以下に、いくつかの指針をご紹介します。
- 小規模なプロジェクトでは、モジュールスコープ変数を使用するのが最も簡単です。
- モジュール間で定数を共有する必要がある場合は、共有オブジェクトを使用するのが良いでしょう。
- 環境によって定数を切り替えたい場合は、環境変数を使用するのが便利です。
- オブジェクト指向的なコードを書きたい場合は、クラスを使用するのが良いでしょう。
// my-module.js
const API_KEY = 'your_api_key';
function getAPIKey() {
return API_KEY;
}
module.exports = {
getAPIKey,
};
// main.js
const myModule = require('./my-module');
const apiKey = myModule.getAPIKey();
console.log(apiKey); // 'your_api_key'
// constants.js
const constants = {
API_KEY: 'your_api_key',
// ...
};
module.exports = constants;
// my-module.js
const constants = require('./constants');
console.log(constants.API_KEY); // 'your_api_key'
// .env
API_KEY=your_api_key
// my-module.js
const API_KEY = process.env.API_KEY;
console.log(API_KEY); // 'your_api_key'
// constants.js
class Constants {
static get API_KEY() {
return 'your_api_key';
}
// ...
}
module.exports = Constants;
// my-module.js
const Constants = require('./constants');
console.log(Constants.API_KEY); // 'your_api_key'
上記はあくまでサンプルコードです。プロジェクトの要件に合わせて、自由にコードを変更してください。
Node.jsモジュールで定数を共有するその他の方法
require.cache を使用する
// constants.js
module.exports = {
API_KEY: 'your_api_key',
// ...
};
// my-module.js
const constants = require.cache[require.resolve('./constants.js')].exports;
console.log(constants.API_KEY); // 'your_api_key'
lodash などのライブラリを使用する
// my-module.js
const _ = require('lodash');
const constants = _.defaults({
API_KEY: 'your_api_key',
}, process.env);
console.log(constants.API_KEY); // 'your_api_key'
自作のモジュールを使用する
// constants.js
const constants = {};
module.exports = {
get: (key) => constants[key],
set: (key, value) => constants[key] = value,
};
// my-module.js
const constants = require('./constants');
constants.set('API_KEY', 'your_api_key');
console.log(constants.get('API_KEY')); // 'your_api_key'
- require.cache を使用する方法は、最もシンプルですが、あまり一般的ではありません。
- lodash などのライブラリを使用する方法は、コードが簡潔になりますが、ライブラリに依存する必要があります。
- 自作のモジュールを使用する方法は、最も柔軟ですが、コード量が増えてしまいます。
注意事項
- require.cache を使用する方法は、キャッシュの問題が発生する可能性があります。
- lodash などのライブラリを使用する方法は、ライブラリのバージョンによって動作が変わる可能性があります。
- 自作のモジュールを使用する方法は、モジュールのテストが重要になります。
javascript node.js