I am trying to create my own npm package for a react component. I am getting the following error:
Here is the project structure that I am using
Here’s my index.ts file
import LeetcodeCalendar from "./components/LeetcodeCalendar";
export default LeetcodeCalendar;
My rollup.config.mjs
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { readFileSync } from 'fs';
import dts from "rollup-plugin-dts";
import peerDepsExternal from "rollup-plugin-peer-deps-external";
const packageJson = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
export default [
{
input: "src/index.ts",
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
},
{
file: packageJson.module,
format: "esm",
sourcemap: true,
},
],
plugins: [
peerDepsExternal(),
resolve({ extensions: [".js", ".ts", ".jsx", ".tsx"] }),
commonjs(),
typescript({
noEmitOnError: true,
tsconfig: './tsconfig.json',
declaration: true,
declarationDir: 'dist',
}),
terser(),
],
external: ["react", "react-dom"],
},
{
input: "src/index.ts",
output: [{ file: packageJson.types, format: "es" }],
plugins: [dts.default],
},
];
tsconfig.json file
{
"compilerOptions": {
"target": "ES5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react-jsx",
"rootDir": "src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
I have tried the following:
- Initially I was using “rollup.config.js” now with esm module
- I have tried merging my component/LeetcodeCalendar directly as index.tsx file… This creates various more error.
- When I work with this component in a react project, it works. However, I am unable to create a npm package out of it.
Attaching image for package.json