Skip to content

🎥 Make videos programmatically with React

Support .env.local

Jonny Burger288305b80aaf
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index c0b212f01f..68de8afcf2 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -134,7 +134,7 @@ export const getCliOptions = (options: {
frameRange,
shouldOutputImageSequence,
inputProps: getInputProps(null, options.logLevel),
- envVariables: getEnvironmentVariables(null, options.logLevel),
+ envVariables: getEnvironmentVariables(null, options.logLevel, false),
jpegQuality: ConfigInternals.getJpegQuality(),
crf,
pixelFormat,
diff --git a/packages/cli/src/get-env.ts b/packages/cli/src/get-env.ts
index 47a0e30308..66781148b0 100644
--- a/packages/cli/src/get-env.ts
+++ b/packages/cli/src/get-env.ts
@@ -65,12 +65,19 @@ const watchEnvFile = ({
return unwatch;
};
-const getEnvForEnvFile = (
- processEnv: ReturnType<typeof getProcessEnv>,
- envFile: string,
- onUpdate: null | ((newProps: Record<string, string>) => void),
- logLevel: LogLevel,
-) => {
+const getEnvForEnvFile = ({
+ processEnv,
+ envFile,
+ onUpdate,
+ logLevel,
+ indent,
+}: {
+ processEnv: ReturnType<typeof getProcessEnv>;
+ envFile: string;
+ onUpdate: null | ((newProps: Record<string, string>) => void);
+ logLevel: LogLevel;
+ indent: boolean;
+}) => {
try {
const envFileData = readFileSync(envFile);
if (onUpdate) {
@@ -84,6 +91,8 @@ const getEnvForEnvFile = (
}
}
+ Log.verbose({indent, logLevel}, `Loaded env file from ${envFile}.`);
+
return {
...processEnv,
...dotenv.parse(envFileData),
@@ -95,9 +104,25 @@ const getEnvForEnvFile = (
}
};
+const findDotEnvFile = (
+ remotionRoot: string,
+): {found: string | null; defaultEnvFile: string} => {
+ const defaultEnvFile = path.resolve(remotionRoot, '.env');
+ const paths = [defaultEnvFile, path.resolve(remotionRoot, '.env.local')];
+
+ for (const p of paths) {
+ if (fs.existsSync(p)) {
+ return {found: p, defaultEnvFile};
+ }
+ }
+
+ return {found: null, defaultEnvFile};
+};
+
export const getEnvironmentVariables = (
onUpdate: null | ((newProps: Record<string, string>) => void),
logLevel: LogLevel,
+ indent: boolean,
): Record<string, string> => {
const processEnv = getProcessEnv();
@@ -110,7 +135,7 @@ export const getEnvironmentVariables = (
process.exit(1);
}
- return getEnvForEnvFile(processEnv, envFile, onUpdate, logLevel);
+ return getEnvForEnvFile({processEnv, envFile, onUpdate, logLevel, indent});
}
const remotionRoot = RenderInternals.findRemotionRoot();
@@ -127,11 +152,12 @@ export const getEnvironmentVariables = (
process.exit(1);
}
- return getEnvForEnvFile(processEnv, envFile, onUpdate, logLevel);
+ return getEnvForEnvFile({processEnv, envFile, onUpdate, logLevel, indent});
}
- const defaultEnvFile = path.resolve(remotionRoot, '.env');
- if (!fs.existsSync(defaultEnvFile)) {
+ const {defaultEnvFile, found} = findDotEnvFile(remotionRoot);
+
+ if (!found) {
if (onUpdate) {
if (typeof fs.watchFile === 'undefined') {
Log.warn(
@@ -151,5 +177,11 @@ export const getEnvironmentVariables = (
return processEnv;
}
- return getEnvForEnvFile(processEnv, defaultEnvFile, onUpdate, logLevel);
+ return getEnvForEnvFile({
+ processEnv,
+ envFile: found,
+ onUpdate,
+ logLevel,
+ indent,
+ });
};
diff --git a/packages/cli/src/studio.ts b/packages/cli/src/studio.ts
index e1044df1a3..aae96dcd6f 100644
--- a/packages/cli/src/studio.ts
+++ b/packages/cli/src/studio.ts
@@ -69,15 +69,19 @@ export const studioCommand = async (
});
});
}, logLevel);
- let envVariables = getEnvironmentVariables((newEnvVariables) => {
- StudioServerInternals.waitForLiveEventsListener().then((listener) => {
- envVariables = newEnvVariables;
- listener.sendEventToClient({
- type: 'new-env-variables',
- newEnvVariables,
+ let envVariables = getEnvironmentVariables(
+ (newEnvVariables) => {
+ StudioServerInternals.waitForLiveEventsListener().then((listener) => {
+ envVariables = newEnvVariables;
+ listener.sendEventToClient({
+ type: 'new-env-variables',
+ newEnvVariables,
+ });
});
- });
- }, logLevel);
+ },
+ logLevel,
+ false,
+ );
const maxTimelineTracks = ConfigInternals.getMaxTimelineTracks();
const keyboardShortcutsEnabled =
diff --git a/packages/docs/docs/env-variables.md b/packages/docs/docs/env-variables.md
index 8063ed8978..161329f824 100644
--- a/packages/docs/docs/env-variables.md
+++ b/packages/docs/docs/env-variables.md
@@ -23,7 +23,8 @@ In your project, you can access the variable using `process.env.REMOTION_MY_VAR`
## Using a dotenv file
-[Dotenv](https://www.npmjs.com/package/dotenv) support is built in if you use the CLI.
+[Dotenv](https://www.npmjs.com/package/dotenv) support is built in if you use the CLI.
+If you use the Node.JS APIs, the `.env` file **is not automatically read** and you need to use the `dotenv` package yourself.
Place a `.env` file in the [root](/docs/terminology/remotion-root) of your project and fill it with key-value pairs.
@@ -32,7 +33,18 @@ MY_VAR=hello
ANOTHER_VAR=world
```
-In your Remotion project you can read `process.env` to get an object of environment variables: `{"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"}`.
+
+Since `v4.0.110`, the following locations will get automatically recognized:
+
+- `.env` in your [Remotion Root](/docs/terminology/remotion-root)
+- `.env.local` in your [Remotion Root](/docs/terminology/remotion-root)
+
+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.
+```
You can override the location of your dotenv file using the [configuration file setting](/docs/config#setdotenvlocation) or the [CLI flag](/docs/cli).