Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't import Vue component built by vue-cli-service

I'm quite new in Vue and maybe my issue is trivial.

What I have to do:

I want to create a Vue component that I can put in a NPM private repo and import it into other projects with a sth like bundle.js file

TLDR:

can't import vue component building by vue-cli-service build --target lib/wc. Importing component I have sth silimar to "export 'HelloWorld' was not found in '../node_modules/hello-world'

long version:

I have asked questions and projects as much as I could. All projects are built by vue-cli without any additional changes.

    1. create new default project vue create hello-world
    1. by default we havefirst component here - src/component/HelloWorld, and for this example this is component which we want to export
    1. using vue-cli-service try to make exportable file.
  • 3a. vue-cli-service build --target lib --name vue-test ./src/components/index.js where index.js is

    import Vue from 'vue';
    import HelloWorld from './HelloWorld.vue';
    
    const Components = {
        HelloWorld,
    };
    
    Object.keys(Components).forEach((name) => {
        Vue.component(name, Components[name]);
    });
    
    export default Components;
    
  • 3b. or directly vue file vue-cli-service build --target wc --name vue-test 'src/components/HelloWorld.vue'

in both scenarios vue-cli-service generates me file in to /dist folder and I want to believe this file are correct

in both scenarios I can't import this component to another vue project using import {HelloWorld} from 'path/to/folder/or/file'; or require('path/to/folder/or/file'). It seems like bundle files haven't exported member.

what I doing wrong? Should use build --target wc or build --target lib?


If You don't want to create new app to reproduce this issue U can download repo from https://github.com/okosowski/vueTest (project started using vue cli).

  1. git clone
  2. npm install
  3. npm run build-bundle-lib or npm run build-bundle-lib
  4. npm link or simply copy file to exiting vue project
  5. try to import/display HelloWorld

I will be grateful for any help!!! thanks


node v10.14.2

npm 6.4.1

vue-Cli 2.9.6 (the same on 3.3.0)

other used version in https://github.com/okosowski/vueTest/blob/master/package.json

like image 329
Oskar Kosowski Avatar asked Jan 08 '19 18:01

Oskar Kosowski


1 Answers

I was facing the same exact problem while using vue-cli to build and test my Vue component. Fortunately I was able to find the solution after reporting it as a bug to vue-cli issue tracker on GitHub. Turned out there was nothing wrong with the common.js file nor was it a bug.

Anyway... long story short, the existing project you are trying to import into is unable to resolve symlinks (because this is what happens when you use npm link). In order to solve your problem, you need to add the following into vue.config.js in the root folder of the project you are importing into:

// vue.config.js
module.exports = {
    chainWebpack: config => config.resolve.set('symlinks', false)
}

Hope this helps. For more info, check out these links:

My issue report in the vue-cli tracker
Webpack configuration: resolve.symlinks

like image 149
A. B. Al-Idrus Avatar answered Sep 20 '22 11:09

A. B. Al-Idrus