nuxt: Server-side rendering with shared vue components in yarn workspace results in "Cannot find module"
Version
Reproduction link
Steps to reproduce
This is all inside one folder, to reproduce the exact error message it would live at /home/myuser/repos/core
- Create a folder structure like
common/vue/components/atoms/withbox.vueat the top and apackage.jsonatcommon/vuedefining thenameascommon-vue - Create a folder structure like
example/testcontaining a nuxt project - Add both as yarn workspaces
- In the nuxt project, import the common vue component with
import EBox from 'common-vue/components/atoms/box'; - Run nuxt dev or build/run commands
What is expected ?
The site works as expected, showing the shared vue component
What is actually happening?
You see a page with a NuxtServerError stating:
Cannot find module 'common-vue/components/atoms/box' from '/home/myuser/repos/core/example/test'
Additional comments?
We tried quite a few things:
- Replacing the yarn created symlinks in
node_moduleswith an actual copy of the respective folder - Setting
rootDir,modulesDirand webpack’snodeExternalsto various combinations, sometimes managing to change the error toCannot find module 'common-vue/components/atoms/box' from '/home/myuser/repos/core/'(i.e. pointing to where the root levelnode_modulesare) - Setting the
bundleRenderer’sbaseDir
My best guess right now is that it is tied to vue-ssr’s externals config that only whitelists css by default as documented here. I sadly can’t find whether/where nuxt sets this value and didn’t find a way to override it.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 9
- Comments: 20 (1 by maintainers)
Hi! I will share a demo repository soon 😃
Having the same issue, also using Nuxt TypeScript, but seems to be an issue even without it. @pi0 did you share a demo repository anywhere, or would you like me to create one?
About this, I solve solation by some config. After setup lerna.js and yarn workspace for each package in nuxt.config.js set
modulesDir: ['../../node_modules']my directorythen for link system design to the public, I run this command to the public directory
finally, import my vue component in public component by
but for short than src you also can add alias in nuxt.config
I hope useful.
Here’s an example repository (it’s using TypeScript & Lerna thought, but I’m pretty sure it’s the same underlying issue)
https://github.com/RobinBertilsson/nuxt-lerna-6929
Steps to reproduce
yarn && yarn lerna bootstrapuniversaltospaJust ran into this issue today and spent that last 3-4 hours trying to fix it! 😞
None of the workarounds are remotely ideal. The one I’ve decided to go with is to define
common-vueas an alias that points directly to the project folder:Also, need a similar path mapping in
tsconfig.jsonfor TypeScript projects:It’s important that
common-vueisn’t symlinked intonode_modules. In my case, I actually defined my alias with a different name (e.g.common-vue-alias) so that I don’t forget to fix it properly once there’s a better solution. Speaking of which…Anyone know a better solution? Like I said, none of the workarounds are ideal, and having a shared component library in a monorepo must be a common use case. Is it not?