2

I am writing my own React library with Typescript. I have a lot of problems with building and using it. I am using webpack with ts loader to build files into lib folder. When I am trying to use my components from build

import React from 'react'
import { Text } from '../lib'

export default () => {
    return <Text lg>Blabla</Text>
}

I get an error

index.js:9 Uncaught Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

What is wrong?

tsconfig

{
 "compilerOptions": {
 "target": "es5",
 "lib": ["dom", "esnext"],
 "allowJs": true,
 "outDir": "./lib/",
 "noImplicitAny": true,
 "allowSyntheticDefaultImports": true,
 "module": "esnext",
 "jsx": "react",
 "moduleResolution": "node",
 "esModuleInterop": true,
 "declaration": true,
 "typeRoots": ["node_modules/@types"]
},
"include": ["src"],
"exclude": ["node_modules"]
}

webpack

const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
  entry: path.resolve(__dirname, '../src/index.ts'),
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, '../lib'),
    library: 'my-library',
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  plugins: [new CleanWebpackPlugin()]
}

package.json

{
  "name": "my-library",
  "version": "1.0.0",
  "private": true,
  "types": "lib",
  "main": "lib/index.js",
  "dependencies": {
    "@types/node": "^14.0.11",
    "@types/react": "^16.9.35",
    "@types/react-dom": "^16.9.8",
    "@types/styled-components": "^5.1.0",
    "clean-webpack-plugin": "^3.0.0",
    "html-webpack-plugin": "^4.3.0",
    "prettier": "^2.0.5",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "styled-components": "^5.1.1",
    "ts-loader": "^7.0.5",
    "typescript": "^3.9.5",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "build": "webpack --config config/webpack.prod.js",
    "dev": "webpack-dev-server --mode development --open --hot --config config/webpack.dev.js",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
  },
  "devDependencies": {
    "@babel/core": "^7.10.2",
    "@storybook/addon-actions": "^5.3.19",
    "@storybook/addon-links": "^5.3.19",
    "@storybook/addons": "^5.3.19",
    "@storybook/react": "^5.3.19",
    "babel-loader": "^8.1.0"
  }
}

1 Answer 1

1

TL;DR I got this issue before. The issue may caused by Have multiple instances of React. I got the solution from here: https://github.com/facebook/react/issues/16029#issuecomment-570912067


And, this is how I resolve the issue with my package.json and webpack configuration.

  1. In package.json:
  • Move react into devDependencies.
  • Add react into peerDependencies too.
{
  "name": "react-customize-token-input",
  
  // ... omit

  "peerDependencies": {
    "react": "^16.10.2 || ^17.0.0 || ^18.0.0"
  },
  "devDependencies": {

    // ... omit

    "react": "^17.0.2",

    // ... omit
  }
}
  1. In webpack:
  • Add react into externals
  externals: {
    react: {
      root: 'React',
      commonjs2: 'react',
      commonjs: 'react',
      amd: 'react',
    },
  },

Example: https://github.com/seawind543/react-token-input/pull/7/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.