You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建集成Redux的Vite React应用失败,遇createContext导出错误

解决React+Vite+Redux项目pnpm build时"createContext is not exported"错误

问题重现

执行pnpm build时出现以下错误:

$ pnpm build

> temp-vite@1.0.0 build
> tsc && vite build

vite v4.3.9 building for production...
✓ 639 modules transformed.
✓ built in 1.67s
"createContext" is not exported by "../../node_modules/.pnpm/react@18.2.0/node_modules/react/index.js", imported by "../../node_modules/.pnpm/react-redux@8.0.5_@types+react-dom@18.0.11_@types+react@18.0.37_react-dom@18.2.0_react@18.2.0_redux@4.2.1/node_modules/react-redux/es/components/Context.js".
file: .pnpm/react-redux@8.0.5_@types+react-dom@18.0.11_@types+react@18.0.37_react-dom@18.2.0_react@18.2.0_redux@4.2.1/node_modules/react-redux/es/components/Context.js:1:9
1: import { createContext } from 'react';
            ^
2: export const ReactReduxContext = /*#__PURE__*/createContext(null);
error during build:
RollupError: "createContext" is not exported by "../../node_modules/.pnpm/react@18.2.0/node_modules/react/index.js", imported by "../../node_modules/.pnpm/react-redux@8.0.5_@types+react-dom@18.0.11_@types+react@18.0.37_react-dom@18.2.0_react@18.2.0_redux@4.2.1/node_modules/react-redux/es/components/Context.js".

ELIFECYCLE  Command failed with exit code 1.

解决方案

按优先级尝试以下方法:

1. 清理pnpm缓存并重装依赖

缓存损坏可能导致依赖解析异常,执行命令:

pnpm store prune
pnpm install

2. 修复依赖版本冲突

  • 执行pnpm list react react-redux查看依赖树,确认是否存在多版本React。如有,合并依赖版本:
    pnpm dedupe
    
  • 升级react-redux到最新兼容版本:
    pnpm update react-redux
    

3. 配置Vite解析规则

在vite.config.ts中添加配置,确保正确解析React的ES模块导出:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  resolve: {
    mainFields: ['browser', 'module', 'main'],
    alias: {
      'react': 'react/index.js'
    }
  },
  optimizeDeps: {
    include: ['react', 'react-redux']
  }
})

4. 检查自定义别名是否错误

若项目配置了React别名,确保指向生产环境入口文件,避免指向react/cjs/react.development.js这类仅用于开发的文件。


内容的提问来源于stack exchange,提问作者Armen Babakanian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:52:59