nuxt: AWS Amplify generates runtime crash
Environment
- Operating System:
Windows_NT - Node Version:
v16.13.1 - Nuxt Version:
3.0.0-27366343.1d741cb - Package Manager:
yarn@1.22.17 - Bundler:
Vite - User Config:
publicRuntimeConfig,typescript,css,buildModules - Runtime Modules:
- - Build Modules:
@vueuse/nuxt@7.5.3
Reproduction
I tried to provide a minimal reproduction in this sandbox instance, although I cannot really understand how it works. Refer to https://github.com/nuxt/nuxt.js/issues/13277.
I’m providing in that environment some testing configurations for the convenience of the developers, I will shut them down when the problem is solved.
Describe the bug
When using @aws-amplify/auth for connecting to an AWS Cognito user pool, nuxt provides a runtime error very hard to debug. (See below).
In general, it would be very nice to have a more informative stacktrace about these kinds of issues.
Additional context
No response
Logs
Stacktrace of the bug
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\..\repo\url' imported from C:\Users\..\repo\.nuxt\dist\server\server.mjs
at new NodeError (node:internal/errors:371:5)
at finalizeResolution (node:internal/modules/esm/resolve:416:11)
at moduleResolve (node:internal/modules/esm/resolve:932:10)
at defaultResolve (node:internal/modules/esm/resolve:1044:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
at ESMLoader.import (node:internal/modules/esm/loader:276:22)
at importModuleDynamically (node:internal/modules/esm/translators:111:35)
at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
at $id_28e5ebab (file:///C:/Users/../repo/.nuxt/dist/server/server.mjs:13500:233) {
code: 'ERR_MODULE_NOT_FOUND'
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 23 (10 by maintainers)
I finally was able to solve the issue, thanks to https://github.com/nuxt/framework/discussions/2308, to https://github.com/nuxt/nuxt.js/issues/12803, and to nuxt3 docs, I’ll summarize the solution here, of which you can see a minimal reproduction in this sandbox environment.
Step 1 - Polyfill definition:
Include a polyfill like in https://github.com/nuxt/nuxt.js/issues/12803, (a typical location is
public/js/polyfill.js, but any other is fine as long as you update the paths accordingly) with the following content:Step 2 - Polyfill inclusion:
Include the polyfill as a loaded script at the main entry point of your app, a typical example is shown here for a basic
app.vue:Step 3 - Plugin setup:
Add the information for setting up an Amplify plugin. The following shows a typical usage for just the
Authmodule of Amplify, other can be added similarly. Note also that this steps assumes anaws-exports.tsfile at the root of your nuxt3 app containing the settings for Amplify, an example can be found in the sandbox environment at the beginning of this post, otherwise refer to the official Amplify documentation for the details.Step 4 - Transpiling/aliasing libraries:
Set the nuxt config to alias the amplify libraries of
aws-amplify:That’s it for the setup, now it can be used in various way. Note, however, as pointed out in https://github.com/nuxt/framework/discussions/2308, that plugin dependencies might not be available at setup time, so two usages approach can be considered. I show here an example for an
authenticatedvariable from mt-u in a classical vue style and in a more idiomatic composable style.Approach 1:
onMounted()Approach 2:
composablewhich can then be used in components in the following way:
I’ll close the issue here, I recommend to continue on the related discussion https://github.com/nuxt/framework/discussions/2308, in case of other issues.
Hi, I am having the exact same issue. Not only the url but several packages that come from amplify are not properly resolved.
Thanks again! The Authenticator component works fine, but I have not been able to update my own components when the connection status changes. From the source code there seems to be an AmplifyEventBus, I’ll check if this can be used.
@markovickosta Thanks for this fix!
This was all I needed to get
API.graphqlworking properly. I defined it inside a client-only plugin to avoid any server-side issues, especially since I’d only be using GraphQL on the client side.plugins/graphql.client.ts
@ennioVisco Thank you very much for trying to reproduce my error and helping me out!
@markovickosta I checked and tried to reproduce it here: https://codesandbox.io/s/long-resonance-hcodk3?file=/nuxt.config.ts
It seems that when you don’t have
ssr: falsein yournuxt.config.tsit doesn’t work. However, having ssr means that you are running vianodeon some server, which in that case it probably doesn’t need the wholealiasblock in the configs at least… I never explored that case tbh… Note that in SSG you must havessr: false.@ennioVisco I still have problems with the same issue as them. I created the same polyfill as you mentioned in #2874 (comment) My plugin looks like this:
My nuxt.config.ts:
And my package.json:
When I run ‘npm run dev’ I get the following error:
I can run ‘npm run build’ and ‘npm run preview’ after, without any problems. BUT when I try to publish that to amplify hosting I get “AccessDenied”.