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

could not resolve import when module isEnabled = false #454

Open
agracia-foticos opened this issue Jun 22, 2023 · 7 comments
Open

could not resolve import when module isEnabled = false #454

agracia-foticos opened this issue Jun 22, 2023 · 7 comments
Labels
enhancement An improvement that needs to be added p3 Minor issue

Comments

@agracia-foticos
Copy link

agracia-foticos commented Jun 22, 2023

Environment

  • Operating System: Linux
  • Node Version: v18.16.0
  • Nuxt Version: 3.5.3
  • Nitro Version: 2.4.1
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

We are using the same code, for multi-site APP... same sites has login and others sites hasnt login.

We have a wrapper to use useAuth, but we have a problem with
src/server/routes/api/auth/[...].ts file...

we have the module disabled but when in auth file try to import import { NuxtAuthHandler } from '#auth' throws an error

(node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. (node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. "#auth" is imported by "src/server/routes/api/auth/[...].ts", but could not be resolved – treating it as an external dependency.

If we use visual studio debugger, we cant start the app, only can with npx nuxi build and node start

Describe the bug

We are using the same code, for multi-site APP... same sites has login and others sites hasnt login.

We have a wrapper to use useAuth, but we have a problem with
src/server/routes/api/auth/[...].ts file...

we have the module disabled but when in auth file try to import import { NuxtAuthHandler } from '#auth' throws an error

(node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. (node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. "#auth" is imported by "src/server/routes/api/auth/[...].ts", but could not be resolved – treating it as an external dependency.

If we use visual studio debugger, we cant start the app, only can with npx nuxi build and node start

Additional context

No response

Logs

No response

@agracia-foticos
Copy link
Author

any solution?

@treygrr
Copy link

treygrr commented Jul 18, 2023

Make sure that in the application that you are referencing that imports #auth has the module for '@sidebase/nuxt-auth', imported in the nuxt.config.ts file.

I'm not totally sure how your mutli site is wired, but each nuxt instance needs the import to build out the type stubs/ definitions when the nuxt / vite lifecycle hooks are called. Without this module running, it will not build out your types and ts will show an error.

@agracia-foticos
Copy link
Author

image

The problem is that when I put enabled=false, I can't import
import { NuxtAuthHandler } from '#auth'

Therefore, the enabled does not solve anything... I want to enable and disable the module without having to change my programming

@treygrr
Copy link

treygrr commented Jul 19, 2023

I would then recommend you rename the above file to something else ie authApi.ts and then name a new file what it is currently, the import the old file based on if the auth is enabled or disabled. As of right now you are trying to import a reference when it's prescribing module import isn't built.

[...].ts'
export default function () {
// logic if the module is enabled
// import your authApi.ts file
https://stackoverflow.com/questions/13444064/typescript-conditional-module-import-export
}

@th3l0g4n
Copy link

In my opinion it would make sense to still provide the virtual module (#auth) and all of its exports even if the auth package is disabled and "just" disable the whole authorization logic inside the package.

@zoey-kaiser
Copy link
Member

@agracia-foticos I understand your problem, however I see multiple issues with exposing all the exports.

isEnabled tries to minimize how much is needed to load, to ensure that if you still have it inside, but don't need it your startup times are not hindered by NuxtAuth. Loading all the exports will have an effect on the performance and load times of the app, as there is just a lot that needs to be loaded.
I personally only see two use cases where you would want isEnabled to be false:

  1. Testing it with mocked Data
  2. Keeping your authentication setup, but disabling auth for a while.

For 1. I would recommend you look at my answer here on how to properly mock an authentication request (without disabling the module).
For 2. I personally think, commenting out the export would be enough for now, as loading all the files would not be worth it IMO.

If you have any other use cases for isEnabled or any ideas on how you would change the logic to:

  • Ensure your code can stay the same
  • and making sure no unneeded files are being loaded

please let me know and we can futhur discuss this matter!

@zoey-kaiser zoey-kaiser removed the bug label Feb 23, 2024
@zoey-kaiser zoey-kaiser linked a pull request Feb 23, 2024 that will close this issue
4 tasks
@zoey-kaiser zoey-kaiser added p3 Minor issue enhancement An improvement that needs to be added labels Feb 23, 2024
@zoey-kaiser zoey-kaiser removed their assignment Feb 23, 2024
@phoenix-ru
Copy link
Collaborator

Related to #596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement that needs to be added p3 Minor issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants