Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular dependency in nuxt with nitropack #18789

Closed
huang-julien opened this issue Feb 5, 2023 · 43 comments · Fixed by unjs/nitro#936 or #18948
Closed

Circular dependency in nuxt with nitropack #18789

huang-julien opened this issue Feb 5, 2023 · 43 comments · Fixed by unjs/nitro#936 or #18948

Comments

@huang-julien
Copy link
Member

huang-julien commented Feb 5, 2023

Environment


  • Operating System: Windows_NT
  • Node Version: v16.17.0
  • Nuxt Version: 3.1.2
  • Nitro Version: 2.1.1
  • Package Manager: yarn@1.22.18
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

and stackblitz

Reproduction

https://stackblitz.com/edit/nuxt-starter-kqf57y?file=README.md

Describe the bug

Hi 👋 !
Nuxt build in 3.1.2 logs a warning about circular dependency

Additional context

No response

Logs

`
Export "useNitroApp" of module "node_modules/nitropack/dist/runtime/app.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "node_modules/nuxt/dist/core/runtime/nitro/renderer.mjs" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.
@manniL
Copy link
Member

manniL commented Feb 5, 2023

Related #18766 (?)

@huang-julien
Copy link
Member Author

this might be related, i'm using 3.1.2 while @Barbapapazes is using 3.1.1.
I tried to use useHead in a component and then build a project but this did not trigger the warning with useHead

@Zerro97
Copy link

Zerro97 commented Feb 7, 2023

There is similar issue raised at nuxt content repo

@Zerro97
Copy link

Zerro97 commented Feb 7, 2023

I previously had cloudflare deployment failing with this circular dependency warning. However, this was problem with the build output directory in cloudflare, because I just managed to resolve the deployment failure by specifying .output/public as the Build output directory. Previously I was using default dist folder as the output directory.

@sandercoffee
Copy link

I previously had cloudflare deployment failing with this circular dependency warning but I managed to resolve the deployment failure by specifying .output/public as the Build output directory. Previously I was using default dist folder as the output directory.

Can you give an example of the file and code?

@Zerro97
Copy link

Zerro97 commented Feb 7, 2023

So I had my blog failing deployment originally, so I made this repo with fresh nuxt setup for testing purpose.

Both of them were failing in deployment but that was because in Cloudflare setting, I was using the default dist build output directory instead of specifying it as .output/public.

This might still be related to the circular dependency issue though because my deployment was working fine with default dist folder previously.

@StefanoTesla
Copy link

same problem here, generated a basic ssg with only i18n static site, the static version goes in an inifinite loop loading, the ssr instead seems work correctly, but i need ssg you can check here #18876

@danielroe
Copy link
Member

cc: @pi0

@StefanoTesla
Copy link

 WARN  Export "useNitroApp" of module "node_modules/nitropack/dist/runtime/app.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "node_modules/nuxt/dist/core/runtime/nitro/renderer.mjs" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.

I continue to get the message

Nuxt project info: 09:03:47


  • Operating System: Windows_NT
  • Node Version: v19.4.0
  • Nuxt Version: 3.1.2
  • Nitro Version: 2.2.1
  • Package Manager: npm@9.4.2
  • Builder: vite
  • User Config: pages, css, modules, i18n
  • Runtime Modules: @nuxtjs/i18n@8.0.0-beta.9-27926914.891e694
  • Build Modules: -

@madebyfabian
Copy link
Sponsor Collaborator

madebyfabian commented Feb 9, 2023

@StefanoTesla probably because the fix not released yet, just merged, right?

Edit: oh I see a new nuxt version has been released 1hr ago.. awesome 💚💚🎉🎉

@StefanoTesla
Copy link

@StefanoTesla probably because the fix not released yet, just merged, right?

Edit: oh I see a new nuxt version has been released 1hr ago.. awesome 💚💚🎉🎉

I seen the merged changes reflected to the My repo.
I update now to the last version but warning isnt disappeared

@Barbapapazes
Copy link
Contributor

Fix is released in nitro 2.2.1 but nuxt 3.2.0 use nitro 2.2.0

@StefanoTesla
Copy link

Fix is released in nitro 2.2.1 but nuxt 3.2.0 use nitro 2.2.0

Nuxt 3.2.0 dependency use "nitropack": "^2.2.1"

"node_modules/nuxt": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/nuxt/-/nuxt-3.2.0.tgz", "integrity": "sha512-8jAYyjU1Ht+MXPLLDIdIUmV56KiI0g7KusKwzvqn+vlzyCNtSHg2W/VBCGw5QWplb/MXruogcMl2sDenlQRZFg==", "dev": true, "dependencies": { [..] "nitropack": "^2.2.1", "nuxi": "3.2.0", [..] },

  • Operating System: Windows_NT
  • Node Version: v19.4.0
  • Nuxt Version: 3.2.0
  • Nitro Version: 2.2.1
  • Package Manager: npm@9.4.2
  • Builder: vite
  • User Config: pages, css, modules, i18n
  • Runtime Modules: @nuxtjs/i18n@8.0.0-beta.9-27926914.891e694
  • Build Modules: -

@pi0
Copy link
Member

pi0 commented Feb 9, 2023

I hope issue is fixed in last release. You can try nuxi upgrade —force

in cast of 2.2.1 reproducing issues, please ping me with a reproduction to reopen :)

@ram-you
Copy link

ram-you commented Feb 10, 2023

Hi @pi0
I have the latest version though.
Nuxi 3.2.0
Nuxt 3.2.0 with Nitro 2.2.1
but still have the warning:

Export "useNitroApp" of module "node_modules/nitropack/dist/runtime/app.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "node_modules/nuxt/dist/core/runtime/nitro/renderer.mjs" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.

Thank you

@manniL
Copy link
Member

manniL commented Feb 10, 2023

@ram-you Could you please provide a reproduction? 🙏

@StefanoTesla
Copy link

StefanoTesla commented Feb 10, 2023

@ram-you Could you please provide a reproduction? 🙏

https://github.com/StefanoTesla/nuxtproject

@ram-you
Copy link

ram-you commented Feb 10, 2023

Hi @manniL
My code was working fine from early versions of Nuxt3 up to v3.1.2.
The warning message started after updating to Nuxt 3.2.0 and I think I'm not the only one.
If some more people could verify that would be great!

@wallchr

This comment was marked as duplicate.

@emaia

This comment was marked as duplicate.

@manniL manniL reopened this Feb 10, 2023
@manniL
Copy link
Member

manniL commented Feb 10, 2023

Thanks! Can be reproduced with a clean starter running npm build

@danielroe
Copy link
Member

Indeed, it's still present in 3.2.0, but it should be resolved in the edge channel, or in the next release.

@StefanoTesla
Copy link

Thank you for clarification 🙂

@fabianwohlfart
Copy link

I get this error with a fresh package and the build command.
Do we have any solutions to it?

Nuxt 3.6.5 with Nitro 2.6.0 

✔ Generated public .output/public                                                                                                                                         nitro 11:34:52
ℹ Building Nitro Server (preset: node-server)                                                                                                                             nitro 11:34:52
Export "useNitroApp" of module "../../../node_modules/nitropack/dist/runtime/app.mjs" was reexported through module "../../../node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "server/api/test.js" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.
✔ Nitro server built  

server/api/test.js

export default defineEventHandler(async (event) => {
  const nitroApp = useNitroApp()
  console.log(nitroApp)
  return true
})

Copy link
Member

Thanks for the report. Raised #22785.

@cshomo11
Copy link

cshomo11 commented Dec 8, 2023

I'm seeing a similar circular dependency in Nuxt 3.8.2 with Nitro 2.8.1 (and Nuxt 3.8.0 before I tried an update) where useRuntimeConfig is being flagged.

 ERROR  [worker reload] [worker init] Cannot access '_sharedRuntimeConfig' before initialization                                                             12:33:34 PM

  at useRuntimeConfig (node_modules/nitropack/dist/runtime/config.mjs:13:5)
  at <anonymous> (node_modules/nitropack/dist/runtime/route-rules.mjs:11:16)
  at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

And these errors when I run the build command

Export "useRuntimeConfig" of module "node_modules/nitropack/dist/runtime/config.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "node_modules/nuxt/dist/core/runtime/nitro/paths.js" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.
Export "getRouteRules" of module "node_modules/nitropack/dist/runtime/route-rules.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "node_modules/nuxt/dist/core/runtime/nitro/renderer.js" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.
Export "useRuntimeConfig" of module "node_modules/nitropack/dist/runtime/config.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "node_modules/nuxt/dist/core/runtime/nitro/renderer.js" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.
Export "useRuntimeConfig" of module "node_modules/nitropack/dist/runtime/config.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in ".nuxt/dist/server/server.mjs" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.

At first, I suspected this was due to a use of useRuntimeConfig in a Nitro hook plugin. But I rewrote that plugin to remove the use of the runtime config, and it was still being flagged in the build and when trying to run dev.

@dargmuesli
Copy link
Member

dargmuesli commented Dec 11, 2023

I also started seeing this again (reference nuxt-modules/og-image#122).

Export "useNitroApp" of module "../node_modules/.pnpm/nitropack@2.8.1/node_modules/nitropack/dist/runtime/app.mjs" was reexported through module "../node_modules/.pnpm/nitropack@2.8.1/node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "../node_modules/.pnpm/nuxt-og-image@3.0.0-rc.4_@nuxt+devtools@1.0.5_@vue+compiler-core@3.3.11_nuxt@3.8.2_postcss@8._ny3u4kyggqg4uuvdg57xciguji/node_modules/nuxt-og-image/dist/runtime/core/utils/resolveRendererContext.mjs" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.

@cshomo11
Copy link

cshomo11 commented Jan 9, 2024

To provide a little more context for my previous comment, I did some digging in the generated .nuxt/dev/index.mjs. It looks to be referencing useRuntimeConfig well before it is defined in the file.

In my file it starts with this block at line 920:

const config$1 = useRuntimeConfig();
const _routeRulesMatcher = toRouteMatcher(
  createRouter({ routes: config$1.nitro.routeRules })
);
function createRouteRulesHandler(ctx) {
  return eventHandler((event) => {
    const routeRules = getRouteRules(event);
    if (routeRules.headers) {
      setHeaders(event, routeRules.headers);
    }
    if (routeRules.redirect) {
      return sendRedirect(
        event,
        routeRules.redirect.to,
        routeRules.redirect.statusCode
      );
    }
    if (routeRules.proxy) {
      let target = routeRules.proxy.to;
      if (target.endsWith("/**")) {
        let targetPath = event.path;
        const strpBase = routeRules.proxy._proxyStripBase;
        if (strpBase) {
          targetPath = withoutBase(targetPath, strpBase);
        }
        target = joinURL(target.slice(0, -3), targetPath);
      } else if (event.path.includes("?")) {
        const query = getQuery(event.path);
        target = withQuery(target, query);
      }
      return proxyRequest(event, target, {
        fetch: ctx.localFetch,
        ...routeRules.proxy
      });
    }
  });
}

That is being imported from nitropack/dist/runtime/route-rules.mjs.

However the definition of useRuntimeConfig that is found in nitropack/dist/runtime/config.mjs is not injected into my dev/index.mjs until line 1795.

const _sharedRuntimeConfig = _deepFreeze(
  _applyEnv(klona(_inlineRuntimeConfig))
);
function useRuntimeConfig(event) {
  if (!event) {
    return _sharedRuntimeConfig;
  }
  if (event.context.nitro.runtimeConfig) {
    return event.context.nitro.runtimeConfig;
  }
  const runtimeConfig = klona(_inlineRuntimeConfig);
  _applyEnv(runtimeConfig);
  event.context.nitro.runtimeConfig = runtimeConfig;
  return runtimeConfig;
}
const _sharedAppConfig = _deepFreeze(klona(appConfig));
function useAppConfig(event) {
  if (!event) {
    return _sharedAppConfig;
  }
  if (event.context.nitro.appConfig) {
    return event.context.nitro.appConfig;
  }
  const appConfig$1 = klona(appConfig);
  event.context.nitro.appConfig = appConfig$1;
  return appConfig$1;
}

So as far as I can tell, nitropack/dist/route-rules.mjs refers to useRuntimeConfig and imports that from nitropack/dist/config.mjs. But when built as a dev script, the import from nitropack/dist/config.mjs is being injected well after the route-rules.mjs reference.

This is currently blocking us from running the dev environment locally and I cannot tell if this is just a import or reference I need to move around or if this is something inside of Nitro.

If it is helpful I can move this to a new bug report.

@dargmuesli
Copy link
Member

Given the detailed description, I reopen this issue.

@dargmuesli dargmuesli reopened this Jan 10, 2024
@danielroe
Copy link
Member

That looks like it might be a nitropack issue...

What do you think @pi0?

@pi0
Copy link
Member

pi0 commented Jan 12, 2024

Is there a recent minimal reproduction based on either Nuxt or Nitro latest versions?

Copy link
Contributor

Would you be able to provide a reproduction? 🙏

More info

Why do I need to provide a reproduction?

Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making.

What will happen?

If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritize it based on its severity and how many people we think it might affect.

If needs reproduction labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it.

How can I create a reproduction?

We have a couple of templates for starting with a minimal reproduction:

👉 https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz
👉 https://codesandbox.io/s/github/nuxt/starter/v3-codesandbox

A public GitHub repository is also perfect. 👌

Please ensure that the reproduction is as minimal as possible. See more details in our guide.

You might also find these other articles interesting and/or helpful:

@cshomo11
Copy link

I can try to track down a minimal reproduction of the issue. I am not sure exactly what triggered the issue originally as it occured during my projects transition from Nuxt 2 to Nuxt 3. I suspect maybe it was when we introduced custom router rules but I will see if I can make a new repo with the issue.

@cshomo11
Copy link

Ah I found the issue. We had a config that was being imported into our core layer's app config. That config was importing composable functions. When I commented those out the error went away. I'm going to rework the config so that we don't directly import those composables into the config. Just a casualty of converting a lot of JSON based configs into one main app.config.ts file.

@dargmuesli
Copy link
Member

So, it was a project-specific issue, not something to fix in Nuxt? Then I'd close this issue again 🙌

@cshomo11
Copy link

Yes this can be closed out again. Sorry for the confusion

@pi0 pi0 closed this as completed Jan 17, 2024
@noook
Copy link

noook commented Feb 7, 2024

I can reproduce with Nuxt 3.10.1, Nitropack 2.8.1

Export "useNitroApp" of module "node_modules/nitropack/dist/runtime/app.mjs" was reexported through module "node_modules/nitropack/dist/runtime/index.mjs" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "server/api/index.ts" to point directly to the exporting module or reconfigure "output.manualChunks" to ensure these modules end up in the same chunk.

Here is the link to reproduction: https://codesandbox.io/p/sandbox/quirky-lovelace-pd9l7w

@tberk
Copy link
Contributor

tberk commented Feb 9, 2024

Getting the same errors on my build too, which uses nitro plugins..

@pi0
Copy link
Member

pi0 commented Feb 9, 2024

please open a new issue with reproduction 🙏🏼

@fabianwohlfart
Copy link

Did you guys try this? #22785 (comment)

import { useNitroApp } from '#internal/nitro/app'

@noook
Copy link

noook commented Feb 9, 2024

@fabianwohlfart This indeed suppressed the error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet