You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are working in a monorepo and trying to migrate our apps to vite.
We have a React component package which is built using roolup. The rollup config builds two entrypoint:
index.js (CJS), this also builds the SASS and CSS files (index.scss & index.css at the same place)
index.esm.js (ES)
All build files are in a build folder. An abstract of the folder structure:
Lib
src (source files)
build
package.json
rollup.config.mjs
App
src
index.html
package.json
vite.config.ts
When trying to build the app we are blocked by this error: Failed to resolve entry for package "lib". The package may have incorrect main/module/exports specified in its package.json.
This code block reset the value of the resolved entrypoint (before the if the value is ./build/index.esm.js). Following up the code, we can see later that this leads the resolver to scan for the presence of index files at the root of the lib and not in the build folder. Which makes the resolve mechanism fail.
I was thinking of opening a PR to change the entry = '' to something like entry = path.dirname(entry). A quick local fix in the built code of vite changing the code to entry = "./build/" fixes the import issue.
vite v5.4.6 building for production...
transforming (1) index.htmlDeprecation [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
transforming (19) src/App.cssfile:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46574
const err = new Error(
^
Error: Failed to resolve entry for package "lib". The package may have incorrect main/module/exports specified in its package.json.
at packageEntryFailure (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46574:15)
at resolvePackageEntry (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46571:3)
at tryNodeResolve (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46387:16)
at ResolveIdContext.resolveId (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46137:19)
at PluginContainer.resolveId (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:48952:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:66208:15
at async internalImporter (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:37307:22) {
code: 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'
}
Node.js v18.19.1
npm ERR! Lifecycle script build failed with error:
npm ERR! Error: command failed
npm ERR! in workspace: app@0.0.0
npm ERR! at location: /home/holynoodle/dev/repro_vite/app
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
For those coming here with the same issue, here how I fixed it (thanks to @hi-ogawa). See the PR for details.
Solution:
Specifying the sass property in the package.json of the lib > exports > "." > sass.
This property needs to be present before the import and require. Putting it after result in the same error as in this issue.
Example:
{
"main": "build/index",
"module": "build/index.esm.js",
"exports": {
".": {
"sass": "./build/index.scss",
... Other properties like require or import
}
}
Describe the bug
We are working in a monorepo and trying to migrate our apps to vite.
We have a React component package which is built using roolup. The rollup config builds two entrypoint:
All build files are in a
build
folder. An abstract of the folder structure:When trying to build the app we are blocked by this error:
Failed to resolve entry for package "lib". The package may have incorrect main/module/exports specified in its package.json.
In my investigations I found this code block: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/resolve.ts#L1070-L1075
This code block reset the value of the resolved entrypoint (before the if the value is
./build/index.esm.js
). Following up the code, we can see later that this leads the resolver to scan for the presence ofindex
files at the root of the lib and not in thebuild
folder. Which makes the resolve mechanism fail.I was thinking of opening a PR to change the
entry = ''
to something likeentry = path.dirname(entry)
. A quick local fix in the built code of vite changing the code toentry = "./build/"
fixes the import issue.WDYT about such a solution?
Reproduction
https://github.com/HolyNoodle/vite-sass-reproduction
Steps to reproduce
Moving the file
./lib/build/index.scss
to./lib/index.scss
will also fix the problem.System Info
Used Package Manager
npm
Logs
vite v5.4.6 building for production...
transforming (1) index.htmlDeprecation [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
transforming (19) src/App.cssfile:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46574
const err = new Error(
^
Error: Failed to resolve entry for package "lib". The package may have incorrect main/module/exports specified in its package.json.
at packageEntryFailure (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46574:15)
at resolvePackageEntry (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46571:3)
at tryNodeResolve (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46387:16)
at ResolveIdContext.resolveId (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46137:19)
at PluginContainer.resolveId (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:48952:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:66208:15
at async internalImporter (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:37307:22) {
code: 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'
}
Node.js v18.19.1
npm ERR! Lifecycle script
build
failed with error:npm ERR! Error: command failed
npm ERR! in workspace: app@0.0.0
npm ERR! at location: /home/holynoodle/dev/repro_vite/app
Validations
The text was updated successfully, but these errors were encountered: