Vuepress: Global vuepress fail to resolve custom theme

Created on 10 May 2018  Â·  11Comments  Â·  Source: vuejs/vuepress

Here's the simplest way to repro it:

  1. Add a custom theme (take vuepress-theme-vue for example) to a new repo:
mkdir testrepo
cd testrepo
yarn add vuepress-theme-vue
  1. Create src and base files, then the directory will be like this:
+-- src/
|   +-- .vuepress/
|       +-- config.js
|   +-- README.md
+-- node_modules
|   +-- vuepress-theme-vue/
+-- package.json
+-- yarn.lock
  1. Set theme to vue in config.js
module.exports = {
  theme: 'vue'
}
  1. Run global vuepress
vuepress dev src
  1. Error
Error: [vuepress] Failed to load custom theme "vue". File vuepress-theme-vue/Layout.vue does not exist.
  1. Possible solutions
  • Install custom theme globally (i.e. yarn global add vuepress-theme-vue)
  • (Or) Install vuepress locally (i.e. yarn add vuepress) and run it locally (i.e. yarn vuepress dev src)

The cause is that we are using require.resolve() to resolve custom theme:

https://github.com/vuejs/vuepress/blob/145cf4f244a9b2fff3ee9a3c59985dcda9642aa6/lib/prepare.js#L139-L140

Is there a better solution?

All 11 comments

Perhaps something like autoloading the theme folder from npm registry by fetching it would be nice or maybe change the documentation saying that if you specify a custom theme (which isn't local) you have to yarn add vuepress-theme-something.

@hmatalonga Come on!

The 2nd solution produces the same problem (from my short testing) since when add vuepress, no theme dependency is added, it will still be missing the vuepress-theme-vue/Layout.vue as the folder doesn't exist anywhere.

Look at the last line of the first step to repro it:

yarn add vuepress-theme-vue

Although you add the theme locally, you cannot use it via global vuepress.

I just took vuepress-theme-vue for example, you could change it to vuepress-theme-test to have a try.

@meteorlxy my apologies, I don't known how I how missed the first steps... I removed my dumb comments from the previous message xD

Anyway for using it via global vuepress does the idea of somehow fetching the theme contents makes sense to you?

@hmatalonga
As a global cli, it should work, I think.

In my opinion, we must consider every possible & sensible scenario

In my opinion, we must consider every possible & sensible scenario

once again, I want to emphasize the intention of Evan: VuePress is mainly for vuejs.

So we don't have to think about all possible scenarios.

@ulivz Sorry but I didn't get what you mean here.

When using global vuepress, you have to make the theme global. That's unreasonable

So I use "sensible" together. Not all possible scenarios should we consider, but those sensible ones.

I don't think this feature is sensible. If you just want to play around with VuePress, you can install it globally. but since you have a custom theme installed at local, why not installing VuePress at local too?

"If you just want to play around with VuePress" - and try some different themes randomly - this is sensible.

Just because that we don't have various themes now

If you just want to play around with VuePress, and try some different themes randomly

it makes sense.

Hi @ulivz @meteorlxy ,
I update my vuepress global-cli to version 0.10.0 and follow folder structure from @meteorlxy above, then I run 'vuepress dev src' but still produce error, the same as point.5 above.

However if I modify ~/.config/yarn/global/node_modules/vuepress/lib/prepare.js and add
path.resolve('node_modules') in the paths array
as in
paths: [ path.resolve(__dirname, '../node_modules'), path.resolve('node_modules'), path.resolve(sourceDir) ]

It fix the error, I guess it resolve the theme location within main project working directory node_modules folder, if that make sense?
What do you think? or did I miss something?

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lesliecdubs picture lesliecdubs  Â·  3Comments

kid1412621 picture kid1412621  Â·  3Comments

gaomd picture gaomd  Â·  3Comments

genedronek picture genedronek  Â·  3Comments

ynnelson picture ynnelson  Â·  3Comments