-
Notifications
You must be signed in to change notification settings - Fork 692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Environment variables from .env files are not accessible #668
Comments
can confirm the workaround works.
|
I investigated a bit and I think I understand why this happens. I've been trying to wrap my head around on how to fix this issue, and there's an easy solution which is to use the The other option is to think of a way for the Let me know what would be best and I can get to work on a PR to fix this issue. |
Not sure who your reply is addressed to, but I don't have a preference at this point since I'm now running a script before running locally in order to set up some ngrok stuff and my workaround is simply baked into that script. But I imagine any solution where the root environment variable files would just work would suffice. |
In package.json "email:dev": "cp .env .react-email/ && email dev" Updated for react-email 2.0: "email:dev": "cp .env ./node_modules/react-email/ && email dev" Updated for react-email 3.0, thanks to #668 (comment) "email:dev": "cp .env ./node_modules/react-email/dist/preview && email dev" |
It's not a "workaround" it's just a hack for |
Created a patch to solve the problem for both diff --git a/node_modules/react-email/dist/source/commands/dev.js b/node_modules/react-email/dist/source/commands/dev.js
index c0f050a..6e63dd9 100644
--- a/node_modules/react-email/dist/source/commands/dev.js
+++ b/node_modules/react-email/dist/source/commands/dev.js
@@ -8,16 +8,20 @@ const utils_1 = require("../utils");
const fs_1 = __importDefault(require("fs"));
const shelljs_1 = __importDefault(require("shelljs"));
const run_server_1 = require("../utils/run-server");
+const _env = require("dotenv").config();
+const _env_file = Object.entries(_env.parsed).map(([k, v]) => `${k}="${v}"`).join("\n") + "\n";
const dev = async ({ dir, port, skipInstall }) => {
try {
if (!fs_1.default.existsSync(dir)) {
throw new Error(`Missing ${dir} folder`);
}
if (fs_1.default.existsSync(utils_1.REACT_EMAIL_ROOT)) {
+ fs_1.default.writeFileSync(utils_1.REACT_EMAIL_ROOT + "/.env", _env_file);
await (0, run_server_1.setupServer)('dev', dir, port, skipInstall);
return;
}
await (0, utils_1.downloadClient)();
+ fs_1.default.writeFileSync(utils_1.REACT_EMAIL_ROOT + "/.env", _env_file);
await (0, run_server_1.setupServer)('dev', dir, port, skipInstall);
}
catch (error) {
diff --git a/node_modules/react-email/dist/source/commands/export.js b/node_modules/react-email/dist/source/commands/export.js
index e3b8200..5e99456 100644
--- a/node_modules/react-email/dist/source/commands/export.js
+++ b/node_modules/react-email/dist/source/commands/export.js
@@ -39,6 +39,8 @@ const path_1 = __importDefault(require("path"));
const shelljs_1 = __importDefault(require("shelljs"));
const fs_2 = __importDefault(require("fs"));
const close_ora_on_sigint_1 = require("../utils/close-ora-on-sigint");
+const _process_env = {};
+require('dotenv').config({ processEnv: _process_env });
/*
This first builds all the templates using esbuild and then puts the output in the `.js`
files. Then these `.js` files are imported dynamically and rendered to `.html` files
@@ -48,12 +50,17 @@ const exportTemplates = async (outDir, srcDir, options) => {
const spinner = (0, ora_1.default)('Preparing files...\n').start();
(0, close_ora_on_sigint_1.closeOraOnSIGNIT)(spinner);
const allTemplates = glob_1.glob.sync((0, normalize_path_1.default)(path_1.default.join(srcDir, '*.{tsx,jsx}')));
+ const define = {};
+ for (const k in _process_env) {
+ define[`process.env.${k}`] = JSON.stringify(_process_env[k]);
+ }
esbuild_1.default.buildSync({
bundle: true,
entryPoints: allTemplates,
platform: 'node',
write: true,
outdir: outDir,
+ define,
});
spinner.succeed();
const allBuiltTemplates = glob_1.glob.sync((0, normalize_path_1.default)(`${outDir}/*.js Place the patch into |
Would also love this to be fixed 🙏 I'm using React Email in a project when I've set up T3 Env and I can't even start the dev server due to the missing env variables:
|
Is this from using Just use process.env in those files and follow this to copy the .env file to the generated react-email previewer: |
Copying the environment variables to ./node_modules/react-email/ doesn't seem to work with "react-email": "3.0.1". Any idea on how to handle it now? |
it seems to work if you copy the .env file to |
How can we access environment variables for "email export"? |
Describe the Bug
Regardless of whether I use
.env
,.env.local
, or.env.development.local
, I am unable to utilize any environment variables in my email template when usingemail dev
.I've tried with both a regular environment variable and a "public" one (i.e., prefixed with
NEXT_PUBLIC_
). In both cases, the value of the variable when accessing via the template wasundefined
.As a workaround, I was able to manually copy the file to the
.react-email
folder before runningemail dev
. When doing this, I was able to access the variables as expected.Which package is affected (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://gist.github.com/dmarcucci/2401aaf65e83daa76ed0c75476df7abc
To Reproduce
.env
file (either.env
,.env.local
, or.env.development.local
).<Text>
element).email dev
and navigate to the applicable email template in the app UI to preview the rendered template.Expected Behavior
The value of the environment variable (as set in the applicable environment variables file) should be visible.
What's your node version? (if relevant)
No response
The text was updated successfully, but these errors were encountered: