nuxt: Nuxt cloudflare-pages deployment fails

Environment

  • Operating System: Darwin
  • Node Version: v18.16.1
  • Nuxt Version: 3.6.0
  • Nitro Version: 2.5.1
  • Package Manager: npm@9.5.1
  • Builder: vite
  • User Config: devtools, modules, apollo
  • Runtime Modules: @nuxtjs/apollo@5.0.0-alpha.6
  • Build Modules: -

Reproduction

https://github.com/floatingpixels/nuxt-apollo.git

Describe the bug

I’m trying to deploy a Nuxt app to cloudflare pages. The app builds and runs fine locally, but the deployment to cloudflare pages fails. I’m using the Nuxt Apollo module, but I think the error might not be specific to the module, but has something to do with how Nuxt deploys on Cloudflare Pages. When deploying to Cloudflare pages I get the following error:

20:41:08.091 | [success] Server built in 1589ms
-- | --
20:41:08.108 | [success] [nitro] Generated public dist
20:41:08.168 | [info] [nitro] Building Nitro Server (preset: `cloudflare-pages`)
20:41:12.418 | (inject plugin) rollup-plugin-inject: failed to parse /opt/buildhome/repo/node_modules/graphql/jsutils/instanceOf.mjs. Consider restricting the plugin to particular files via options.include
20:41:12.421 | [error] [nitro] RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
20:41:12.422 |  
20:41:12.422 |  
20:41:12.422 | 10:   /* c8 ignore next 6 */
20:41:12.422 | 11:   // FIXME: https://github.com/graphql/graphql-js/issues/2317
20:41:12.423 | 12:   globalThis.process && globalThis.process.env.NODE_ENV === 'production'
20:41:12.423 | ^
20:41:12.423 | 13:     ? function instanceOf(value, constructor) {
20:41:12.423 | 14:         return value instanceof constructor;
20:41:12.424 | [error] Unexpected token (Note that you need plugins to import files that are not JavaScript)
20:41:12.425 | at error (node_modules/rollup/dist/es/shared/node-entry.js:2245:30)
20:41:12.425 | at Module.error (node_modules/rollup/dist/es/shared/node-entry.js:13572:16)
20:41:12.425 | at Module.tryParse (node_modules/rollup/dist/es/shared/node-entry.js:14298:25)
20:41:12.425 | at Module.setSource (node_modules/rollup/dist/es/shared/node-entry.js:13899:39)
20:41:12.425 | at ModuleLoader.addModuleSource (node_modules/rollup/dist/es/shared/node-entry.js:24422:20)
20:41:12.472 | Failed: Error while executing user command. Exited with error code: 1
20:41:12.482 | Failed: build command exited with code: 1
20:41:13.591 | Failed: error occurred while running build command

This implies the build works fine, but rollup-plugin-inject fails to parse the graphql module. I’m not sure how to solve this, I searched through issues here in nuxt, and also over at nuxt apollo, but I don’t know how to go about fixing this. Any help would be appreciated!

Additional context

No response

Logs

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 20 (13 by maintainers)

Most upvoted comments

You’re saying that this error is caused by vite, and the fact that globalThis.process.env.NODE_ENV is not a syntax that vite supports by default ?

Yupp, I am saying that somewhere in your toolchain, you simply replace a string in your JavaScript without checking if it is a full expression - which is extremely dangerous, as the output of a simple string replacement on JavaScript code is not neccesarily JavaScript code.

I’m not a nuxt user myself, but if this only gets changed over at graphql, the next library is bound to ship code and break things again.

It seems that you are using the rollup replace plugin, which has a delimiters option that you can adjust to make sure this doesn’t happen.
Witout trying it, I believe changing the left delimiter from \b to (?<!\.)\b should help here, so you’d end up with delimiters: ['(?<!\\.)\\b', '\\b(?!\\.)']

Maybe you could even bring that up with rollup? It seems like a sane default to see if there is not only a word-boundary, but also no dot preceeding that. 🙂

I’m not sure why, but it looks like globalThis is not supported with some nitro presets. You can work around the issue it by patching this line in node_modules/graphql/jsutils/instanceOf.mjs

Replace this line :

globalThis.process && globalThis.process.env.NODE_ENV === 'production'

with this line :

process && process.env.NODE_ENV === 'production'

It’s easier if you use pnpm or yarn because of patch support. If you’re using npm, use https://github.com/ds300/patch-package.

You can also use nitropack update in package.json as in the example below.

Checked for operation and no problems for several weeks in production 😉

    "pnpm": {
	"overrides": {
	    "nitropack": "npm:nitropack-edge@2.6.0-28142855.f0d0d91"
	}
    }

@Hebilicious I believe this might actually be on the Nuxt side.

The error message likely shows the unminified code - and the problem lies in the minified code. At least with vite, this transformation happens:

-globalThis.process.env.NODE_ENV
+globalThis."production"

Which would explain the error message of "Unexpected token", which you would not get only because a variable name like process or globalThis would not exist.

Thanks for chiming in @phryneas.

You’re saying that this error is caused by vite, and the fact that globalThis.process.env.NODE_ENV is not a syntax that vite supports by default ?

Looking at vite code, it looks like this should work : https://github.com/vitejs/vite/pull/12194

---globalThis.process.env.NODE_ENV
+++"production"

There’s definitely something strange happening, I’ll investigate a little further.

Edit :

in dist/_nuxt/entry.hash.js the output looks fine, so this must happens on the Nitro build further down the line.

  /* c8 ignore next 6 */
  // FIXME: https://github.com/graphql/graphql-js/issues/2317
  globalThis.process && true ? function instanceOf2(value, constructor) {
    return value instanceof constructor;
  } : function instanceOf3(value, constructor) {
    if (value instanceof constructor) {
      return true;
    }

My best guess would be that the rollup custom replace logic happening there doesn’t work with globalThis.process.env.NODE_ENV

Edit 2: I made a PR with a fix in the upstream Nitro repository.

@Hebilicious I believe this might actually be on the Nuxt side.

The error message likely shows the unminified code - and the problem lies in the minified code. At least with vite, this transformation happens:

-globalThis.process.env.NODE_ENV
+globalThis."production"

Which would explain the error message of "Unexpected token", which you would not get only because a variable name like process or globalThis would not exist.

It works using pnpm with its patch function. Thank you very much @Hebilicious !

Thank you @Hebilicious, I used yarn patch to replace the line and now it works!