Sometimes it's even not compiling and showing error on browser . Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of Home.


Folder and export structure .


src
 |
 +-- components
 |  | 
 +--+------ Button
 |  |       | 
 |  |       +-- Button.tsx 
 |  |       |
 |  |       +-- index.tsx  //export * from './Button';
 |  |       | 
 |  |       OtherComponent
 |  |       |  
 |  |       +-- Conponent.tsx
 |  |       |          
 |  |       +-- index.tsx
 |  |  
 |  +index.tsx // export * from './Button'
 |  
 +index.tsx // export * from 'components'  

Button File

import React, { FC, ReactNode } from 'react';
import { Button as BootstrapButton, ButtonProps } from 'react-bootstrap';

export interface customButtonProps extends ButtonProps {
  children: ReactNode;
  isDropdownMenuButton?: boolean;
};

export const Button: FC<customButtonProps> = ({children, isDropdownMenuButton, ...props}) => (
  <BootstrapButton className={isDropdownMenuButton ? 'btn-dropdown-menu' : ''} {...props}>
    <span className="btn-lines btn-lines--left">
      <span className="btn-lines btn-lines--right">{ children }</span>
      <span className="btn-lines--close"></span>
    </span>
  </BootstrapButton>
);

Rollup Configuration

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import postcss from "rollup-plugin-postcss";
import dts from "rollup-plugin-dts";
import packageJson from "./package.json" assert { type: "json" };
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { terser } from "rollup-plugin-terser";
import alias from '@rollup/plugin-alias';
import image from '@rollup/plugin-image';

export default [
  {
    input: "src/index.tsx",
    output: [
      {
        file: packageJson.main,
        format: "cjs",
        sourcemap: true,
      },
      {
        file: packageJson.module,
        format: "esm",
        sourcemap: true,
      },
    ],
    plugins: [
      alias({
        // Define an alias for your library path
        entries: [
          {
            find: 'ui-library',
            replacement: './dist/esm'  // Specify the relative path to the dist directory
          }
        ]
      }),
      resolve(),
      commonjs(),
      typescript({ tsconfig: "./tsconfig.json" }),
      postcss(),
      peerDepsExternal(),
      terser(),
      image()
    ],
  },
  {
    input: "dist/esm/types/index.d.ts",
    output: [{ file: "dist/index.d.ts", format: "esm" }],
    plugins: [dts()],
    external: [/\.(css|less|scss)$/],
  },
];

tsconfi.json

{
    "compilerOptions": {
      // Default
      "target": "es5",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strict": false,
      "skipLibCheck": true,
  
      // Added
      "jsx": "react",
      "module": "ESNext",
      "declaration": true,
      "declarationDir": "types",
      "sourceMap": true,
      "outDir": "dist",
      "moduleResolution": "node",
      "allowSyntheticDefaultImports": true,
      "emitDeclarationOnly": true,
      "rootDir": "src",
  
      // Missing Options from the First Configuration
      "removeComments": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "baseUrl": "./",
      "incremental": true,
      },
    "include": ["src/**/*"]  
}

pcakag.json

{
  "name": "cgpt-ui-library",
  "version": "1.0.1",
  "description": "",
  "main": "dist/cjs/index.js",
  "module": "dist/esm/index.js",
  "files": [
    "dist"
  ],
  "type": "module",
  "scripts": {
    "rollup": "rollup -c",
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "npm run rollup",
    "build:esm": "tsc",
    "build:cjs": "tsc --module commonjs --outDir dist/cjs",
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@rollup/plugin-alias": "^5.0.1",
    "@rollup/plugin-commonjs": "^25.0.5",
    "@rollup/plugin-node-resolve": "^15.2.3",
    "@rollup/plugin-typescript": "^11.1.5",
    "@storybook/addon-essentials": "^7.4.6",
    "@storybook/addon-interactions": "^7.4.6",
    "@storybook/addon-links": "^7.4.6",
    "@storybook/addon-onboarding": "^1.0.8",
    "@storybook/blocks": "^7.4.6",
    "@storybook/react": "^7.4.6",
    "@storybook/react-vite": "^7.4.6",
    "@storybook/testing-library": "^0.2.2",
    "@types/react": "^18.2.22",
    "bootstrap": "^5.3.2",
    "chromatic": "^7.2.0",
    "css-loader": "^6.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "rollup": "^4.0.2",
    "rollup-plugin-dts": "^6.1.0",
    "rollup-plugin-peer-deps-external": "^2.2.4",
    "rollup-plugin-postcss": "^4.0.2",
    "rollup-plugin-terser": "^7.0.2",
    "sass": "^1.68.0",
    "storybook": "^7.4.6",
    "typescript": "^5.2.2"
  },
  "dependencies": {
    "@emotion/react": "^11.11.1",
    "@emotion/styled": "^11.11.0",
    "@rollup/plugin-image": "^3.0.3",
    "@types/react": "^18.0.15",
    "bootstrap": "^5.3.2",
    "react": "^18.2.0",
    "react-bootstrap": "^2.9.0",
    "react-dom": "^18.2.0",
    "typescript": "^5.2.2"
  },
  "peerDependencies": {
    "@types/react": "^18.0.15",
    "@types/react-dom": "^18.0.6",
    "bootstrap": "^5.3.2",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "types": "dist/index.d.ts"
}

**usage in react project after installation as a dependency **

import { Button } from "ui-library";


export default function Home() {

  return (
    <main style={{ background: '#000000' }}>
      <>
<Button >
        Default Button
      </Button>
</>
   </main>
  )
}
0

There are 0 best solutions below