microsoft / microsoft/monaco-editor

[Bug] Getting Could not resolve "monaco-editor/esm/vs/editor/editor.worker"

Open
#5,188 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Reproducible in vscode.dev or in VS Code Desktop?
  • Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
Monaco Editor Playground Link

No response

Monaco Editor Playground Code

Reproduction Steps

I am using vite 5.1.21 and monaca editor 0.55.1 in an electron project and this is my main.ts. This works fine in debug mode, but when I try to build it I get the below. What am I doing wrong?

error during build:
[vite-plugin-moncao-editor-esm] Build failed with 1 error:
error: Could not resolve "monaco-editor/esm/vs/editor/editor.worker"

and

✓ 1780 modules transformed.
X [ERROR] Could not resolve "monaco-editor/esm/vs/editor/editor.worker"

  The module "./esm/vs/editor/editor.worker" was not found on the file system:

    node_modules/monaco-editor/package.json:44:11:
      44 │     "./*": "./*"
         ╵            ~~~~~

main.ts

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import {router} from './routes/router'
import { vuetify } from './plugins/vuetify';
import pinia from './plugins/pinia';
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js?worker';
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker.js?worker';
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker.js?worker';

self.MonacoEnvironment = {
  getWorker(_, label) {
    if (label === 'json') return new jsonWorker();
    if (label === 'typescript' || label === 'javascript') return new tsWorker();
    return new editorWorker();
  }
};

const app = createApp(App)

app.use(router);
app.use(vuetify);
app.use(pinia);

app.config.errorHandler = (err, instance, info) => {
  console.error("Vue error:", err);

  // fire your own event so App.vue can catch it
  window.dispatchEvent(
    new CustomEvent("vue-global-error", { detail: err })
  );
};

app.mount('#app').$nextTick(() => {
  window.ipcRenderer.on('main-process-message', (_event, message) => {
    console.log(message)
  })
})

and in vite.congif.ts

import { defineConfig } from 'vite'
import path from 'node:path'
import electron from 'vite-plugin-electron/simple'
import vue from '@vitejs/plugin-vue'
import vuetify from "vite-plugin-vuetify";
import { readFileSync } from 'node:fs';
import monacoEditorEsmPlugin from 'vite-plugin-monaco-editor-esm'

const pkg = JSON.parse(readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8'));

// https://vitejs.dev/config/
export default defineConfig({
  optimizeDeps:{
    include:[
      'monaco-editor/esm/vs/editor/editor.worker',
      'monaco-editor/esm/vs/language/json/json.worker',
      'monaco-editor/esm/vs/language/typescript/ts.worker'
    ]
  },
  plugins: [
    vue(),
    vuetify({autoImport:true}),
    monacoEditorEsmPlugin(),
    electron({
      main: {
        // Shortcut of `build.lib.entry`.
        entry: 'electron/main.ts',
        vite: {
          build: {
            rollupOptions: {
              external: ['keytar'],
            },
          },
        },
      },
      preload: {
        // Shortcut of `build.rollupOptions.input`.
        // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
        input: path.join(__dirname, 'electron/preload.ts'),
      },
      // Ployfill the Electron and Node.js API for Renderer process.
      // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
      // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
      renderer: process.env.NODE_ENV === 'test'
        // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
        ? undefined
        : {},
    }),
  ],
  define: {
    __APP_VERSION__: JSON.stringify(pkg.version),
  }
})
Actual (Problematic) Behavior

No response

Expected Behavior

No response

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with main.ts and vite.config.ts, then reproduce the production build using Vite 5.1.21, Monaco Editor 0.55.1, and vite-plugin-monaco-editor-esm. Check how the worker imports and optimizeDeps entries are resolved during the build. Done means the Electron project builds without the reported editor.worker resolution error.

Written by the indexing model from the issue text.

Assessment

Tech stack
electron, typescript, vite
Domain
build-system, desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.