Skip to main content

环境变量

v2.1.2

Remotion 支持通过 CLI 直接传递环境变量,使用 .env 文件以及通过 renderMedia() 函数传递。

🌐 Remotion supports environment variables being passed directly from the CLI, using a .env file and from the renderMedia() function.

从命令行传递变量

🌐 Passing variables from the CLI

如果你想从命令行传递环境变量,你需要在它前面加上 REMOTION_。这是一个安全功能,用于防止你的整个环境(可能包含敏感信息)被包括在 Webpack 包中。

🌐 If you want to pass an environment variable from the CLI, you need to prefix it with REMOTION_. This is a security feature to prevent your whole environment (which could contain sensitive information) being included in a Webpack bundle.

你可以在开发模式和渲染时传递环境变量。例如:

🌐 You can pass environment variables in development mode and while rendering. For example:

REMOTION_MY_VAR=hello_world npm run dev

在你的项目中,你可以使用 process.env.REMOTION_MY_VAR 来访问该变量。

🌐 In your project, you can access the variable using process.env.REMOTION_MY_VAR.

使用 dotenv 文件

🌐 Using a dotenv file

如果你使用 CLI,Dotenv 支持是内置的。 如果你使用 Node.JS API,.env 文件不会被自动读取,你需要自己使用 dotenv 包。

在你的项目的根目录中放置一个 .env 文件,并填写键值对。

🌐 Place a .env file in the root of your project and fill it with key-value pairs.

.env
MY_VAR=hello ANOTHER_VAR=world

在你的 Remotion 前端代码中,你可以读取 process.env 来获取环境变量对象:{"MY_VAR": "hello", "ANOTHER_VAR": "world"}

🌐 In your Remotion frontend code you can read process.env to get an object of environment variables: {"MY_VAR": "hello", "ANOTHER_VAR": "world"}.

v4.0.110 起,以下位置将被自动识别:

🌐 Since v4.0.110, the following locations will get automatically recognized:

你可以通过在命令中添加 --log=verbose 标志并注意以下日志,来查看哪个配置文件被读取:

🌐 You can see which config file gets read by adding a --log=verbose flag to your command and looking out for the following log:

Loaded env file from /Users/my-user/remotion-project/.env.local.

你可以使用配置文件设置命令行标志来覆盖 dotenv 文件的位置。

🌐 You can override the location of your dotenv file using the configuration file setting or the CLI flag.

在 Node.js API 中使用

🌐 Using in Node.js APIs

在使用 Node.js API(例如 renderMedia()renderMediaOnLambda()renderMediaOnVercel())时,环境变量不会被自动获取。

🌐 When using the Node.js APIs such as renderMedia(), renderMediaOnLambda() or renderMediaOnVercel(), the environment variables are not picked up automatically.

原因是,有人可能会将 Remotion 作为大型应用的一小部分进行集成,如果 Remotion 自动读取 .env 文件并将所有变量传递给渲染,就会导致安全问题。

🌐 The reason is that one might integrate Remotion as a small part of a big application and if Remotion would read the .env file automatically and forward all variables to renders, it would lead to a security issue.

在进行服务器端渲染时,要传递环境变量,请将一个对象传递给 renderMedia()envVariables 选项

🌐 To pass environment variables while server-side-rendering, pass an object to the envVariables option of renderMedia().

如果你想从 .env 文件中读取环境变量,请使用 dotenv 包。

🌐 If you want to read the environment variables from a .env file, use the dotenv package.

envVariables 选项

🌐 The envVariables option

renderMedia()envVariablesrenderMediaOnLambda()](/docs/lambda/rendermediaonlambda)renderMediaOnVercel()](/docs/vercel/render-media-on-vercel#envvariables))的 envVariables选项接受一个键值对对象。 然后可以在你的 React 组件中的process.env` 中读取这些值。

🌐 The envVariables option of renderMedia(), renderMediaOnLambda(), renderMediaOnVercel() accepts an object of key-value pairs.
These values can then be read from process.env inside your React component.

此选项不是用来进行 AWS 身份验证的——请使用上述描述的方法之一加载 AWS 凭据。

🌐 The option is not for authenticating with AWS - instead, load the AWS credentials using one of the described methods above.

另请参阅

🌐 See also