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

在StackBlitz中使用react-monaco-editor遇process重复声明错误求助

解决StackBlitz中react-monaco-editor的Cannot declare a const variable twice: 'process'错误

这个错误是因为StackBlitz内置环境已声明process变量,而react-monaco-editor依赖的包重复声明了该变量,导致冲突。以下是两种可行的解决方法:

方法一:改用维护活跃的替代库

react-monaco-editor维护进度较慢,适配现代开发环境存在问题,推荐使用@monaco-editor/react替代:

  • 执行命令替换依赖:
    npm uninstall react-monaco-editor && npm install @monaco-editor/react
    
  • 修改组件代码:
    import * as React from 'react';
    import { Editor } from '@monaco-editor/react';
    
    export default class EditorBasic extends React.Component<{}, {}> {
      render() {
        return (
          <Editor
            width="400px"
            height="500px"
            options={{ minimap: { enabled: false }, lineNumbers: 'off' }}
            value="content"
            theme="vs-dark"
          />
        );
      }
    }
    

方法二:保留原库并修复变量冲突

如果必须使用react-monaco-editor,可以通过配置屏蔽重复声明:

  • 在项目根目录创建stackblitz.config.js,添加以下配置:
    module.exports = {
      resolve: {
        fallback: {
          process: false
        }
      }
    };
    
  • 或者在项目入口文件最顶部添加全局声明:
    declare const process: any;
    
    注意:这种方法可能引发其他潜在兼容性问题,不推荐作为长期方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:35:29