如何在使用NodeJS CLI应用程序时指定NODE_ENV

huangapple go评论62阅读模式
英文:

How to specify NODE_ENV when using NodeJS CLI app

问题

我有一个使用NodeJS制作的CLI应用程序,但我无法弄清楚如何指定NODE_ENV以外的其他方法,而不是每次都要指定它:

NODE_ENV=development myapp

我有两个环境文件:.env.development和.env.production

我有一个管理环境的配置文件:

import dotenv from "dotenv";
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });

export const config = {
    environment: process.env.NODE_ENV,
    postgres_host: process.env.POSTGRES_HOST,
    postgres_database: process.env.POSTGRES_DATABASE,
    postgres_username: process.env.POSTGRES_USERNAME,
    postgres_password: process.env.POSTGRES_PASSWORD,
    postgres_port: process.env.POSTGRES_PORT,
    telegram_api_key: process.env.TELEGRAM_API_KEY,
    telegram_chat_id: process.env.TELEGRAM_CHAT_ID
};

当需要时,我使用我的配置对象:

import { config } from "../config/config.js";

export class Postgres {
    
    static connect() {
        this.client = new Client({
            host: config.postgres_host,
            database: config.postgres_database,
            user: config.postgres_username,
            password: config.postgres_password,
            port: config.postgres_port
        });
        this.client.connect(function(error) {
            if (error) throw error;
            console.log("Connected to PostgreSQL");
        });
    }
    ...

我想使用:

myapp

并在我的index.js文件或其他地方设置所选的环境。

#! /usr/bin/env node
import { program } from "commander";
import test from "./commands/test.js";
// 可以在这里设置默认值吗?
program
    .command("test")
    .description("test")
    .action(test);

program.parse();

这是我的package.json:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "Myapp",
  "main": "bin/index.js",
  "keywords": [
    "cli"
  ],
  "bin": {
    "myapp": "./bin/index.js"
  },
  ...
}

你有什么想法吗?

英文:

I have a CLI app made with NodeJS and I can't figure out how to specify NODE_ENV another way than specify it everytime :

NODE_ENV=development myapp

I have two env files : .env.development and .env.production

I have a config files that manage the environment :

import dotenv from "dotenv";
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });

export const config = {
	environment: process.env.NODE_ENV,
	postgres_host: process.env.POSTGRES_HOST,
	postgres_database: process.env.POSTGRES_DATABASE,
	postgres_username: process.env.POSTGRES_USERNAME,
	postgres_password: process.env.POSTGRES_PASSWORD,
	postgres_port: process.env.POSTGRES_PORT,
	telegram_api_key: process.env.TELEGRAM_API_KEY,
	telegram_chat_id: process.env.TELEGRAM_CHAT_ID
};

And I use my config object when needed :

import { config } from "../config/config.js";

export class Postgres {

	static connect() {
		this.client = new Client({
			host: config.postgres_host,
			database: config.postgres_database,
			user: config.postgres_username,
			password: config.postgres_password,
			port: config.postgres_port
		});
		this.client.connect(function(error) {
			if (error) throw error;
			console.log("Connected to PostgreSQL");
		});
	}
...

I'd like to use :

myapp 

And set the chosen environment in my index.js file or somewhere else.

#! /usr/bin/env node
import { program } from "commander";
import test from "./commands/test.js";
// Set it by default here maybe ?
program
	.command("test")
	.description("test")
	.action(test);

program.parse();

This is my package.json :

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "Myapp",
  "main": "bin/index.js",
  "keywords": [
    "cli"
  ],
  "bin": {
    "myapp": "./bin/index.js"
  },
...

Do you have an idea ?

答案1

得分: 1

你可以在你的 config.js 文件中设置一个默认环境。在加载你的 dotenv 文件时,你可以指定一个默认的 NODE_ENV 值。例如,如果你希望在未指定 NODE_ENV 时默认使用 .env.development 文件,你可以使用以下行来加载 dotenv 文件。

dotenv.config({ path: `.env.${process.env.NODE_ENV || "development"}` });
英文:

You can set a default environment to use in your config.js file. When loading your dotenv file, you can specify a default NODE_ENV value. For example, if you want to use the .env.development file by default if NODE_ENV is not specified, you can use the following line to load the dotenv file.

dotenv.config({ path: `.env.${process.env.NODE_ENV || "development"}` });

答案2

得分: 1

你可以轻松地说:

myEnv = process.env.NODE_ENV || "development"

然后像这样使用 myEnv 而不是 NODE_ENV

import dotenv from "dotenv"
myEnv = process.env.NODE_ENV || "development"
dotenv.config({ path: `.env.${myEnv}` })
英文:

you can easily say:

myEnv = process.env.NODE_ENV || "development"

and then use myEnv instead of NODE_ENV like that:

import dotenv from "dotenv"
myEnv = process.env.NODE_ENV || "development"
dotenv.config({ path: `.env.${ myEnv }` })

huangapple
  • 本文由 发表于 2023年1月6日 05:43:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75024672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定