Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Could not resolve entry module React + Rollup

I need to build shareable React component which could be used across apps.

For this, I was/am following the below article

  • https://dev.to/alexeagleson/how-to-create-and-publish-a-react-component-library

My Configuration looks exactly the same except the npm packages version (even tried with the same versions)

The folder structure looks the same as below

enter image description here

rollup.config.js

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import dts from "rollup-plugin-dts";

const packageJson = require("./package.json");

export default [
{
 input: "src/index.ts",
 output: [
  {
    file: packageJson.main,
    format: "cjs",
    sourcemap: true,
  },
  {
    file: packageJson.module,
    format: "esm",
    sourcemap: true,
  },
],
plugins: [resolve(), commonjs(), typescript({ tsconfig: "./tsconfig.json" })],
},
{
 input: "dist/esm/types/index.d.ts",
 output: [{ file: "dist/index.d.ts", format: "esm" }],
 plugins: [dts()],
},
];

npm script

"rollup": "rollup -c"

However when I run npm run rollup this throws the below error

[!] Error: Could not resolve entry module (dist/esm/types/index.d.ts).
Error: Could not resolve entry module (dist/esm/types/index.d.ts)

Please suggest. Thanks!

like image 789
Kgn-web Avatar asked Sep 10 '25 19:09

Kgn-web


2 Answers

I also ran into the same problem you are experiencing when working with rollup. After spending some while digging for the solution, I finally got to solve this problem.

My Configuration looks exactly the same except the npm packages version (even tried with the same versions)

The exception you have stated is actually the problem. The problem lies in package versioning. The package @rollup/plugin-typescript versions later than 8.3.3 are not generating nor storing the declaration files in the types folder expected to be at the path: dist/cjs/ and dist/esm/.

The latest version at this point in time is 8.5.0 which still breaks. Hopefully it is fixed in near future.

Steps to fix your error

  • Make sure your tsconfig.json file has "declarationDir": "types" to direct the bundler's typescript plugin to create and store declaration files in the types folder when you run npm run rollup
  • Uninstall the existing @rollup/plugin-typescript package version by running npm un @rollup/plugin-typescript --save-dev
  • Install @rollup/plugin-typescript with the command npm i @rollup/[email protected] --save-dev. As you see, we are picking a specific version.

If you still encounter problems:

  • Manually update the package.json file like: "@rollup/plugin-typescript": "8.3.3". Note that I have removed the caret(^) symbol to tie the package to version 8.3.3.
  • Delete the node_modules folder. You could use the command rm -rf node_modules.
  • Delete package-lock.json.
  • Run npm i to install the packages again with versions specified in package.json

UPDATE


This issue is fixed as from version 10.0.0. Things should work normally without being stuck at an old version.

like image 56
hane Smitter Avatar answered Sep 13 '25 09:09

hane Smitter


Here's an working answer for people coming from 2023 that doesn't lock you to an outdated version of @rollup/plugin-typescript:

Preconditions: Make sure that you get rid off your package-lock.json and your node_modules directory so that you can start from a clean slate and install your project again.

  1. run npm install tslib --save-dev
  2. add "type": "module" to package.json
  3. in tsconfig.json, add "rootDir": "src"
  4. in rollup.config.js, change plugins: [dts()] to plugins: [dts.default()]
  5. back in package.json, add --bundleConfigAsCjs as a parameter to the rollup command in scripts

After that you should be able to continue with the tutorial and be able to create a new build via npm run rollup.

like image 25
StefanGreve Avatar answered Sep 13 '25 09:09

StefanGreve