Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue initialized twice #342

Closed
trapcodeio opened this issue Apr 10, 2019 · 46 comments
Closed

Vue initialized twice #342

trapcodeio opened this issue Apr 10, 2019 · 46 comments

Comments

@trapcodeio
Copy link

trapcodeio commented Apr 10, 2019

Issuehunt badges

Describe the bug

After including slider for use

 import  VueSlider from 'vue-slider-component'; 
 Vue.component('VueSlider', VueSlider);

I see the vue development message twice.

You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html

The first from my main needed.js where i initialized my vue.
The second is from webapp.js where i included the slider.

When i click on any element after adding this slider i get this error.

[Vue warn]: $attrs is readonly.

From this post on stackoverflow:
The $attrs, $listeners and other warnings are displayed if there's more than one instance of Vue loaded

Is there anything i can do to resolve this?

Vue: 2.6.10
Vue-router: 3.0.3
Vue-slider-component: 3.0.36

There is a $50.00 open bounty on this issue. Add more on Issuehunt.

@trapcodeio trapcodeio added the bug label Apr 10, 2019
@NightCatSama
Copy link
Owner

Are you using the 3.0.26 version?

The 3.0.26 version should not contain the vue dependency.

@nilsE
Copy link

nilsE commented Apr 10, 2019

same here, also with 3.0.26

@NightCatSama
Copy link
Owner

Is it a problem with the component? Does this error occur without using the component?

@nilsE
Copy link

nilsE commented Apr 10, 2019

i'm using https://github.com/RobertBoes/nova-slider-field for laravel/nova, it uses your component.
branch 2.x works fine, but with 3.x the slider is not showing up at all and vue is loaded twice
without the component everything's fine.

screenshot_709

@NightCatSama
Copy link
Owner

This library uses components of the v2.x version.

https://github.com/RobertBoes/nova-slider-field/blob/29a959b56f75c376ca55c97cef77818a9bb4548e/package.json#L19

I don't know if it matters, I have not used Laravel Nova 😞 .

@nilsE
Copy link

nilsE commented Apr 10, 2019

sure, updated that to 3.0.26

@NightCatSama
Copy link
Owner

Sorry, I have no other ideas 😭 .

@trapcodeio
Copy link
Author

@NightCatSama yes am using version 3.0.26

It works fine in production but throws error in development

I still can't find a solution too

@trapcodeio
Copy link
Author

Any help with this yet?

@NightCatSama
Copy link
Owner

@trapcodeio I can't reproduce this question, can you provide more information, such as the build environment (webpack.config.js or vue.config.js) and how you introduced vue and components.

@trapcodeio
Copy link
Author

@trapcodeio I can't reproduce this question, can you provide more information, such as the build environment (webpack.config.js or vue.config.js) and how you introduced vue and components.

I explained how i introduced the component in the main post.
The error shows on development but not in production.

see the error in the image below.
needed.js is where i initialized my vue.
webapp.js is where vue-silder-component is included.

this happens once i require or import, even before setting vue to use it.
The second development message in webapp.js shows once i import or require vue-silder-component

i use laravel-mix with laravel so i don't have any of the files you mentioned above.

Screenshot from 2019-04-12 19-14-57

@NightCatSama
Copy link
Owner

@trapcodeio Wouldn't an error occur if v-model is not used on the component?

@trapcodeio
Copy link
Author

@trapcodeio Wouldn't an error occur if v-model is not used on the component?

The error occurs on import or require, even without adding vue-slider in template.

For now i only add the slider when i want to build for production then comment them out in development.

@NightCatSama
Copy link
Owner

@trapcodeio Try executing npm ls vue in your project to see if there are two vue.

If so, try deleting and reinstall dependencies.

@shuhankuang
Copy link

I got the same error.
vue@2.6.10
vue-slider-component@3.0.27
buefy@0.7.4

QQ20190416-204318@2x

@NightCatSama
Copy link
Owner

I used the project created by the Laravel document and did not report an error 😢 .

related code

Create-Project

composer create-project --prefer-dist laravel/laravel blog

./resources/js/components/ExampleComponent.vue

<template>
    <div class="container">
        <vue-slider :width="400" />
    </div>
</template>

<script>
    import VueSlider from 'vue-slider-component'
    export default {
        components: {
            VueSlider,
        },
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

<style>
    .container {
        padding: 30px;
    }
</style>

Before this problem can be solved, can try using v2.x.

I hope someone can solve this problem or provide a reproducible program.

@Stereotip42
Copy link

Stereotip42 commented Apr 23, 2019

And I got the same error after update :(

image
image

@NightCatSama
Copy link
Owner

@Stereotip42 What is your development environment?

@Stereotip42
Copy link

Stereotip42 commented Apr 23, 2019

@NightCatSama its development environment (Project also created by Laravel)

@NightCatSama
Copy link
Owner

@Stereotip42 What is the Laravel version?

@Stereotip42
Copy link

@NightCatSama 5.8 version, that's just recently updated from 5.7

@NightCatSama
Copy link
Owner

@Stereotip42 Try using vue-slider-component/dist/vue-slider-component.common.js

@IssueHuntBot
Copy link

@IssueHunt has funded $50.00 to this issue.


@getninjaN
Copy link

First of all, thank you for an awesome component!
I love it.

Secondly,
I have the same problem in development no matter if I use vue-slider-component.common or vue-slider-component.umd.

vue version is 2.6.10
vue-slider-component version is 3.0.36

image

From what I can find vue.runtime.esm.js is loaded from this line in vue-slider-component.umd.min.js.

module.exports = e(__webpack_require__(/*! vue */ "./node_modules/vue/dist/vue.runtime.esm.js"))

I have tried a bunch of stuff but without success.

There are no errors in production however and the component still functions as it should in development, so it's not broken.
It's just annoying with the console getting wrecked by errors. :)

Not sure if this helps at all, I'm just tired from debugging and thought I'd share.

@NightCatSama
Copy link
Owner

@getninjaN Thank you for sharing.

This error should be caused by two copies of vue in app, but I don't know why this happened.

Can you provide me with a project that can restore the error, just need empty project.

@thiagorb
Copy link

I was having this same issue but it was due to how my bundle was being setup. In my case I need to have templates declared in .js files, so they must be compiled, and therefore the compiler must be included in the bundle.

In order to avoid importing vue/dist/vue in my components, I'm using aliasify to alias vue to either vue/dist/vue or vue/dist/vue.min (depending on whether I'm building for dev or production).

This was leading to vue being included twice in the bundle, once in node_modules/vue/dist/vue.js and once in node_modules/vue/dist/vue.runtime.common.js.

I was able to fix it by adding the flag global: true to the aliasify transform, so that the alias would also apply to vue-slider-component.

@mareczek
Copy link

@NightCatSama isn't something like this:

  configureWebpack: {
    externals: {
      vue: 'vue'
    }
  },

missing from vue.config.js?

@NightCatSama
Copy link
Owner

@mareczek It seems that most of the errors are in the laravel environment, so there is no such vue.config.js.

@cvharris
Copy link

cvharris commented Jun 10, 2019

@mareczek was correct. Once I indicated to Webpack that Vue was loaded somewhere else (in my situation in AEM's clientlibs) then it stops bundling Vue multiple times, which can happen for multi-instance pages or multiple bundled outputs in Webpack. vue-slider-component immediately started working for me

All you have to do in webpack.config.js is:

{
  ...
  externals: {
    vue: 'Vue'
  },
  ...
}

@arneysebaert
Copy link

Same problem here:

vue-slider-component: 3.0.33 (tried 3.0.26 and 2.x as well)
vue: 2.5.22

I also have 2 instances of Vue being loaded.

@davidlbean
Copy link

Specific to a Rails app:

Hmmm...I think I just got rid of my duplicate loading error... Added this to my config/webpack/environment.js file:

// see https://stackoverflow.com/questions/53582944/how-to-properly-install-vuetify-for-rails
const resolver = {
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
}
environment.config.merge(resolver)

Need to do some more testing to verify, but I thought I'd add this now for anyone using Rails and having the problem.

@getninjaN
Copy link

Thank you @davidlbean!
This solved the issue for me.

@michagee
Copy link

@davidlbean Not in Rails context, but same worked for me, thank you!

Repository owner deleted a comment from IssueHuntBot Aug 20, 2019
@Jakob-Maudience
Copy link

My project is an electron environment, and I'm using electron-webpack for bundling.

The above "resolve" webpack config didn't work, but I was able to resolve by whitelisting both the vue and vue-slider-component modules in my elecronWebpack settings in package.json

Since webpack is set to target the electron environment, all modules are treated as externals. Unfortunately, there can be a few situations where this behavior may not be expected by some modules. For the case of some Vue UI libraries that provide raw *.vue components, they will needed to be white-listed. This ensures that vue-loader is able to compile them as the UI library originally expected.

package.json

{
  ...
  "electronWebpack": {
    "whiteListedModules": [
      "vue",
      "vue-slider-component"
    ]
  }
}

Vue version: 2.6.10
vue-slider-component: 3.0.38
electron: 5.0.2
electron-webpack: 2.6.2

Hope this helps someone!

@CoffeeKing68
Copy link

Hi
I'm very new to webpack and compilation. If you wouldn't mind translating the solution for a laravel nova project (using laravel mix to compile).

Thanks

@fedeth
Copy link

fedeth commented Mar 15, 2020

Specific to a Rails app:

Hmmm...I think I just got rid of my duplicate loading error... Added this to my config/webpack/environment.js file:

// see https://stackoverflow.com/questions/53582944/how-to-properly-install-vuetify-for-rails
const resolver = {
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
}
environment.config.merge(resolver)

Need to do some more testing to verify, but I thought I'd add this now for anyone using Rails and having the problem.

this works for me, but i dunno why

@olemarius
Copy link

I got the error in a plain vue app (no vuetify, laravel or anything) when vue.js was added to <head>. Once I removed that and added import Vue from 'vue' in main.js it worked. Vue version 2.6.11 and vue-slider-component 3.1.5.

I'm thinking if you add import vue in the component, my app is resolving that in the build process so it ends up importing only once, while in my case I added vue outside the app file main.js so it couldn't possibly know there's another vue. Makes sense?

So if you set up a simple test like this and add vue-slider-component in app.js you should be able to reproduce the issue.

    <head>
        <title>Demo</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div class="container">
            <div id="app"></div>
        </div>
            <script src="app.js"></script>
    </body>
</html>```

@diegocharles
Copy link

Specific to a Rails app:

Hmmm...I think I just got rid of my duplicate loading error... Added this to my config/webpack/environment.js file:

// see https://stackoverflow.com/questions/53582944/how-to-properly-install-vuetify-for-rails
const resolver = {
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
}
environment.config.merge(resolver)

Need to do some more testing to verify, but I thought I'd add this now for anyone using Rails and having the problem.

I own you a coffee buddy.
I was having the same error here with my Rails app.

@borismacias
Copy link

Specific to a Rails app:

Hmmm...I think I just got rid of my duplicate loading error... Added this to my config/webpack/environment.js file:

// see https://stackoverflow.com/questions/53582944/how-to-properly-install-vuetify-for-rails
const resolver = {
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
}
environment.config.merge(resolver)

Need to do some more testing to verify, but I thought I'd add this now for anyone using Rails and having the problem.

Its still working with version 3.2.3, send me a link to buy you a beer 🍺

@james090500
Copy link

james090500 commented Aug 16, 2020

Using laravel mix, when using the command

npm install vue-slider-component --save 

it installs the component as a devDependencies and installs it to the local user node_modules (/home/james/node_modules) rather than the project directory. If I just use

npm install vue-slider-component

It installs it as a dependencies and into the project node_modules. (/home/james/project/node_nodues)

If the project is installed a dependencies rather than a devDependencies it seems to run fine. If it's installed as a devDependencies you get compilation errors. and "VueJS" not found

@feldsam
Copy link

feldsam commented Oct 22, 2020

Hello, I am using Laravel Nova and developing my own Nova Tool. I think that this problem is caused by this component's explicit dependency on the Vue (there is require('vue') in dist JS) in conjunction with how Nova manages third-party code.

Laravel Nova is shipped as the package with Vuejs installed, Nova extensions (tools, fields) have structure as composer package with own package.json and npm too. Problem is, that if I want to use this slider component, I have to install Vuejs in my Tool even it is installed in Laravel Nova. I can't compile Tool's resources without it.

The solution should be removing the explicit dependency on Vue from this package, so we can compile assets without installing second Vuejs in the Nova Tool/Field.

@trapcodeio
Copy link
Author

I closed this because there seem to be a fixed above

@feldsam
Copy link

feldsam commented Oct 24, 2020

Hello, it still not fixed. It is possible to remove dep on vue?

@NightCatSama
Copy link
Owner

@feldsam It has been tested that the slider does not work in versions prior to vue2.6.0 after removing require('vue').

So I updated it by releasing a beta version:

  yarn add vue-slider-component@beta
  # npm install vue-slider-component@beta --save

@feldsam
Copy link

feldsam commented Oct 24, 2020

Hello, perfect! Working now without ugly errors. Thank you!

@therealtimhawkins
Copy link

I had this problem when upgrading from vue-cli to vite and solved it by adding the alias in vite.config.json:

resolve: {
    alias: {
      vue: 'vue/dist/vue.esm.js'
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests