How to bake NODE_ENV=production into executable?
There's a --options CLI flag used to bake v8 options into executable, but I can't see if it could be used for that or not.
You may consider creating a bundle.js file:
process.env.NODE_ENV = "production"
const app = require('./app.js')
app.run()
And run pkg on it.
When you run the packaged app ./packaged-app or whatever, just run it with NODE_ENV=production ./packaged-app
@ksmithut Thanks, but I can't force a client to pass this arg manually. Just need to start my-app.exe with a NODE_ENV=production already embedded there at package-time.
@Clement-TS Thanks for the workaround. It would be great if there could exist a more elegant way out-of-the-box though. :)
are there any ways to read local env or config file?
I also use the dotenv package which was able to read a .env file that exists next to the binary produced by pkg
But I guess if you can鈥檛 have a client change their environment variable, then forcing them to create an env file probably won鈥檛 be better.
in windows, you can run cmd.exe like this:
SET NODE_ENV=production && app.exe
@PranayAgarwal Well, usually it's exactly opposite: if nothing's set, then it's development. See Express.js or Koa.js as an example.
I'd love to bake an auth token into the executable. We have some build scripts we've compiled into an executable using pkg. It's all internal, but I still don't want an auth token in text. Just want our locked down CI to add that auth token for the code to pull in at compile time.
Agree with lock down access tokens at build time.
Here's an example of what I am currently doing.
I just add the token (cat $TOKEN >> .env or similar) in at CI and use this CI to pkg the tool.
@Deilan : Why don't you just set NODE_ENV to production as default and overwrite it with NODE_ENV=development if you are in development?
You control the development environment, so that should be an easy thing to do.
You could do something like this in a global config.ts file:
const PRODUCTION = 'production';
export const NODE_ENV = process.env.NODE_ENV || PRODUCTION;
If you want to ALWAYS use production mode when in pkg binaries, you could just check for process.pkg (https://github.com/zeit/pkg#snapshot-filesystem), and then set NODE_ENV accordingly:
export const NODE_ENV = (<any>process).pkg ? PRODUCTION : process.env.NODE_ENV;
I think this issue can be closed, because you can set the env vars yourself in a init script by checking for process.pkg
@beac0n this actually worked great. Nice solution
Thank you for your reply dear @beac0n ! So I was able to solve my headache !
We don't need to use NODE_ENV at all. Just check if process.pkg exist. process.pkg == production maybe for some of you like me.
However the code remains present in the package.
If there is a simple method to make everything cleaner, I am interested (like removing dead code (dev)) otherwise it is not a big deal ... Thank you again :)
exports.is_production = process.pkg ? true : false;
const cwd = this.is_production ? process.cwd() : __dirname;
if (!this.is_production) {
// here (for me) == NODE_ENV=development
}
Most helpful comment
You may consider creating a bundle.js file:
And run pkg on it.