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

Vite: The package may have incorrect main/module/exports specified in its package.json #19149

Closed
1 of 4 tasks
nemonemi opened this issue Sep 13, 2023 · 12 comments
Closed
1 of 4 tasks
Assignees
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug

Comments

@nemonemi
Copy link

Current Behavior

I have a library, which can no longer be generated with Webpack, that consumes a custom npm package, and when building it, it fails with the following message

>  NX   Failed to resolve entry for package "@our-custom/ui-components". The package may have incorrect main/module/exports specified in its package.json.

I have also reported it to Vite, but haven't had a chance, as of yet, to create a reproducible example.

At the moment, the library's package.json defines both main and module properties

    "main": "dist/index.js",
    "module": "dist/index.modern.js",

where the first one's content looks as if it was transpiled for ie5

'use strict';

var React$1 = require('react');
var reactUse = require('react-use');
...

and the second one for the modern browsers

import * as React$1 from 'react';
import React__default, { useContext, useMemo, useState, useCallback, useRef, useLayoutEffect, useEffect, forwardRef, createRef, createContext } from 'react';
import { useGetSet } from 'react-use';
...

Expected Behavior

It should work.

GitHub Repo

No response

Steps to Reproduce

Nx Report

Node   : 18.15.0
   OS     : linux-x64
   yarn   : 3.6.1
   
   nx                 : 16.5.3
   @nx/js             : 16.5.3
   @nx/jest           : 16.5.3
   @nx/linter         : 16.5.3
   @nx/workspace      : 16.5.3
   @nx/cypress        : 16.5.3
   @nx/eslint-plugin  : 16.5.3
   @nx/react          : 16.5.3
   @nx/webpack        : 16.5.3
   typescript         : 5.1.6

Failure Logs

No response

Package Manager Version

yarn

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@AgentEnder AgentEnder added the scope: bundlers Issues related to webpack, rollup label Sep 13, 2023
@nemonemi
Copy link
Author

Removing the "module" property resolves the issue, but I'm not sure if that's the desired approach, since one might not have the control over the library they consume.

@steven10172
Copy link

Related to #18492 (comment)

@teofilomonteiro
Copy link

@barbados-clemens left a comment that makes me very unhappy. This sounds like a breaking change release and not a feature release. It breaks the behaviour that it was before. Especially when migrating should keep everything working. Is there a workaround that doesn't involve changing the behaviour of the npm base repo?

@teofilomonteiro
Copy link

teofilomonteiro commented Oct 3, 2023

I will add more, the solution suggested just causes another issue. If you make changes to the lib, this will not trigger a reload of the app or a rebuild of the lib/app.

Just makes me wonder why do you support even the npm workspaces.

@joeldbirch
Copy link

I'm getting the same issue using yarn workspaces.

@teofilomonteiro
Copy link

I think another solution for this is to prevent the migration to use the `import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; Something is missing on this plugin that makes it break.

If I still use vite-tsconfig-paths everything works as it should.

So the question now is, what does nxViteTsPaths try to solve?

@fsgreco
Copy link

fsgreco commented Jan 7, 2024

Hello @teofilomonteiro I've just found a new solution for this, explained everything in detail here: #21030

TL;DR: you can change the paths alias inside the tsconfig.base.json, as long as it does not match the name of the custom package/library (its name inside its package.json it will work fine). I still repute it a bug but at least this is an easy and not invasive solution.

@teofilomonteiro
Copy link

@fsgreco I don't think we can use your solution because we are publishing packages. And from what I understood from your suggestion you need to rename the package. Is that correct?

@MBelniak
Copy link

MBelniak commented Apr 3, 2024

Maybe this will help? #18931 (comment)

@fsgreco
Copy link

fsgreco commented Apr 3, 2024

@teofilomonteiro actually you can change the strategy and rename the exports instead. The package will still have it's name, but you have to rename the pathname on the tsconfigfile*. As long as they don't match it should work 👍🏽

Ps: let me know how it goes, it's been a couple of months since I create this issue and found the solution.

* and with that also every import inside the app on the monorepo that consume them but that could be done with a general search and replace

@Coly010
Copy link
Contributor

Coly010 commented May 16, 2024

Closing as duplicate of #20520
Which also has a better reproduction.

Thanks everyone for chiming in here, this is all valuable information

@Coly010 Coly010 closed this as completed May 16, 2024
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
Development

No branches or pull requests

8 participants