https://codesandbox.io/s/codesandbox-nuxt-vb0ki
With a pretty intro-level configuration of the auth module like so (excerpt of nuxt.config.js):
router: {
middleware: ['auth'],
},
modules: [
'@nuxtjs/auth',
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios',
],
/*
** Auth module configuration
** See https://auth.nuxtjs.org/guide/scheme.html
*/
auth: {
strategies: {
auth0: {
client_id: process.env.AUTH0_CLIENT_ID,
audience: process.env.AUTH0_AUDIENCE,
// For some reason, even though this `domain` property should be all we
// need, it isn't; so I have to add the following manually below:
domain: process.env.AUTH0_DOMAIN,
authorization_endpoint: `https://${process.env.AUTH0_DOMAIN}/authorize`,
userinfo_endpoint: `https://${process.env.AUTH0_DOMAIN}/userinfo`,
},
},
redirect: {
login: '/',
logout: '/',
home: '/',
callback: '/logged-in',
},
},
/*
** Axios module configuration
** See https://axios.nuxtjs.org/options
*/
axios: {},
(Where all my process.env.AUTH0_* env vars are correctly defined.)
What happens when I run the app via yarn dev--everything works perfectly, namely I shouldn't get that unknown middleware SSR error.
Basically when running with yarn dev everything works perfectly, but when running with yarn build and then yarn start (and I mean my app does practically _nothing_ but login/logout, have a homepage, a profile page (for authenticated users only) and a logged-in route that redirects to the homepage), I get the following error from the Nuxt server's SSR:

Again, this error only happens when running from the built universal app server (i.e. yarn build && yarn start) and there are no errors at all and the whole workflow works perfectly with yarn dev.
Additionally, notice my comment in that excerpt of my nuxt.config.js about the domain not working: For some reason, even though this domain property should be all I need for the login/fetchUser functionality to work with the auth0 strategy, it isn't; so I still had to add the authorization_endpoint and userinfo_endpoint options manually.
Actually, I just double-checked. This isn't a bug. It was happening because I'm using webpack's tree-shaking (i.e. I'm adding sideEffects: true to the webpack rule that processes my JS/TS/etc), and while I had added node_modules to the exclude array, I needed to add .nuxt to it too so the server code in particular won't have its unused imports pruned.
I just double-checked about the domain thing too, and that too was due to this over-zealous tree-shaking.
Goes to show sometimes all you have to do is form the question to get the answer 馃し鈥嶁檪. All the same, could be worth thinking about to make this repo tree-shaking friendly. 鉂わ笍 this module; thanks!
i got the same problem, can u teach me the solve, thanks
Most helpful comment
Actually, I just double-checked. This isn't a bug. It was happening because I'm using webpack's tree-shaking (i.e. I'm adding
sideEffects: trueto the webpack rule that processes my JS/TS/etc), and while I had addednode_modulesto the exclude array, I needed to add.nuxtto it too so the server code in particular won't have its unused imports pruned.I just double-checked about the domain thing too, and that too was due to this over-zealous tree-shaking.
Goes to show sometimes all you have to do is form the question to get the answer 馃し鈥嶁檪. All the same, could be worth thinking about to make this repo tree-shaking friendly. 鉂わ笍 this module; thanks!