question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature request: inject special env vars/runtime vars to detect `--local` mode

See original GitHub issue

With miniflare one could inject an environment variable in build scripts to delineate slightly differently between a real worker and one that runs locally.

see

https://github.com/cloudflare/miniflare/blob/5d9032470247173c54f4fecfc4f7750f10beaee8/packages/core/src/plugins/build.ts#L73

This is helpful to get around the lack of support of secrets so could be used like;

// file: my-app.js

const get_jwt_factory = (secret) =>
   HS256({
     key: secret,
     expires: 86400, // 24 hours
   });

export const jwt = (env) => get_jwt_factory(__DEV__ ? 'test-key' : env.JWT_SECRET);

// file: worktop.config.js

import { define } from 'worktop.build';

export default define({
	modify(config) {
		config.define = {
			__DEV__: JSON.stringify(process.env.MINIFLARE === '1'),
		};
	},
});

would be nice to bring this environment variable back.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
threepointonecommented, May 4, 2022

We landed initial support for dotenv style injection in https://github.com/cloudflare/wrangler2/pull/879 tldr make a file called .dev.vars in your project root

0reactions
threepointonecommented, May 5, 2022

I think a better solution would be to use environments. In your wrangler.toml, you can set up

[vars]
secret = 'actual-key'

[build]
command = "npm run build"

[env.dev.vars]
secret = 'test-key'

[env.dev.build]
command = "DEV=1 npm run build"

So you can run wrangler dev --env dev and it’ll load test-key.

To be clear, this tackles multiple separate issues -

  • injecting variables into a build time process, which you can with regular system environment variables. We also landed dotenv support, so you can use .env files as well.
  • injecting runtime vars, which you can do with wrangler.toml/--env/[vars]
  • additionally, if you want to inject variables only during wrangler dev you can use dev.vars, which uses dotenv syntax.

I don’t think we have any plans right now to introduce any variables to detect whether we’re running local vs remote modes, but I’ll leave this issue open till we actually discuss and decide. This is also not a regression since we never supported it, and I’ve amended the title to reflect that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Environment Variables in Node.js for App Configuration ...
Learn why experienced Node.js developers use environment variables for app config and secrets, including how to manage default values and ...
Read more >
Adding Custom Environment Variables | Create React App
Your project can consume variables declared in your environment as if they were declared locally in your JS files.
Read more >
Environment variables in Compose | Docker Documentation
How to set, use and manage environment variables in Compose. ... The .env file feature only works when you use the docker-compose up...
Read more >
Env Variables and Modes - Vite
Vite exposes env variables on the special import.meta.env object. ... [mode].local # only loaded in specified mode, ignored by git. Env Loading Priorities....
Read more >
Configuration and Config Vars - Heroku Dev Center
For example, in a Ruby environment, you could set a RACK_ENV config var to development to enable such a mode. It's important to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found