cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.67k stars 701 forks source link

🐛 BUG: globalThis?.process.env throws error and prevent wrangler dev #6402

Open schettn opened 2 months ago

schettn commented 2 months ago

Which Cloudflare product(s) does this pertain to?

Workers Runtime

What version(s) of the tool(s) are you using?

3.68.0

What version of Node are you using?

v18.15.0

What operating system and version are you using?

MacOS

Describe the Bug

import {Hono} from 'hono'

const app = new Hono()

app.get('/', c => {
  return c.text('Hello Hono!')
})

export default app

// ERROR
console.log(globalThis?.process.env)

Some packages depend on process.env. It would be better to just provide an empty object instead of an error.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

--- 2024-08-02T10:07:21.383Z debug
Failed to load .env file ".env": Error: ENOENT: no such file or directory, open '.env'
    at Object.openSync (node:fs:601:3)
    at Object.readFileSync (node:fs:469:35)
    at tryLoadDotEnv (/Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:157705:72)
    at loadDotEnv (/Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:157714:12)
    at /Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:202107:20
    at /Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:165871:16
    at maybeAsyncResult (/Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:164092:44)
    at /Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:165870:14
    at /Users/schettn/Documents/pylon/pylon-template/wrangler/node_modules/wrangler/wrangler-dist/cli.js:164079:22
    at Array.reduce (<anonymous>) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '.env'
}
---
schettn commented 2 months ago

Update:

Does NOT work: console.log(globalThis?.process['env'])

Works!: console.log(globalThis.process['env'])

Note the optional chaining?