Skip to content

Commit f28f549

Browse files
committed
fix: workaround for process.env and new node compat
1 parent 2ea9fa3 commit f28f549

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

playground/nuxt.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default defineNuxtConfig({
2020
kv: true,
2121
cache: true,
2222
bindings: {
23-
// compatibilityFlags: ['nodejs_compat_v2']
23+
compatibilityDate: '2024-10-02',
24+
compatibilityFlags: ['nodejs_compat']
2425
// Used for /api/hyperdrive
2526
// hyperdrive: {
2627
// POSTGRES: '8bb2913857b84c939cd908740fa5a5d5'

playground/server/api/env.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default eventHandler(async () => {
2+
return {
3+
test: true,
4+
projectKey: process.env.NUXT_HUB_PROJECT_KEY
5+
}
6+
})

src/module.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mkdir, writeFile, readFile } from 'node:fs/promises'
22
import { argv } from 'node:process'
3-
import { defineNuxtModule, createResolver, logger, installModule, addServerHandler } from '@nuxt/kit'
3+
import { defineNuxtModule, createResolver, logger, installModule, addServerHandler, addServerPlugin } from '@nuxt/kit'
44
import { join } from 'pathe'
55
import { defu } from 'defu'
66
import { findWorkspaceDir } from 'pkg-types'
@@ -165,6 +165,9 @@ export default defineNuxtModule<ModuleOptions>({
165165
nuxt.options.nitro.commands = nuxt.options.nitro.commands || {}
166166
nuxt.options.nitro.commands.preview = 'npx nuxthub preview'
167167
nuxt.options.nitro.commands.deploy = 'npx nuxthub deploy'
168+
169+
// Add the env plugin
170+
addServerPlugin(resolve('./runtime/env'))
168171
}
169172

170173
// Local development without remote connection
@@ -204,8 +207,7 @@ export default defineNuxtModule<ModuleOptions>({
204207
}
205208
await installModule('nitro-cloudflare-dev')
206209
}
207-
nuxt.options.nitro.plugins = nuxt.options.nitro.plugins || []
208-
nuxt.options.nitro.plugins.push(resolve('./runtime/ready.dev'))
210+
addServerPlugin(resolve('./runtime/ready.dev'))
209211
}
210212
}
211213
})

src/runtime/env.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { NitroAppPlugin } from 'nitropack'
2+
import type { H3Event } from 'h3'
3+
4+
// Hack around Cloudflare regression since nodejs_compat_v2 for process.env
5+
export default <NitroAppPlugin> function (nitroApp) {
6+
nitroApp.hooks.hook('request', async (event: H3Event) => {
7+
const env = event.context.cloudflare?.env || {}
8+
for (const key in env) {
9+
process.env[key] = env[key]
10+
}
11+
})
12+
}

0 commit comments

Comments
 (0)