question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vite cannot import react component from a workspace package

See original GitHub issue

Describe the bug

I am trying to import a Button component from a workspace package shared-ui from a vite react app but i am getting 'Button' is not exported by ../shared-ui/dist/index.js, imported by src/App.tsx error.

Reproduction

https://github.com/unworthyEnzyme/react-monorepo

Steps to reproduce

To reproduce clone this repository and npm install and then run npx nx build my-vite-app. I thought maybe i was doing something wrong and created a react app with create-react-app and if you npx nx build my-app you can see there are no errors.

System Info

Windows 10 
Node v18.0
npm 8.6.0

Used Package Manager

npm

Logs

‘Button’ is not exported by …/shared-ui/dist/index.js, imported by src/App.tsx file: C:/Users/asus/Desktop/package-based/packages/my-vite-app/src/App.tsx:1:9 1: import { Button } from ‘shared-ui’; ^ 2: function App() { 3: return ( error during build: Error: ‘Button’ is not exported by …/shared-ui/dist/index.js, imported by src/App.tsx at error (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:1858:30) at Module.error (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:12429:16) at Module.traceVariable (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:12788:29) at ModuleScope.findVariable (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:11440:39) at FunctionScope.findVariable (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:6372:38) at ChildScope.findVariable (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:6372:38) at Identifier.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:7439:40) at CallExpression.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5265:73) at CallExpression.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:8935:15) at Property.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5269:23) at ObjectExpression.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5265:73) at CallExpression.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5265:73) at CallExpression.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:8935:15) at ReturnStatement.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5269:23) at BlockStatement.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5265:73) at FunctionDeclaration.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5269:23) at Program.bind (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:5265:73) at Module.bindReferences (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:12425:18) at Graph.sortModules (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:23062:20) at Graph.build (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:22940:14) at async file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:23730:13 at async rollupInternal (file:///C:/Users/asus/Desktop/package-based/node_modules/rollup/dist/es/shared/rollup.js:23727:5) at async doBuild (file:///C:/Users/asus/Desktop/package-based/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:45395:24) at async build (file:///C:/Users/asus/Desktop/package-based/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:45233:16) at async CAC.<anonymous> (file:///C:/Users/asus/Desktop/package-based/node_modules/vite/dist/node/cli.js:756:9)

Validations

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bluwycommented, Nov 25, 2022

When you import 'shared-ui', Vite needs to resolve shared-ui to an actual file in your file system to load it. If that path doesn’t include the /node_modules/ string, it’s a linked package, e.g. /Users/bjorn/projects/my-linked-dep/index.js.

When using a monorepo that links packages up, you get a path like above so Vite can know it’s a linked dep. About realpath, you can check out the nodejs docs. Because linking creates a symlink, you could be initially referring to /Users/bjorn/projects/my-project/node_modules/my-linked-dep/index.js. Calling realpath returns the actual file which is /Users/bjorn/projects/my-linked-dep/index.js.

0reactions
JUNNNIcommented, Nov 25, 2022

Understood! Thanks for the clarification

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot import Library build package created for React tsx ...
[plugin: vite:dep-scan] Failed to resolve entry for package "@reporting-ui-component". The package may have incorrect main/module/exports ...
Read more >
yarn workspaces monorepo with vite, react, tailwind
I have created a component inside my apps/ui/src folder and when I run yarn workspace ui run dev , the app can be...
Read more >
Configuring Vite
Config File Resolving # ... When running vite from the command line, Vite will automatically try to resolve a config file named vite.config.js...
Read more >
unplugin-vue-components
Components auto importing for Vue. Latest version: 0.22.12, last published: 12 days ago. Start using unplugin-vue-components in your project ...
Read more >
Create a React component library with Vite and Typescript
We use yarn workspaces to manage dependencies. To configure our monorepo, we need to create a package.json file at the repository's root. { ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found