electron-vue 整合monaco editor时报Uncaught Error: Cannot find module 'monaco-editor'

发布时间:2024-01-11 17:15:47

在electron-vue中整合monaco editor报了如下错误

Uncaught Error: Cannot find module 'monaco-editor' 
    at Module._resolveFilename (module.js:543:15)
    at Function.Module._resolveFilename (E:\vue-monaco-electron\node_modules\electron\dist\resources\electron.asar\common\reset-search-paths.js:35:12) 
    at Function.Module._load (module.js:473:25)
    at Module.require (module.js:586:17)
    at require (internal/module.js:11:18)
    at eval (webpack:///external_%22monaco-editor%22?:1:18) 
    at Object.monaco-editor (http://localhost:9080/renderer.js:1790:1) 
    at __webpack_require__ (http://localhost:9080/renderer.js:791:30) 
    at fn (http://localhost:9080/renderer.js:102:20)

安装

npm install --save monaco-editor@0.27.0
npm install --save monaco-editor-webpack-plugin@4.2.0

使用

<template>
    <div ref="main" style="width: 100%; height: 300px"></div>
  </template>
  
  <script>
  import * as monaco from "monaco-editor";
  
  export default {
    data() {
      return {
        monacoEditor: null,
      };
    },
    mounted() {
      this.init();
    },
    methods: {
      init() {
        // 使用 - 创建 monacoEditor 对象
        this.monacoEditor = monaco.editor.create(this.$refs.main, {
          theme: "vs-dark", // 主题
          value: "console.log(1111)", // 默认显示的值
          language: "javascript",
          folding: true, // 是否折叠
          foldingHighlight: true, // 折叠等高线
          foldingStrategy: "indentation", // 折叠方式  auto | indentation
          showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
          disableLayerHinting: true, // 等宽优化
          emptySelectionClipboard: false, // 空选择剪切板
          selectionClipboard: false, // 选择剪切板
          automaticLayout: true, // 自动布局
          codeLens: false, // 代码镜头
          scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
          colorDecorators: true, // 颜色装饰器
          accessibilitySupport: "off", // 辅助功能支持  "auto" | "off" | "on"
          lineNumbers: "on", // 行号 取值: "on" | "off" | "relative" | "interval" | function
          lineNumbersMinChars: 5, // 行号最小字符   number
          enableSplitViewResizing: false,
          readOnly: false, //是否只读  取值 true | false
        });
      },
    },
  };
  </script>
  

在这里就报了标题中的错误,最后在github中找到了解决办法,引入方式要如下 https://github.com/microsoft/monaco-editor/issues/2624

import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js'

Card image cap
APP STORE
Card image cap
应用宝
Card image cap
小米
Card image cap
华为