The module cannot be located due to an error: Package path ./dist/style.css is not being exported from the package

I am facing an issue with importing CSS from a public library built with Vite. When I try to import the CSS using:

import 'rd-component/dist/style.css';

I encounter an error during the project build process:

ERROR in ./src/page/photo/gen/GenPhoto.tsx 18:0-37
Module not found: Error: Package path ./dist/style.css is not exported from package /Users/john/source/reddwarf/frontend/snap-web/node_modules/rd-component (see exports field in /Users/john/source/reddwarf/frontend/snap-web/node_modules/rd-component/package.json)

webpack compiled with 1 error and 1 warning
No issues found.

I have tried to address this by modifying the plugins configuration as follows:

plugins: [
    {
      name: 'extract-css', 
      generateBundle(_, bundle) {
        for (const fileName in bundle) {
          const chunk = bundle[fileName];

          if (fileName.endsWith('.js') && chunk.code.includes('import "./style.css";')) {
            const cssFileName = fileName.replace('.js', '.css');
            const css = chunk.code.match(/(?<=import ")[^"]+(?=";)/s)[0];

            fs.writeFileSync(`dist/${cssFileName}`, css, 'utf-8');

            chunk.code = chunk.code.replace(css, `./${cssFileName}`);
          }
        }
      },
    },
  ],

However, I receive an error stating that the chunk does not contain a code attribute. How can I successfully export and import the CSS from the public library into my current project? Here is the node_modules structure of the public library:

https://i.stack.imgur.com/YjKKJ.png

Answer №1

After some trial and error, I discovered the necessary steps to export the CSS properly in my package.json:

"exports": {
        ".": {
            "import": "./dist/rd-component.es.js",
            "require": "./dist/rd-component.umd.js"
        },
        "./dist/style.css": {
            "import": "./dist/style.css",
            "require": "./dist/style.css"
        }
    },

For more detailed information, check out this resource: https://github.com/vitejs/vite/discussions/2657

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

Customizing textfield error color in MUI5 React based on conditions

I am looking for a way to dynamically change the color of error messages in my application, with warnings displaying in orange and errors in red. I prefer not to use useStyle as it is now deprecated in mui5. Below is the code snippet I have created: import ...

Visual Studio is refusing to highlight my code properly, intellisense is failing to provide suggestions, and essential functions like go to definition are not functioning as expected

Due to a non-disclosure agreement, I am unable to share any code. However, I am experiencing an issue with Visual Studio not highlighting my code or allowing me to utilize its built-in tools. While I can rebuild the project, I cannot edit or access any fil ...

Codesandbox is experiencing a technical issue where the React library is not being identified. However,

Just wanted to showcase some example code on Codesandbox, but this particular code in MyTable.tsx import { FC, useState } from "react"; interface Row { id: number; content: string; } interface Props { initialRows: Row[]; } export const M ...

RxJS emits an array of strings with a one second interval between each emission

Currently, my code is set up to transform an Observable<string[]> into an Observable<string>, emitting the values one second apart from each other. It's like a message ticker on a website. Here's how it works at the moment: const ...

Utilizing the Parent's Props in ReactJs: A Comprehensive Guide

I am currently utilizing reactjs version 16. My objective is to create a wizard that can be reused anywhere on my website, providing flexibility and convenience. Take a look at the code I have implemented: export default class Website extends Component ...

The issue with hiding elements using .setAttribute in HTML/CSS is not occurring as expected

function showEditStudentProfile() { document.getElementById('editstudentprofile').setAttribute('class', 'unhide'); document.getElementById('profile_viewStudent').setAttribute('class', ' ...

React components multiplying with every click, tripling or even quadrupling in number

My app enables users to create channels/chatrooms for communication. I have implemented a feature where pressing a button triggers the creation of a channel, using the function: onCreateChannel. Upon calling this function, the state of createChannel chan ...

What steps should I take to solve the issue of a black screen showing up once my React website has finished loading

Here's a photo showing error messages displayed on my website. Strangely, there are no errors appearing in my terminal and the website loads perfectly fine. However, I encountered some issues when trying to make styling changes using my dev tools. Aft ...

Typescript encounters an overload error on the Accumulator argument while using reduce operation

I encountered the following code snippet: const foo = ( fields: { [key: string]: string, } ) => { const { one, two } = Object.values(fields).reduce( (acc, field) => { if (isOne(field)) { return { ...acc, two: [...acc.two, ...

Difficulty with two-dimensional arrays in Angular and Typescript

I am currently stuck trying to assign values to a 2-dimensional object array in Angular/Typescript. I have noticed that the last assignment seems to override the previous ones, but I cannot pinpoint why this is happening. Could someone please review my cod ...

Show various Bootstrap Alert Messages based on the type of error detected?

Trying to figure out how best to explain this, here it goes! I am working on a website and I want to incorporate alert messages. Check out some examples here. Depending on the form inputs, I need different alerts to be displayed. PHP will validate the in ...

Are you encountering an issue with your React application while attempting to retrieve JSON data?

I recently developed a Home.js component and integrated it within the App.js file of my React application. However, I am encountering an error when trying to fetch JSON data. The error message is displayed at this link: https://i.stack.imgur.com/hGkN3.png ...

Converting a React element using JSON.stringify results in transforming it into a JavaScript object

I have an element in React called testReactElement that I want to display on the screen. I also want it to persist even after the user closes the tab and opens it again, so I decided to store it in localStorage. To add a React element to localStorage, I fi ...

Altering the parent component's output depending on a boolean value established in the child component within Angular

Recently I began learning Angular and find myself in need of some assistance when it comes to managing a specific situation with Angular 16. In our project, we have two different versions of the site header represented by two components - one as the defaul ...

Is there a way to leverage JavaScript to click on one div and modify the settings of another div simultaneously?

I am struggling with my code which has unnecessary information. <div> <div id="one" class="button"></div> <div id="two" class="button"></div> </div> <div> <div class="Home tab"> < ...

Manipulate CSS Properties with Javascript Based on Dropdown Selection

I am currently working on implementing a feature that involves changing the CSS property visibility: of an <input> element using a JavaScript function triggered by user selection in a <select> dropdown. Here's what I have so far in my cod ...

Guide on transforming observable<User> to Observable<boolean> in Angular 4

As a newcomer in Angular and Mean Stack, I need help implementing the canActivate() method to restrict admin routes. In my service file, the checkAdmin method returns an observable of type "User", but the canActivate method's return type is an observa ...

Webpack is refusing to compile

Having an issue with Webpack in my React-Django project. The compiled file (main.js) isn't generating as expected, and I'm struggling to identify the problem. Here's a snapshot of the file tree: |-- __init__.py |-- admin.py |-- apps.py |-- m ...

Animating the removal of a selected value with a React slide animation when unmounting on exit

I am encountering an issue where, upon clicking a radio button, the next question appears with a slide animation. However, when unmounting or exiting, the previously selected radio button value also gets deselected. How can I prevent this from happening? c ...

What is the largest image dimension allowed for website use?

What are the dimensions in pixels and file size in MB? I've been curious about this for a while, appreciate any insights! ...