bridge icon indicating copy to clipboard operation
bridge copied to clipboard

useNuxtApp is not defined after build

Open codeofsumit opened this issue 1 year ago • 7 comments

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

codeofsumit avatar Jul 15 '24 18:07 codeofsumit

It can be worked around by explicitly importing.

import { useNuxtApp } from ‘#imports'

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

wattanx avatar Aug 02 '24 14:08 wattanx

@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?

codeofsumit avatar Aug 03 '24 22:08 codeofsumit

#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'

wattanx avatar Aug 04 '24 05:08 wattanx

@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 avatar Aug 04 '24 08:08 codeofsumit

@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.

codeofsumit avatar Aug 04 '24 08:08 codeofsumit

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

wattanx avatar Aug 04 '24 11:08 wattanx

@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'],
};

codeofsumit avatar Aug 04 '24 18:08 codeofsumit