Stephan-v Stephan-v - 3 years ago 172
Node.js Question

How to publish a library of Vue.js components?

I am working on a project containing a Vuex module and an abstract components that users can extend from.

I would love to publish this on NPM to clean up my codebase and pull this away from my project as a solid well tested module. I have specified the main file in

to load an index which imports everything I want to expose:

The index contains this at the moment:

import AbstractFilter from './src/components/filters/abstract/AbstractFilter.vue';
import Search from './src/store/modules/search';

module.exports = {

For this to work I need to transpile this since a babel compiler normally won't transpile files imported from node_modules(Correct me if I am wrong here). Besides that I would probably be a good idea to do this so it can be used by different systems.

How do I transpile only the files that I need though with Webpack? Do I have to create a separate config for this?

What does a config like that look like? I know the vue-cli has a
command for one single file component but this is a bit different.

Any tips or suggestions on how to transpile something like this are welcome.


This seems like a good start as well:

The most import thing for Webpack users to notice is that you need to transpile your files in UMD which can be set by:

libraryTarget: 'umd'

This will make sure your are transpiling for
Universal Module Definition
, meaning your code will work in different environments like AMD,CommonJS, as a simple script tag, etc.

Besides that it is import to provide the externals property in webpack:

externals: {}

Here you can define which libraries your project users but should not be built into your
file. For example you don't want the Vue library to be compiled / transpiled into the source of your NPM package.

I will research a bit more, so far the best options looks like to create a custom project myself If I want flexibility and unit testing.

yev yev
Answer Source

The best way probably will be to build the module and set main in your package.json to my_dist/my_index.js. Otherwise every project that will use your module will have to add it to include which is tedious.

You will also want your webpack build to follow UMD (Universal Module Definition). For that you must set libraryTarget to umd:

output: {
    filename: 'index.js',
    libraryTarget: 'umd'

Also a good thing will be to add Vue to externals so that you didn't pack extra 200kb of vue library.

externals: {
    vue: 'vue'
resolve: {
  alias: {
    'vue$': 'vue/dist/vue.esm.js'

If you need an existing example of how to pack a vue.js component, you can take a look in one of the modules I maintain:

Particularly webpack.config.js and package.json will be interesting for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download