-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Question on using ENV var , replacing during build time #438
Comments
Closing as a duplicate of #69. You can use the esbuild API for this to write a build script that replaces const { build } = require('esbuild')
const define = {}
for (const k in process.env) {
define[`process.env.${k}`] = JSON.stringify(process.env[k])
}
const options = {
stdio: 'inherit',
entryPoints: ['./src/main.ts'],
outfile: './dist/main.js',
bundle: true,
define,
}
build(options).catch(() => process.exit(1)) |
I did tried to create a file e.g : builder.js that contains the content above... what do i do next ? , i tried node ./builder.js it has no effect no file is created ... then i tried esbuild ./builder.js but its asking to use node as platform, but im not gonna use it for node, only in browser... any help would be appreciated thanks :) |
Sorry, it turns out the code snippet I copied and pasted from the old thread was for an old version of esbuild. The const { buildSync } = require('esbuild')
const define = {}
for (const k in process.env) {
define[`process.env.${k}`] = JSON.stringify(process.env[k])
}
const options = {
entryPoints: ['./src/main.ts'],
outfile: './dist/main.js',
bundle: true,
define,
}
buildSync(options) |
thanks it now works like a charm... |
This doesn't work for env variable destructuring :/ $ echo 'const { NODE_ENV } = process.env; console.log(NODE_ENV); console.log(process.env.NODE_ENV);' | esbuild --define:process.env.NODE_ENV=\"production\"
const { NODE_ENV } = process.env;
console.log(NODE_ENV);
console.log("production"); |
You could fix that by defining |
Partially. You could replace the whole esbuild --define:process.env={\\'NODE_ENV\\':\\'production\\'} input.js --outfile=output.js
const { NODE_ENV, API_URL } = process.env;
console.log(NODE_ENV);
console.log(process.env.NODE_ENV);
console.log(API_URL);
console.log(process.env.API_URL);
var NODE_ENV = "production";
var define_process_env_default = { NODE_ENV };
const { NODE_ENV: NODE_ENV2, API_URL } = define_process_env_default;
console.log(NODE_ENV2);
console.log(define_process_env_default.NODE_ENV);
console.log(API_URL);
console.log(define_process_env_default.API_URL); $ API_URL=example.com node output.js
production
production
undefined
undefined |
Hi i would like to know how i can utilize dotenv
i have a web app, that utilizes dotenv...
this is not a nodejs app , so meaning i have no scope of process.env
is there a way we can replace during build time process.env.ANY_VARIABLE
to the equivalent value in .env ?
The text was updated successfully, but these errors were encountered: