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

Build hanging when using database after 3.7.0 #23334

Closed
romainsimon opened this issue Sep 21, 2023 · 4 comments
Closed

Build hanging when using database after 3.7.0 #23334

romainsimon opened this issue Sep 21, 2023 · 4 comments

Comments

@romainsimon
Copy link

Environment


  • Operating System: Darwin
  • Node Version: v16.17.0
  • Nuxt Version: 3.7.0
  • CLI Version: 3.9.0
  • Nitro Version: 2.6.3
  • Package Manager: yarn@1.22.19
  • Builder: -
  • User Config: experimental, routeRules, runtimeConfig, devtools, modules, image, pinia, css, plausible, tailwindcss
  • Runtime Modules: @nuxtjs/tailwindcss@6.8.0, @nuxt/content@2.8.2, @nuxt/image@1.0.0-rc.2, @pinia/nuxt@0.4.11, @nuxtjs/plausible@0.2.3, @vueuse/nuxt@10.4.1, @pinia-plugin-persistedstate/nuxt@1.1.2
  • Build Modules: -

I have the same issue running locally, and when building on Netlify / Vercel

Reproduction

  1. Install Nuxt 3.7.0 to 3.7.3
  2. Connect to a database. For example inplugins/index.ts
import mongoose from 'mongoose'

export default async () => {
  const config = useRuntimeConfig()
  try {
    await mongoose.connect(config.mongoUrl)
    console.log('✔ Connected to MongoDB')
  } catch (e) {
    console.error(e)
  }
}
  1. Run nuxt build

Describe the bug

When running nuxt build, the build keeps hanging after:

✔ You can preview this build using node .output/server/index.mjs

I was able to find this comes from my database connection, which works fine in 3.6.5 but blocks the build when upgrading to Nuxt from 3.7.0 to 3.7.3

The issue is the same if I have my database connection in server/index.ts

Additional context

No response

Logs

No response

@pi0
Copy link
Member

pi0 commented Sep 21, 2023

Hi. Are you creating db instance in server/plugins/*.ts right? You need to close the database connection after generated. (previously we were always force closing)

Here is a workaround:

export default defineNuxtConfig({
  hooks: {
    close: () => {
      // Workaround for https://github.com/nuxt/cli/issues/169
      process.exit(0)
    }
  }
})

Let's track via nuxt/cli#169 & please ping me if above workaround didn't work.

@romainsimon
Copy link
Author

@pi0 Yes it works, thanks a lot for the workaround!

@CharlesBT
Copy link

Hi. Are you creating db instance in server/plugins/*.ts right? You need to close the database connection after generated. (previously we were always force closing)

Here is a workaround:

export default defineNuxtConfig({
  hooks: {
    close: () => {
      // Workaround for https://github.com/nuxt/cli/issues/169
      process.exit(0)
    }
  }
})

Let's track via nuxt/cli#169 & please ping me if above workaround didn't work.

This workaround has nasty side effect:
1- nuxt prepare doesn't generate ts.config files in ./.nuxt folder
2- nuxt-vitest stop working properly

@phuze
Copy link

phuze commented May 1, 2024

This workaround has nasty side effect:
1- nuxt prepare doesn't generate ts.config files in ./.nuxt folder
2- nuxt-vitest stop working properly

As an alternative, you could consider using an environment variable.

Azure pipeline:

Create a variable group and define a variable such as IS_PIPELINE (the value doesnt matter).
Link your variable group to your pipeline, which exposes those as environment variables in your code.

- job: Build
  variables:
    - group: MyVariableGroup

BitBucket pipeline:

Export a variable within the build step of your pipeline, making it available in your code.

- step:
    script:
      - export IS_PIPELINE=yes
      - npm run build

Github action:

Define an environment variable for your build step, making it available in your code.

steps:
  - name: Build
    env:
      IS_PIPELINE: yes
    run: npm run build

You can then test for the existence of this environment variable, as a means of controlling whether you initialize a connection within your plugins.

let redis = null
if(!('IS_PIPELINE' in process.env)) {
  redis = new Redis(azureRedis)
}

Provided that you explicitly define your environment variables with the runtimeConfig and publicRuntimeConfig configs in nuxt.config.(ts|js), your IS_PIPELINE variable shouldn't make it into your production build, ensuring this effectively addresses this issue during build only.

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

No branches or pull requests

4 participants