Skip to content

[BUG] Build fails with getCloudflareContext() error #317

@mst-mkt

Description

@mst-mkt

Describe the bug

When building code containing getCloudflareContext() with $ opennextjs-cloudflare, an error occurs and the build fails.

Steps to reproduce

  1. Create sample project with
    pnpm create cloudflare@latest opennext-sample --framework=next --experimental
  2. Replace src/app/page.tsx with the following code.
    I confirmed that this is not page.tsx, for example, calling it with app/api/route.ts will also generate the same error.
import { getCloudflareContext } from "@opennextjs/cloudflare";

export default function Home() {
  // call `getCloudflareContext`
  getCloudflareContext();

  return <div />;
}
  1. Build with pnpm opennextjs-cloudflare.
  2. The following error occurs.
> [email protected] build /repo-directory/opennext-sample
> next build

  ▲ Next.js 14.2.23

Using vars defined in .dev.vars
   Creating an optimized production build ...
Using vars defined in .dev.vars
Using vars defined in .dev.vars
Using vars defined in .dev.vars
 ✓ Compiled successfully
 ✓ Linting and checking validity of types
 ✓ Collecting page data
   Generating static pages (0/5)  [=   ]Error:

ERROR: `getCloudflareContext` has been called without having called `initOpenNextCloudflareForDev` from the Next.js config file.
You should update your Next.js config file as shown below:

   \`\`\`
   // next.config.mjs

   import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";

   initOpenNextCloudflareForDev();

   const nextConfig = { ... };
   export default nextConfig;
   \`\`\`


    at /repo-directory/opennext-sample/.next/server/app/page.js:1:32375
    at o (/repo-directory/opennext-sample/.next/server/app/page.js:1:32802)
    at eh (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:134660)
    at e (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:137545)
    at ek (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:138019)
    at Array.toJSON (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135629)
    at stringify (<anonymous>)
    at eP (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142093)
    at eE (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142572)
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14) {
  digest: '1448810572'
}
Error:

ERROR: `getCloudflareContext` has been called without having called `initOpenNextCloudflareForDev` from the Next.js config file.
You should update your Next.js config file as shown below:

   \`\`\`
   // next.config.mjs

   import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";

   initOpenNextCloudflareForDev();

   const nextConfig = { ... };
   export default nextConfig;
   \`\`\`


    at /repo-directory/opennext-sample/.next/server/app/page.js:1:32375
    at o (/repo-directory/opennext-sample/.next/server/app/page.js:1:32802)
    at eh (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:134660)
    at e (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:137545)
    at ek (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:138019)
    at Array.toJSON (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135629)
    at stringify (<anonymous>)
    at eP (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142093)
    at eE (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142572)
    at Timeout._onTimeout (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135349) {
  digest: '4029345042'
}
   Generating static pages (3/5)  [==  ]Error:

ERROR: `getCloudflareContext` has been called without having called `initOpenNextCloudflareForDev` from the Next.js config file.
You should update your Next.js config file as shown below:

   \`\`\`
   // next.config.mjs

   import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";

   initOpenNextCloudflareForDev();

   const nextConfig = { ... };
   export default nextConfig;
   \`\`\`


    at /repo-directory/opennext-sample/.next/server/app/page.js:1:32375
    at o (/repo-directory/opennext-sample/.next/server/app/page.js:1:32802)
    at eh (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:134660)
    at e (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:137545)
    at ek (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:138019)
    at Array.toJSON (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135629)
    at stringify (<anonymous>)
    at eP (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142093)
    at eE (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142572)
    at Timeout._onTimeout (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135349) {
  digest: '4029345042'
}

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error

Error:

ERROR: `getCloudflareContext` has been called without having called `initOpenNextCloudflareForDev` from the Next.js config file.
You should update your Next.js config file as shown below:

   \`\`\`
   // next.config.mjs

   import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";

   initOpenNextCloudflareForDev();

   const nextConfig = { ... };
   export default nextConfig;
   \`\`\`


    at /repo-directory/opennext-sample/.next/server/app/page.js:1:32375
    at o (/repo-directory/opennext-sample/.next/server/app/page.js:1:32802)
    at eh (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:134660)
    at e (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:137545)
    at ek (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:138019)
    at Array.toJSON (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135629)
    at stringify (<anonymous>)
    at eP (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142093)
    at eE (/repo-directory/opennext-sample/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142572)
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
 ✓ Generating static pages (5/5)

> Export encountered errors on following paths:
        /page: /
 ELIFECYCLE  Command failed with exit code 1.
node:internal/errors:983
  const err = new Error(message);
              ^

Error: Command failed: pnpm build
    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:888:11)
    at Object.execSync (node:child_process:960:15)
    at buildNextjsApp (file:///repo-directory/opennext-sample/node_modules/.pnpm/@opennextjs+aws@https+++pkg.pr.new+@opennextjs+aws@715/node_modules/@opennextjs/aws/dist/build/buildNextApp.js:15:8)
    at build (file:///repo-directory/opennext-sample/node_modules/.pnpm/@[email protected][email protected]_@[email protected]_/node_modules/@opennextjs/cloudflare/dist/cli/build/build.js:51:9)
    at async file:///repo-directory/opennext-sample/node_modules/.pnpm/@[email protected][email protected]_@[email protected]_/node_modules/@opennextjs/cloudflare/dist/cli/index.js:7:1 {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 80303,
  stdout: null,
  stderr: null
}

The next.config.mjs is as follows, and the error occurred with or without initOpenNextCloudflareForDev().

import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare';

initOpenNextCloudflareForDev();

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;

Expected behavior

Successful build

> [email protected] build /repo-directory/opennext-sample
> next build

  ▲ Next.js 14.2.23

Using vars defined in .dev.vars
   Creating an optimized production build ...
Using vars defined in .dev.vars
Using vars defined in .dev.vars
Using vars defined in .dev.vars
 ✓ Compiled successfully
 ✓ Linting and checking validity of types
 ✓ Collecting page data
 ✓ Generating static pages (5/5)
 ✓ Collecting build traces
 ✓ Finalizing page optimization

Route (app)                              Size     First Load JS
┌ ○ /                                    137 B          87.2 kB
└ ○ /_not-found                          870 B            88 kB
+ First Load JS shared by all            87.1 kB
  ├ chunks/516-113ddead2270df79.js       31.6 kB
  ├ chunks/669f4e52-0b2a86fe678ea1c2.js  53.6 kB
  └ other shared chunks (total)          1.86 kB


○  (Static)  prerendered as static content

@opennextjs/cloudflare version

0.4.3

Wrangler version

3.107.2

next info output

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 ZEN SMP PREEMPT_DYNAMIC Fri, 06 Dec 2024 11:15:23 +0000
  Available memory (MB): 7628
  Available CPU cores: 8
Binaries:
  Node: 22.11.0
  npm: 10.2.4
  Yarn: 4.0.2
  pnpm: 9.12.1
Relevant Packages:
  next: 14.2.23 // An outdated version detected (latest is 15.1.6), upgrade is highly recommended!
  eslint-config-next: 14.2.23
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.7.3
Next.js Config:
  output: N/A

Additional context

I am not sure whether this is a bug or a misuse on my part. I would appreciate any clarification.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions