@ou-imdt/utils
Version:
Utility library for interactive media development
31 lines (30 loc) • 926 B
JavaScript
import getEnvVariable from './getEnvVariable.js';
/**
* Utility function to check if the current environment is production.
* It checks both Vite's built-in PROD variable and Node.js's NODE_ENV variable.
*
* @returns {boolean} - True if the environment is production, false otherwise.
*
* @example
*
* Assuming NODE_ENV or --mode set to 'production'
* if (isProduction()) {
* console.log('Running in production mode');
* } else {
* console.log('Not running in production mode');
* }
*
* @example
* Using in npm scripts in package.json
* {
* "scripts": {
* "build": "vite build --mode production", //vite
* "prod": "NODE_ENV=production node server.js" //node
* }
* }
*/
export default function isProduction() {
const viteProd = typeof import.meta !== 'undefined' && import.meta.env.PROD;
const nodeEnv = getEnvVariable('NODE_ENV') === 'production';
return viteProd || nodeEnv;
}