cross-env vs dotenv vs dotenv-flow vs env-cmd
环境变量管理工具
cross-envdotenvdotenv-flowenv-cmd类似的npm包:

环境变量管理工具

环境变量管理工具用于在Node.js应用程序中处理环境变量的加载和设置。这些工具可以帮助开发者在不同的环境(如开发、测试和生产)中管理配置,确保应用程序在不同环境下的行为一致性。通过使用这些工具,开发者可以轻松地管理敏感信息和配置,而不必将其硬编码在代码中,从而提高安全性和灵活性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
cross-env06,53120.2 kB16 个月前MIT
dotenv020,35093.3 kB81 个月前BSD-2-Clause
dotenv-flow090160.3 kB82 年前MIT
env-cmd01,81655.2 kB257 个月前MIT

功能对比: cross-env vs dotenv vs dotenv-flow vs env-cmd

环境变量加载方式

  • cross-env:

    cross-env通过在npm脚本中设置环境变量来实现跨平台的环境变量管理,确保在不同操作系统上以相同的方式设置变量。

  • dotenv:

    dotenv通过读取.env文件并将其内容加载到process.env中,提供简单的环境变量管理。

  • dotenv-flow:

    dotenv-flow扩展了dotenv的功能,支持根据当前环境自动加载相应的配置文件,提供更灵活的环境变量管理。

  • env-cmd:

    env-cmd允许在命令行中指定环境变量文件,灵活加载不同的环境配置,适合需要快速切换环境的场景。

多环境支持

  • cross-env:

    cross-env不直接支持多环境配置,但可以通过多个npm脚本来实现不同环境的设置。

  • dotenv:

    dotenv主要支持单一的.env文件,不支持多环境配置。

  • dotenv-flow:

    dotenv-flow专门设计用于支持多环境配置,能够根据当前环境自动加载不同的.env文件。

  • env-cmd:

    env-cmd支持通过命令行参数指定不同的环境变量文件,灵活应对多环境需求。

易用性

  • cross-env:

    cross-env使用简单,主要用于npm脚本中设置环境变量,适合快速上手。

  • dotenv:

    dotenv的使用非常简单,只需创建一个.env文件并调用dotenv.load()即可。

  • dotenv-flow:

    dotenv-flow在dotenv的基础上增加了环境管理的复杂性,但仍然保持易用性,适合需要多环境支持的用户。

  • env-cmd:

    env-cmd的使用相对简单,可以通过命令行参数快速指定环境变量文件,适合命令行操作频繁的用户。

社区支持与维护

  • cross-env:

    cross-env是一个广泛使用的工具,社区支持良好,维护活跃。

  • dotenv:

    dotenv是最流行的环境变量管理工具之一,拥有广泛的社区支持和文档。

  • dotenv-flow:

    dotenv-flow相对较新,但也得到了良好的社区支持,适合需要多环境配置的用户。

  • env-cmd:

    env-cmd在社区中也有一定的使用基础,维护活跃,适合需要灵活环境管理的用户。

安全性

  • cross-env:

    cross-env本身不处理敏感信息的安全性,依赖于用户的实现方式。

  • dotenv:

    dotenv将敏感信息存储在.env文件中,需注意文件权限和安全性。

  • dotenv-flow:

    dotenv-flow同样依赖于.env文件的安全性,适合在安全的环境中使用。

  • env-cmd:

    env-cmd通过命令行加载环境变量,需谨慎处理敏感信息,避免在命令行历史中泄露。

如何选择: cross-env vs dotenv vs dotenv-flow vs env-cmd

  • cross-env:

    选择cross-env如果你需要在不同的操作系统(如Windows和Unix)上统一设置环境变量,特别是在npm脚本中。它可以确保在所有平台上以相同的方式设置环境变量。

  • dotenv:

    选择dotenv如果你需要简单地从.env文件中加载环境变量到process.env中。它适合于小型项目或简单的环境变量管理。

  • dotenv-flow:

    选择dotenv-flow如果你需要支持多种环境配置文件(如.env、.env.development、.env.production等),并希望在不同环境中自动加载相应的配置。它提供了更灵活的环境管理功能。

  • env-cmd:

    选择env-cmd如果你需要在运行应用程序时加载特定的环境变量文件,且希望在命令行中灵活指定环境配置。它适合于需要在不同环境下快速切换的场景。

cross-env的README

cross-env 🔀

Run scripts that set and use environment variables across platforms

🎉 NOTICE: cross-env is "done" as in it does what it does and there's no need for new features. Learn more


Build Status Code Coverage version downloads MIT License

The problem

Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is Bash on Windows, which uses native Bash.) Similarly, there's a difference in how windows and POSIX commands utilize environment variables. With POSIX, you use: $ENV_VAR and on windows you use %ENV_VAR%.

This solution

cross-env makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform. Just set it like you would if it's running on a POSIX system, and cross-env will take care of setting it properly.

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev cross-env

WARNING! Make sure that when you're installing packages that you spell things correctly to avoid mistakenly installing malware

NOTE : Version 8 of cross-env only supports Node.js 20 and higher, to use it on Node.js 18 or lower install version 7 npm install --save-dev cross-env@7

Usage

I use this in my npm scripts:

{
	"scripts": {
		"build": "cross-env NODE_ENV=production node ./start.js --enable-turbo-mode"
	}
}

Ultimately, the command that is executed (using cross-spawn) is:

node ./start.js --enable-turbo-mode

The NODE_ENV environment variable will be set by cross-env

You can set multiple environment variables at a time:

{
	"scripts": {
		"build": "cross-env FIRST_ENV=one SECOND_ENV=two node ./my-program"
	}
}

You can also split a command into several ones, or separate the environment variables declaration from the actual command execution. You can do it this way:

{
	"scripts": {
		"parentScript": "cross-env GREET=\"Joe\" npm run childScript",
		"childScript": "cross-env-shell \"echo Hello $GREET\""
	}
}

Where childScript holds the actual command to execute and parentScript sets the environment variables to use. Then instead of run the childScript you run the parent. This is quite useful for launching the same command with different env variables or when the environment variables are too long to have everything in one line. It also means that you can use $GREET env var syntax even on Windows which would usually require it to be %GREET%.

If you precede a dollar sign with an odd number of backslashes the expression statement will not be replaced. Note that this means backslashes after the JSON string escaping took place. "FOO=\\$BAR" will not be replaced. "FOO=\\\\$BAR" will be replaced though.

Lastly, if you want to pass a JSON string (e.g., when using ts-loader), you can do as follows:

{
	"scripts": {
		"test": "cross-env TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} node some_file.test.ts"
	}
}

Pay special attention to the triple backslash (\\\) before the double quotes (") and the absence of single quotes ('). Both of these conditions have to be met in order to work both on Windows and UNIX.

cross-env vs cross-env-shell

The cross-env module exposes two bins: cross-env and cross-env-shell. The first one executes commands using cross-spawn, while the second one uses the shell option from Node's spawn.

The main use case for cross-env-shell is when you need an environment variable to be set across an entire inline shell script, rather than just one command.

For example, if you want to have the environment variable apply to several commands in series then you will need to wrap those in quotes and use cross-env-shell instead of cross-env.

{
	"scripts": {
		"greet": "cross-env-shell GREETING=Hi NAME=Joe \"echo $GREETING && echo $NAME\""
	}
}

The rule of thumb is: if you want to pass to cross-env a command that contains special shell characters that you want interpreted, then use cross-env-shell. Otherwise stick to cross-env.

On Windows you need to use cross-env-shell, if you want to handle signal events inside of your program. A common case for that is when you want to capture a SIGINT event invoked by pressing Ctrl + C on the command-line interface.

Windows Issues

Please note that npm uses cmd by default and that doesn't support command substitution, so if you want to leverage that, then you need to update your .npmrc to set the script-shell to powershell. Learn more here.

Inspiration

I originally created this to solve a problem I was having with my npm scripts in angular-formly. This made contributing to the project much easier for Windows users.

Other Solutions

  • env-cmd - Reads environment variables from a file instead
  • @naholyr/cross-env - cross-env with support for setting default values

LICENSE

MIT