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

useNuxtApp is not defined after build #1268

Open
codeofsumit opened this issue Jul 15, 2024 · 7 comments
Open

useNuxtApp is not defined after build #1268

codeofsumit opened this issue Jul 15, 2024 · 7 comments

Comments

@codeofsumit
Copy link

Environment

  • Operating System: Darwin
  • Node Version: v20.15.1
  • Nuxt Version: 2.17.4
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.7
  • Package Manager: [email protected]
  • Builder: webpack
  • User Config: bridge, ssr, modern, telemetry, components, head, loading, build, publicRuntimeConfig, serverHandlers, devServerHandlers, devServer, typescript, nitro, buildModules
  • Runtime Modules: -
  • Build Modules: (), @nuxt/[email protected]

Reproduction

https://github.com/tresorone/tresor-repro

Describe the bug

As part of our nuxt 2 to nuxt 3 migration we ran across an error where useNuxtApp is not defined in our build, while it works fine in dev.

To reproduce:

  1. Clone the reproduction repo
  2. npm i
  3. npm run build
  4. npm start
  5. open localhost:3000 in your browser and open the javascript console

You should see the error useNuxtApp is not defined.

Additional context

No response

Logs

No response

@wattanx
Copy link
Collaborator

wattanx commented Aug 2, 2024

It can be worked around by explicitly importing.

import { useNuxtApp } from #imports'

https://stackblitz.com/edit/github-hqqmha

@codeofsumit
Copy link
Author

codeofsumit commented Aug 3, 2024

@wattanx thanks!

import { useNuxtApp } from ‘#imports'

That seems to fix it so the web app starts. However, tests have a problem with this:

image

Anything special I need to do in tests that test the components that use this import workaround?

@wattanx
Copy link
Collaborator

wattanx commented Aug 4, 2024

#imports is alias.
You can check what alias it is by looking at .nuxt/tsconfig.json.

If you are using vitest, you can set this in resolve.alias.
https://vitest.dev/config/#alias

If alias is not used, the following writing style will also work.

import { useNuxtApp } from '@nuxt/bridge/dist/runtime'

@codeofsumit
Copy link
Author

codeofsumit commented Aug 4, 2024

@wattanx we are using jest and @vue/test-utils.
There is no tsconfig.json in .nuxtunfortunately.

image

I replaced all imports of import { useNuxtApp } from '#imports'; with import { useNuxtApp } from '@nuxt/bridge/dist/runtime' but unfortunately it did not help.

Cannot find module '@nuxt/bridge/dist/runtime' from 'composables/usePortfolioReloadHandler.js

This is what thenuxt bridge folder looks like:

image

@codeofsumit
Copy link
Author

@wattanx I also tried it with

import { useNuxtApp } from '@nuxt/bridge/dist/runtime/index.mjs';

Now the error is this:

FAIL helper/index.test.js
● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Can this be fixed by doing some magic in jest.config.js?
I tried transformIgnorePatterns: ['/node_modules/(?!@nuxt/bridge)'], but it didn't help.

@wattanx
Copy link
Collaborator

wattanx commented Aug 4, 2024

I replaced all imports of import { useNuxtApp } from '#imports'; with import { useNuxtApp } from '@nuxt/bridge/dist/runtime' but unfortunately it did not help.

I can't reproduce it. If you can provide me with a reproduction, I can investigate more.
https://stackblitz.com/edit/github-hqqmha-zf5tch

we are using jest and @vue/test-utils.

I also think jest can resolve alias by using moduleNameMapper.
https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring

@codeofsumit
Copy link
Author

@wattanx adding the alias via moduleNameMapper helped!

Unfortunately jest can't deal with ESM - I'm trying to figure this out using babel but no luck yet.

This is the jest config

module.exports = {
  setupFiles: ['./jest.setup.js'],
  testEnvironment: 'jsdom',
  moduleNameMapper: {
    '/assets/json/generated/(.*)': '<rootDir>/tests/__mocks__/$1',
    '^@/(.*)$': '<rootDir>/$1',
    '^~/(.*)$': '<rootDir>/$1',
    '^vue$': 'vue/dist/vue.common.js',
    '^#imports$': '@nuxt/bridge/dist/runtime/index.mjs',
  },
  moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node', 'vue'],
  transform: {
    '\\.m?jsx?$': 'jest-esm-transformer',
    '^.+\\.(js)$': 'babel-jest',
    '.*\\.(vue)$': '@vue/vue2-jest',
    '^.+\\.(css)$': '<rootDir>/cssTransform.js',
  },
  transformIgnorePatterns: ['/node_modules/(?!(swiper|@nuxt/bridge)/)'],
};

And this is the babel config

module.exports = {
  presets: ['@babel/preset-env'],
  plugins: ['@babel/plugin-transform-export-namespace-from'],
};

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

2 participants