Vue encountered a double loading issue when utilizing a library compiled with Webpack

I am facing an issue with my TypeScript library of Vue components that gets compiled into a single JS file using Webpack. The problem arises when the TypeScript project consuming this library also depends on Vue. Upon running the application, I noticed that two instances of Vue are being loaded - one from the library bundle (/node_modules/my-lib/dist/index.js) and the other from the project's node_modules (/node_modules/vue/dist/vue.esm.js).

Here is the webpack.config.js for the library:

const path = require('path');
module.exports = {
    entry: './src/index.ts',
    output: {
        filename: 'index.js',
        publicPath: '/dist/',
        path: path.resolve(__dirname, 'dist'),
        library: "fsaa-util",
        libraryTarget: 'umd',
        umdNamedDefine: true
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
            },
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
                options: {
                    allowTsInNodeModules: true,
                    appendTsSuffixTo: [/\.vue$/]
                }
            }
        ]
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.vue', '.wasm', '.mjs', '.js', '.json', 'css'],
        alias: {
            'vue$': 'vue/dist/vue.esm.js',
        },
        modules: ["src", "node_modules"]
    },
};

And here is the webpack.config.js for the consumer project:

const path = require('path');
module.exports = {
    entry: './src/index.ts',
    output: {
        filename: 'index.js',
        publicPath: '/dist/',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
            },
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
                options: {
                    allowTsInNodeModules: true,
                    appendTsSuffixTo: [/\.vue$/],
                    compiler: 'ttypescript'
                }
            }
        ]
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.vue', '.wasm', '.mjs', '.js', '.json', 'css'],
        alias: {
            'vue$': 'vue/dist/vue.esm.js',
        },
        modules: ["src", "node_modules"]
    }
};

The way Vue is imported in both the library and consumer project is as follows:

import Vue from "vue";

I have also attempted to use the following configuration in the webpack.config.js of the library:

externals: {
    vue: 'vue',
},

However, this results in both vue.runtime.esm.js and vue.esm.js being loaded instead.

Answer №1

While facing a similar problem, I discovered that I was importing Vue into my components in two different ways:

import Vue from "vue";

and

import Vue from "Vue";

The error occurred due to the PascalCase used in "Vue", whereas all other modules were imported as "vue". Making this adjustment resolved the issue for me.

Answer №2

Unlike many others, I've chosen not to incorporate "Vue" in my imports.

I've been grappling with this dilemma for a couple of years now across various projects, hoping for assistance from either the Vue or Webpack communities. Unfortunately, it seems that there are numerous potential causes for this issue, none of which appear to be connected to my specific problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Component in Vue router not updating when URL changes

I am currently utilizing vue router within my vite project to dynamically render the appropriate page or component based on the URL. After setting up a router-link to alter the URL, I specify which page should be displayed on that particular URL. However, ...

Issue with calling function from props in React is not being resolved

There seems to be an issue with the function not being called when passed into a functional component. While the onSubmit function is triggered, the login(email, password) function inside the Login component is never executed. Despite placing console.log s ...

The input text in the Typeahead field does not reset even after calling this.setState

As I work on creating a watchlist with typeahead functionality to suggest options as the user types, I encountered an issue where the text box is not resetting after submission. I attempted the solution mentioned in this resource by calling this.setState( ...

A step-by-step guide on setting up and accessing multiple environment files in Vue/Nuxt JS based on the domain

We have developed a Nuxt.js multi-tenancy website where we need to fetch environment variables based on the domain. We have 5 domains listed below: Note: The source code is the same for all domains. test1.com test2.com test3.com test4.com test5.com When ...

Retrieving the input value using ref in Vue 3 and TypeScript

It appears to be a straightforward issue, but I haven't been able to find consistent Vue 3 TypeScript documentation on accessing an input field and retrieving its value from within a function. <template> <Field type="text" ...

Exploring the connection between the App.vue file and other components within a Vue.js webpack setup

I set up vuejs using webpack as my bundler. vue init webpack frontend In the main.js file, I implemented vue-router with the following routes: routes: [ { path: '/', name: 'Home', component: HomeView }, This is ...

Creating a sophisticated union type by deriving it from another intricate union type

I have a unique data structure that includes different types and subtypes: type TypeOne = { type: 'type-one', uniqueKey1111: 1, }; type TypeTwo = { type: 'type-two', uniqueKey2222: 2, } type FirstType = { type: 'first&apo ...

Starting up the express server with a React client

Up until this point, my projects have relied on the create-react-app tool, with the express-server and react client kept in separate directories. However, I am now exploring alternatives to create-react-app to gain a deeper understanding of how things wor ...

Is there a way to retrieve the modal's viewport height in Angular?

Is it possible to determine the viewport height of my ng bootstrap modal within my Angular application? Here is what I currently have: I have a modal with CSS styling as shown below: .modal-xxl { width: 95% !important; max-height: 90% !important; ...

Internet Explorer terminates AJAX requests

When I send a request to the server using Ajax and JQuery, it returns more than 2000 records (approximately 16,000 records). In Google Chrome, the call is executed although it takes about 40 seconds. However, in Internet Explorer 11, the execution gets ca ...

Unexpected trigger of Vue function with parameters from child component

I am encountering an issue with the following component: <template> <button class="button" @[click]="action">{{ text }}</button> </template> <script> export default { name: "Button", props: ...

An array devoid of elements may still hold significance

I have a specific function structure as follows: public returnData(): { points: Array<{ x: number, y: number }>, pointsCount: Array<number> } { return { points: [{x: 0, y: 1},{x: 1, y: 2 }], pointsCount: [1, 2, 3, 4] } ...

Interface of TypeScript Undetermined

Currently, I am developing a Demo API Wrapper specifically for Roblox. During the development process, I have come across a certain issue that I would like to address. My aim is to send a request and then return all the data in the manner of an API wrapper ...

Using TypeORM with a timestamp type column set to default null can lead to an endless loop of migrations being

In my NestJs project using TypeORM, I have the following column definition in an entity: @CreateDateColumn({ nullable: true, type: 'timestamp', default: () => 'NULL', }) public succeededAt?: Date; A migration is gene ...

Encountering an Issue with Dynamic Imports in Cypress Tests Using Typescript: Error Loading Chunk 1

I've been experimenting with dynamic imports in my Cypress tests, for example using inputModule = await import('../../__tests__/testCases/baseInput'); However, I encountered an issue with the following error message: ChunkLoadError: Loading ...

The concept of passing arguments in Typescript

During my experience with Typescript programming, I encountered a situation like the one described below. If I pass an argument containing an object with the same name as the parameter defined in the function signature, Typescript recognizes it, but not ...

Various types of generics within an object

Is there a way to achieve different types for the nested K type within a type like MyType? Here's an example: type Config<K> = { value: K; onUpdate: (value: K) => void; } type MyType<F extends string> = { [K in F]: <V>() =& ...

Troubleshooting Vue computed property access issues

Currently, I am in the early stages of learning Vue and have moved on to exploring validation. While looking at some older examples that utilize Vee-Validate, it seems that there has been recent changes to the library. How can I update this code to work w ...

How can I convert a MongoDB document into a DTO in NestJS?

I'm currently working with a data layer that interacts with a MongoDB database. My goal is to only handle MongoDB documents in this layer without exposing the implementation details to my services. My current approach involves the following code: // ...

Prepare the column data for Vue Good-Table by formatting it beforehand

I've created a custom Vue.js component that retrieves orders from a Woocommerce store. These orders include product variations and are received in object form. Before displaying the data in a table, I need to format the object accordingly. This is a ...