Importing a module directly from the umd distribution may yield a distinct outcome compared to importing it

Within my Vite/Vue3 application, I am importing two identical umd.js files:
One is located at

node_modules/foo/bar/dist/foobar.umd.js
(imported with alias @foo = node_modules/@foo).
The second .umd.js file can be found at
<root dir>/foo/bar/dist/foobar.umd.js
.

Both paths are successfully resolved, however, the module imported from outside of node_modules does not import correctly as an ES module:

import * as foobar1 from '@foo/bar/dist/foobar.umd' // comes from node_modules/foo/bar/dist/foobar.umd.js, works
import * as foobar2 from 'foo/bar/dist/foobar.umd' // NOT recognized as ES module

console.log('foobar', foobar1, foobar2)

foobar1 returns named exports (app, pinia, router), __esModule: true, and

Symbol(Symbol.toStringTag): "Module"

foobar2 only returns
Symbol(Symbol.toStringTag): "Module"

What is causing this difference?

If relevant, here is how the alias (for the correctly imported module) is defined in my vite.config.js:

resolve: {
      alias: {
        '@foo': fileURLToPath(
          new URL('./node_modules/@foo', import.meta.url)
        ),
      }
    }

Edit. It seems that Vite's dependency optimization may be impacting this issue. Adding the following to vite.config.js:

optimizeDeps: {
      exclude: ['@foo'],
    }

Results in foobar1 also not returning named exports, but unfortunately, this doesn't provide much clarity.

Answer №1

In the updated question, it appears that utilizing Vite's optimizeDeps feature is crucial.
Once included in the vite.config.js file:

optimizeDeps: {
      include: ['foo/bar'],
    },

this enables correct importing of named exports:

import * as foobar2 from 'foo/bar' // successful import of named exports
import * as foobar2 from '/foo/bar' // unsuccessful import (despite correct path)

It is vital to use the exact path specified in optimizeDeps.include.

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

The Bootstrap carousel controls now add the carousel ID to the address bar instead of just moving to the next slide

I can't figure out why my carousel isn't working. The id of the carousel is showing up in the webpage's address, which usually happens when the 'bootstrap.js' file is missing, but I have included it. Can anyone help me troubleshoo ...

Typescript Angular2 filtering tutorial

In Angular 2 using TypeScript, the goal is to search for matching values from an array within an object array. The intention is to filter out any objects where the 'extraService' property contains any of the values from the 'array_values&apo ...

Encountered an error while running npm run dev on a Laravel Vue 3 project: opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],

I am facing an issue in my Laravel 9 Vue 3 project. When I run php artisan serve and then npm run dev, I encounter the following error: opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ], library: 'di ...

Adjust the hue as you scroll

I've been searching for a solution to this issue, but I haven't been able to make it work. My goal is to have the page header change from a transparent background to a red background as the user starts scrolling down the page. $(window).on("s ...

The @output decorator in Angular5 enables communication between child and

Hello fellow learners, I am currently diving into the world of Angular and recently stumbled upon the @output decorators in angular. Despite my best efforts to research the topic, I find myself struggling to fully grasp this concept. Here's a snippet ...

Leveraging React Hooks for managing the Data Provider and Data Context functionality

Currently, I am revamping my DataProvider by upgrading it from a class component to a functional component utilizing React Hooks. I suspect that the issue lies in how I am setting up my context consumer, but I am struggling to find an effective way to tes ...

PDFMAKE: A Guide to Duplicating Elements in the 'Content' Array

I have an Array within Items. My goal is to display them in a Table format using PDFMake. table: { multiple pages headerRows: 2, widths: ['auto', 100, 200, 'auto', 'auto', 'auto'], body: [ ...

The function 'transformArticles' is not recognized as a property of the object 'Article'

I'm encountering an issue with Typescript that I need help understanding. In my code, I have a route where I am importing a class called Article like this: import { Request, Response } from "express"; const appRoot = require("app-root-path"); import ...

I am experiencing an issue where components are constantly re-rendering whenever I type something. However, I would like them to

Currently, I am in the process of developing a REACT application that takes two names, calculates a percentage and then generates a poem based on that percentage. The issue I am facing is that whenever I start typing in the input fields, the LovePercentCon ...

What's the best way to unpack the gzip data that Ajax sends to JavaScript?

Hello there! I've encountered an issue: PHP is sending compressed data using gzdeflate(): $string=gzdeflate($string,9); echo $string; In the browser, pako.js has been included and the following code is executed: var rsp=rst.responseText; rsp=pako.in ...

What is the most efficient method for clearing the innerHTML when dealing with dynamic content?

Is there a more efficient method for cleaning the innerHTML of an element that is constantly changing? I created a function to clean the containers, but I'm not sure if it's the most efficient approach. While it works with the specified containe ...

Numerous Fascinating Challenges with React Native

Looking to share my React Native project and seeking help with some issues I'm facing. Despite multiple attempts, I have been unsuccessful in resolving them. Can anyone provide assistance? I have thoroughly searched Stack Overflow for similar questio ...

Adding array elements to a JavaScript object

I find myself in a rather unique predicament that I'm struggling to navigate. I have come across some data structured as follows. Please bear with me if I use any incorrect terminology, as I am relatively new to this. usersByName: { "tester&q ...

No data provided in nested parameters for Ajax post request

I am attempting to send an Ajax request to my rails controller using Ajax. $.ajax({ url: '/vulnerabilities/export', type: 'GET', processData: false, data: {export: {ids: this.refs.table.state.selectedRowKeys }} }); When there ...

Running JavaScript code within views

While dealing with AngularJS, I have an index page that includes a <div ui-view></div>. When a specific URL like #/offers/new is entered, it loads a page such as new-offer.html into the ui-view. In my javascript code block within the index.htm ...

React hooks eliminating unnecessary rendering

Having recently delved into React and hooks, I'm facing an issue with refreshing the list of files in my app after clicking on the convert button. The correct file only shows up if I manually refresh the page. The React part of the code involves uplo ...

Potential Unresolved Promise Rejection (ID: 0): The object 'prevComponentInstance._currentElement' is undefined

Attempting to fetch JSON data in react native using axios.get(my_url_path), then updating the state with response.data under the key 'urldatabase'. When attempting to access this state key and read the data from the JSON, an error is encountered: ...

Navigating the FormSpree redirect: Tips and tricks

I recently set up my website on Github Pages and wanted to integrate a free contact form from FormSpree. However, I encountered an issue where after submitting the form, it redirected to a different website, which was not ideal. After researching online, I ...

I am planning to divide my web application into two sections by utilizing react router. I intend to incorporate a router within one of the routes mentioned earlier

/src |-- /components | |-- /signin | |-- SignIn.js | |-- /home | |-- Home.js | | |-- /dashboard | |-- Dashboard.js | |-- /assignee |-- /App.js |-- /index.js Dividing the project into two main parts: signi ...

Making AJAX requests with Laravel using the XMLHttpRequest object is a

When using Laravel to upload a file and create a progress bar with ajax requests, the form action routes to the controller in this way: <form action="{{ URL::route('upload-file-form-post') }}" method="POST" enctype="multipart/form-data"> ...