Utilizing NPM Workspace Project in conjunction with Vite to eliminate the necessity of the dist folder during the Vite build process

I am currently working on a project that involves a module using full path exports instead of index files.

This project is divided into 2 NPM workspaces: one called core and the other called examples.

The challenge I am facing is avoiding long import paths like

@my-project/core/dist/plugins/Input

Although I have managed to configure Typscript to compile without requiring dist in the path, Vite is unable to build it and throws an error stating it cannot find the necessary code:

Error: [vite]: Rollup failed to resolve import "@my-project/core/plugins/Input"

Typescript seems to be fine after some tweaking in the package.json file:

"typesVersions": {
    "*": {
      "*": [
        "./dist/*"
      ]
    }
  },

I suspect there might be something in my Vite configuration causing this issue. Here is how my config looks:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig(() => {
  return {
    build: {
      outDir: 'build',
    },
    plugins: [
      react({
        // Use React plugin in all *.jsx and *.tsx files
        include: '**/*.{jsx,tsx}',
      }),
    ],
    server: {
      port: 3000,
      host: true,
    },
    preview: {
      port: 4000,
    },
  };
});

I have tried various approaches to fix this issue and although I have made progress with TypeScript, I am still struggling to make Vite work properly. Any help or guidance would be greatly appreciated!

Answer №1

To simplify the importing process of a package, consider setting export paths in the package.json file. Here's an example:

{
  "name": "@my-project/core",
  ...
  "exports": {
    ".": "./dist/index.js",
    "./plugins/Input": "./dist/plugins/Input",
  }
}

After configuring this, you can import the subfolder like so:

import { stuff } from '@my-project/core/plugins/Input'

If the package is meant for public use, it would be beneficial to include ESM and CJS export paths to accommodate both sets of outputs:

        ".": {
            "import": "./dist/index.js",
            "require": "./dist/cjs/index.cjs"
        },

Feel free to reach out with any questions or updates on your progress!

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

Error: "Branch not located upstream; will not be pushing."

When I use the np command, I receive the message "Upstream branch not found; not pushing." and it doesn't open the GitHub repo to draft a release. Why is this happening? https://i.stack.imgur.com/8hyn3.gif Currently, I am running np v5.0.3 on bash v ...

Utilize code to scour the npm registry for a specific package

Searching for packages in the npm registry directly from my node app seems to be a bit tricky. I've tried looking for an official way to do it programmatically, but couldn't find any documented solutions. It's tempting to resort to simply us ...

What steps can I take to pinpoint the error I made while running npm run build?

I am brand new to VueJS and this is my very first attempt at creating a single page application with a server-side app. I encountered an issue right before deploying the app on shared hosting. When I tried running "npm run build", it returned an error that ...

Oops! The name provided in the Prisma of the Node.js project is not valid

While working on a node.js project, I incorporated Prisma and executed the following command: npx prisma migrate dev However, I encountered this error message: Environment variables loaded from .env Error: Invalid name: "project name" I am unsu ...

Issue with reading the current length of an array object in a while loop in Angular 6

After successfully splitting an array into parts, I decided to add some filters to only include the items in the list that have an action status of (4). However, I encountered a problem where the while loop couldn't read the length of the array. This ...

Error message: "The function RNFetchBlob.fetch is not a valid function in npm react-native-fetch-blob."

Currently, I am utilizing the npm package react-native-fetch-blob. I have meticulously followed all the steps provided in the git repository to implement this package. After following the instructions, I imported the package by using the following line ...

Helping React and MUI components become mobile responsive - Seeking guidance to make it happen

My React component uses Material-UI (MUI) and I'm working on making it mobile responsive. Here's how it looks currently: But this is the look I want to achieve: Below is the code snippet for the component: import React from 'react'; i ...

The state update is triggering a soft refresh of the page within Next.js

In my Next.js project, I have integrated a modal component using Radix UI that includes two-way bound inputs with state management. The issue arises when any state is updated, triggering a page-wide re-render and refreshing all states. Here is a snippet of ...

Unique Version: Some effective tips for utilizing a fork of type definition such as @types

Currently, I am utilizing Typescript 2.0 along with @types and the experience has been quite positive. Thanks to @types, we can easily leverage type definitions by simply installing the package via npm. Surprisingly, I have not delved into how it actually ...

The Unnecessary Error Messages of Capistrano 3

I am currently troubleshooting an issue with a command when executed through a capistrano task. Interestingly, the command runs smoothly when directly executed on the server: desc 'Run npm install on remote server' task :npm_install do on ...

Testing the functionality of functions through unit testing with method decorators using mocha and sinon

I have a class method that looks like this: export class MyClass { @myDecorator() public async createItem(itemId: string, itemOptions: ItemOption[]): Promise<boolean> { // ... // return await create I ...

When merging multiple prop definitions using Object.assign in defineProps, Vue props can be made optional

I have defined a const called MODAL_OPTION_PROP to set common props for a modal: export const MODAL_OPTION_PROP = { modalOption: { type: Object as PropType<ModalOptionParams>, default: DEFAULT_MODAL_OPTION, }, }; I am trying to use it in ...

Issue encountered during Heroku deployment: Failed to build React app. When attempting to push changes to Heroku, an unexpected end of input error was received instead of the expected result. This error occurred on an unidentified file at

Encountering a parsing error while attempting to deploy a React app on Heroku using git push heroku master. The app built successfully yesterday, but since then some media queries were added by another contributor to various .scss files. The primary error ...

Updating the value in React context does not result in the value being updated

I am in the process of developing a simple routing system where users can either be authenticated or not. I have been using hooks to implement this functionality, but so far, it has not been as successful as I hoped for. authProvider.tsx import React, {Di ...

The package.json file will always fetch the most up-to-date version of a package

On a particular project, my goal is to always retrieve the most recent published version of a package from a private npm registry (Azure DevOps > Artifacts). To verify that the latest version has indeed been released, I downloaded the artifacts in the for ...

A guide on integrating JSHINT programmatically in Node.js applications

After using the npm jshint package in my command line tool, I now wanted to utilize it programmatically. However, the documentation provided an example without specifying how to include or require the JSHINT function. var source = [ 'function goo() ...

Can you explain the distinction between `any[]` and `{ [s: string]: any }`?

I was attempting to make this code snippet function properly: test(a: any[]) { let b: string[][] = []; b.push(Object.keys(a[0])); b.push(...a.map(e => Object.values(e))); } However, the compiler is throwing an error for the b.push(...a.ma ...

Craft a unique typings file tailored to your needs

After recently creating my first published npm package named "Foo", I encountered some difficulties while trying to consume it in a TypeScript project. The tutorials on how to declare modules with custom typings were not clear enough for me. Here are the k ...

Updating the hostname in the systemFile does not result in the desired change being reflected in Next

Operating System : Windows 11 Next Version : 13.5.0 Issue: I am trying to set up my local development environment to run on instead of http://localhost:3000 I have made changes to the host file located in Windows\System32\drivers\etc- hos ...

What is the process for clearing cache in inversifyJS?

In my simple application, I am using express server along with TypeScript. Initially, I can successfully reach my endpoint, but after updating the model, the old data persists. I have attempted the suggested approach mentioned here: How to disable webpage ...