nuxt: Cannot use aliases inside serverMiddleware
Version
Reproduction link
https://codesandbox.io/s/vvm38xp7ny
Steps to reproduce
-
Creating a project with
create-nuxt-app -
Create the serverMiddleware:
project ├─ ... ├─ api └─ index.js -
Add some file, something like
util.js:project ├─ ... ├─ common └─ util.js -
Import the
common/util.jsinapi/index.jsimport express from 'express' import * as util from '~/common/util' ... -
Add the serverMiddleware to the
nuxt.config.js:export default { ... serverMiddleware: [ '~/api/index.js' ] }
What is expected ?
The aliases should be available inside serverMiddleware.
What is actually happening?
✖ Nuxt Fatal Error
Error: Cannot find module '~/common/util'
Additional comments?
Without aliases, we need to replace all imports that uses aliases, to relative path, in this case in api/index.js (server middleware).
But, what about if we use another import aliases inside common/util.js ?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 31
- Comments: 20 (4 by maintainers)
Commits related to this issue
- docs: update comments Remove TODO as this change would introduce this issue: https://github.com/nuxt/nuxt.js/issues/4580 — committed to maevsi/maevsi by dargmuesli 4 years ago
I hope Nuxt team can implement
aliasforserverMiddleware. Many people expect the feature !Some quick notes:
@nmfzone this solves your problem
import * as util from '../common/util'it has to be a relative path i tried already in the sandbox and no problem it runs hereoh ok! but i think they only work inside of Nuxt because Express will not use the alias and its included(imported) in express so on the Express viewpoint it has to be relative or use
__dirnamebecause according to Nuxt~/common/util.jsis correct but Node(express) cant resolve the path and pass it to Nuxt, thats the best of my limited knowledge. but if imported in nuxt it will resolve the path correctly than via a serverMiddlewarein the server export it like so
am on v-2.3.4. then you can go to
yourServerAddress:3000/api/blah-blahNot sure this is related to the same issue, but @pi0 did you notice the same behavior on the nuxt custom modules?
I’m using aliases like this:
It’s working quite well on all the application but on Node obviously. So I added the alises on node as well. It almost worked since a custom module with nothing in it (but using a relative import
import { ... } from '@core'is breaking my app:It’s happening right after I get the dev’ message “ℹ Initial build may take a while”, so I guess nuxt is loaded. Either way, node or nuxt does not seem to resolve my alias. Any idea why?
Thanks!
Running into the same issue here. It’s no problem to modify a few of the initial imports to use relative paths, but it’s the deep dependencies (things in
~/lib/models, and~/lib/queries) that fall down when they attempt to follow aliases.Does anyone have experience applying Webpack aliases outside of the primary client/server bundles?
I’m really stumped, and it seems like a silly thing, but the alternative is crazy module pathing like
../../../../../lib/models, etc.As this never has been implemented (AFAIK) this is a feature request and no bug report.
We don’t touch
serverMiddlewarebesides requiring the file and add it to the underlyingconnectinstance. So it’s not trivial to implement things like aliases (~, @).@devyaz Sorry, but I’ve explain it…please read it carefully…
Basically, I know it will solves the problem. But It didn’t make sense, since I use aliases too inside
common/util.js. ThanksSame issue. It forces not to use aliases in all shared code. I think this
bugrequest should be considered as early as possible.@devyaz That’s the problem. So, we can’t use aliases inside server middleware. Nuxt has define aliases, so it must be available in all context. When using my own server implementation (not server middleware), I just need to use Backpack and added aliases to it’s config, to make node recognize my aliases.