nuxt / module-builder

Complete solution to build and ship Nuxt modules.
MIT License
224 stars 24 forks source link

Could not resolve import "#app" #242

Closed trandaison closed 5 months ago

trandaison commented 6 months ago

I created a module https://github.com/trandaison/nuxt-3-auth, the playground is working perfectly. However, when I run npm run prepack, I got this error

(node-resolve plugin) Could not resolve import "#app" in /nuxt-3-auth/src/runtime/services/AuthStorage.ts using imports defined in /nuxt-3-auth/package.json.
(node-resolve plugin) Could not resolve import "#app" in /nuxt-3-auth/src/runtime/services/AuthStorage.ts using imports defined in /nuxt-3-auth/package.json.
"#app" is imported by "src/runtime/services/AuthStorage.ts", but could not be resolved – treating it as an external dependency.

 WARN  Build is done with some warnings:
- Inlined implicit external #app

 ERROR  Exiting with code (1). You can change this behavior by setting failOnWarn: false . 

Screenshot 2024-03-19 at 11 17 13

Since AuthStorage.ts is not using any import from #app anymore, I don't know why it still throws this error.

=====

trandaison commented 6 months ago

My workaround is set failOnWarn: false by create a build.config.ts

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  failOnWarn: false,
});

However, I think this is a bug, should be fixed in the module builder itself, and the docs should show an example how to custom the build config as well.

danielroe commented 6 months ago

The reason this is happening is that your module is importing types from the runtime code, which itself is augmenting #app.

Instead, you should likely extract your types into a location where they can be shared between your runtime and build-time code.

If you do add config, it should likely be to add #app to your externals, like this:

import { defineBuildConfig } from "unbuild"

export default defineBuildConfig({
  externals: ['#imports', '#app'],
})

We may be able to improve this experience as having shared types between build/runtime is often necessary.