nuxt-security: Error deploying to Cloudflare Workers after upgrade beyond rc.2
Version
- nuxt-security: 1.0.0
- Node.js 18.18.0
- Nuxt 3.9.0
- Nitro 2.8.1
Steps to reproduce
Upgrading nuxt-security beyond 1.0.0-rc.2 breaks deployment to Cloudflare Workers using preset cloudflare_module.
ERROR Cannot resolve "node-fetch-native/native/proxy" from "..." and externals are not allowed!
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Comments: 46 (22 by maintainers)
Issue is with nuxt-security module in /src/runtime/nitro/plugins/05-cspSsgPresets.ts#L2 you should never ever import
@nuxt/kitor nuxt builder dependencies in runtime. It will cause part of Nuxt to build dependencies (including giget that is not even meant for runtime) to be bundled and in workers, causing this issue./cc @danielroe it happened again… I will try to check import protection. (~> https://github.com/nuxt/nuxt/pull/25162)
Hey @pi0 I’ve created a minimal reproduction here.
I’ve setup an empty nuxt 3.9.1 project, add nuxt security and some basic configs.
There is a GH Action that runs the build for
NITRO_PRESET=cloudflare_pagesThe build fails on GH Actions.
Example Project, PR that fails to build
Failing Build GH Action here
Hey guys,
I have just published a 1.0.1 version where we removed the part of the code that was causing the issues. We will refactor it for the next major version that we want to release next week probably (1.1.0)
Sorry it tooked so long but we wanted to refactor the code without disabling functionality but we didnt make it so we will do it in the next release.
Could you please verify if the issue is gone?
Another issue related to this code was fixed in 1.0.1
I think it is probably safest that you disable this flag in meantime. CSP should ideally come to the nitro/nuxt core for proper support in all conditions.
I also made an upstream idea in nitro https://github.com/unjs/nitro/issues/2119 that it can allow same solution you currently do, little more generic so similar usecases can leverage.
Otherwise @danielroe is correct, useNitro is build time and useNitroApp is runtime.
@Baroshem Thank you for this, I can confirm my build now works with 1.0.1
Hi @Baroshem I think a full refactoring is required. However I do think that #298 lay the proper foundations for that. I need to better understand what @huang-julien is doing with the
context, I’ll have a deeper lookHi @pi0 I can’t import
useNitro(), is there something specific I should do ?FYI we have
imports.autoImport=falsein .nuxtrc{ "extends": "../../../.nuxt/tsconfig.server.json" }in src/runtime/nitro/tsconfig.jsonThanks to your comment @pi0 I was able to make the right assumptions when looking through my package.json. Looks like the culprit was a plugin to generate sitemaps (
nuxt-simple-sitemap, https://www.npmjs.com/package/nuxt-simple-sitemap, v3.1.7) which worked fine withnuxt 3.8.4but caused the build error with cloudflare innuxt 3.9.1. The simple fix was to bump this package up to its latest version 5.0.1 (they’ve also renamed it to@nuxtjs/sitemap).Thanks again everyone for your help on this, even though your work wasn’t causing the bug at all. It also made me aware that your package exists. 😃
I wonder what other package causes it for you @1mak
On my example project, if I remove nuxt-security it works fine.
https://github.com/unr/nuxt-security-failed-deploy-example/pull/2
@vejja @pi0
Thanks for your input. Let’s wait some time for guys who reported the issue for reproduction maybe so that we can avoid publishing a new version that wont fix an issue but could break something else 😃
Hey @1mak
Thanks for reporting that! I dont think it is related to NuxtSecurity either (at least the core functionality) but more about its dependencies (giget) or dependency missmatch. I will await for @pi0 here to help us as giget is a package from unjs
I am seeing the exact same issue with nuxt 3.9.1 and nuxt 3.9.1 using cloudflare pages. I also thought it’s a dependency mismatch between
node-fetch-native1.4.1 and 1.6.1 but I could be wrong.Edit: I don’t think this issue is related to nuxt-security as I don’t have this plugin installed.
Yes, definitely
In the meantime, would @unr be ok to try upgrading
nuxtto 3.9.1 ? The fact thatpnpm-lock.yamlwas upgraded there might help giget to findnode-fetch-native@Baroshem I can see that the log from @unr is trying to use
node-fetch-nativefromnitropack@v2.8.1which I understand is v1.6.1 Our module pinsnode-fetch-nativeto v1.4.1 inyarn.lockMaybe upgrading our dependencies would help ?Update: More clues
yarn.lockpinsnitropackto v2.8.0 which in turn pinsnode-fetch-nativeto v1.4.1nitropack@v2.8.1updatednode-fetch-nativeto v1.6.1node-fetch-nativev1.5.0 introducedproxysupport, which is what the log is saying it cannot findnuxt@3.8.2,nitropackwas pinned to v2.8.0; after that it was upgraded to v2.8.1pnpm-lock.yamlfile ofnuxt@3.9.0incorrectly pinsnode-fetch-nativeto v1.4.1. This was corrected innuxt@3.9.1I cannot be 100% sure but it looks like a
gigetproblem. I understand giget has its own dependency resolution algorithm, maybe the fact that the issue only appears in workers environments is a hint that giget cannot resolve the correct version ofnode-fetch-nativethere.