'Package built with tsup/esBuild can't find `react` in react-scripts app

I have an npm package that I built using esBuild that I am using in a react-scripts application. I'm running a build on the package and manually copying and pasting the build folder to /node_modules in the react-scripts app due to an issue with volta and npm-link.

When I run my application I get an error in my npm package code Context.js:4 Uncaught TypeError: undefined is not a function it's not finding createContext that's imported from react.

import { createContext } from 'react';

const EventContext = createContext();

Here is my tsup.config.js file for the npm package:

import { sassPlugin } from '@es-pack/esbuild-sass-plugin';
import { defineConfig } from 'tsup';

export default defineConfig({
  entry: ['src/index.js'],
  splitting: true,
  sourcemap: true,
  clean: true,
  dts: true,
  format: ['esm'],
  target: 'es2020',
  bundle: true,
  esbuildPlugins: [sassPlugin()],
});

And these are my dependencies and peerDependencies in the npm package

"peerDependencies": {
    "classnames": ">=2.0.0 <3.0.0",
    "prop-types": ">=15.0.0 <16.0.0",
    "react": ">=17.0 <18.0.0",
    "react-dom": ">=17.0 <18.0.0"
  },
  "dependencies": {
    "@xstate/react": "^2.0.1",
    "focus-trap": "^6.6.1",
    "react-share": "^4.4.0",
    "xstate": "^4.30.6"
  },

Is there an issue between the react-scripts build setup and the esBuild package output? My application has [email protected] installed and works fine without my npm package installed. Why can't my npm package code import react?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source