Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gbradley's avatar

Library with React dependency causes build error

I'm tearing my hair out trying to understand why my React app is failing in Vite's production build.

My setup is this:

  • in the Laravel project root, my package.json specifies react and react-dom as dependencies
  • in vite.config.js, under resolve.alias I am pointing to a package inside vendor which contains my JS
  • inside the package, I have react and react-dom listed as peer dependencies, and inside the vite config I have them inside rollupOptions.exclude so they are not included in the build.

Running npm run dev works without any problems. But running npm build I then get: "TypeError: fe.current is null (useState)". If I comment out useState inside the package JS, then the error disappears.

Any hints as to where I could be going wrong?

0 likes
2 replies
LaryAI's avatar
Level 58

It sounds like you're encountering an issue where the production build process is not handling your React dependencies correctly. Here's a step-by-step approach to troubleshoot and potentially solve the problem:

  1. Check for Multiple Instances of React: Ensure that there are no multiple instances of React being loaded. This can happen if your package inside vendor is bundling its own version of React. Since you've mentioned that React and ReactDOM are listed as peer dependencies, make sure that they are not being bundled with your package.

  2. Verify Vite's External Configuration: In your vite.config.js, you've mentioned using rollupOptions.exclude to exclude React from the build. However, the correct property to use with Vite and Rollup should be external. Here's how you can specify external dependencies in your Vite config:

    // vite.config.js
    export default {
      // ... other config options
      build: {
        rollupOptions: {
          external: ['react', 'react-dom'],
        },
      },
    };
    
  3. Check the Library's Build Process: If the library you're using from the vendor directory has its own build process, ensure that it's correctly set up to treat React and ReactDOM as external dependencies. This will prevent them from being included in the library's bundle.

  4. Inspect the Error Message: The error message "TypeError: fe.current is null (useState)" suggests that there might be an issue with the way hooks are being used. This could be a symptom of having multiple React instances. Make sure that the library's components are using hooks correctly and that they are not relying on any global state that might not be initialized in a production build.

  5. Clean Install Dependencies: Sometimes, issues can arise from corrupted or outdated node modules. Try deleting your node_modules folder and the package-lock.json file (or yarn.lock if you're using Yarn), and then run npm install (or yarn install) to do a clean install of your dependencies.

  6. Check for Environment-Specific Code: Since the issue only appears in the production build, check if there's any code that behaves differently based on the environment (e.g., using process.env.NODE_ENV). Ensure that such code is correctly configured for production.

  7. Review the Library's Package.json: Make sure that the library's package.json is correctly set up with peerDependencies and that it doesn't include react and react-dom in dependencies or devDependencies which might cause them to be bundled.

  8. Test with a Minimal Setup: Create a minimal setup that only includes the library and tries to build it with Vite. This can help isolate the issue and determine if the problem is with the library or the way it's integrated into your project.

Here's a minimal example of how your vite.config.js might look:

// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      // your aliases
    },
  },
  build: {
    rollupOptions: {
      external: ['react', 'react-dom'],
    },
  },
});

If none of these steps resolve the issue, consider providing more specific error logs or a reproduction repository, as this can greatly help in diagnosing the problem.

gbradley's avatar
gbradley
OP
Best Answer
Level 1

Turns out there were two versions of React being installed with npm run build (but only one with dev). Adding @vitejs/plugin-react resolved the issue.

Please or to participate in this conversation.