[Bug] Getting Could not resolve "monaco-editor/esm/vs/editor/editor.worker" #21426
Replies: 3 comments
-
|
this is a common issue with monaco in vite + electron. the problem is how vite handles web workers during build. fix:
npm install vite-plugin-monaco-editor-nls -D
import monacoEditorPlugin from "vite-plugin-monaco-editor-nls";
export default defineConfig({
plugins: [
monacoEditorPlugin({
languageWorkers: ["editorWorkerService", "json", "typescript"]
}),
// ... other plugins
],
// remove optimizeDeps for monaco
});
// DELETE these lines
// 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";
npm install @monaco-editor/reactthis handles worker loading automatically.
build: {
rollupOptions: {
output: {
manualChunks: {
"monaco-editor": ["monaco-editor"]
}
}
}
}the root cause is that vite resolves esm paths differently in dev vs build. the plugin approach abstracts this away. |
Beta Was this translation helpful? Give feedback.
-
|
Hey! Classic monaco-editor + Vite headache. Worker paths not handled correctly in production builds. Solution 1: Use vite-plugin-monaco-editor (recommended) npm uninstall vite-plugin-monaco-editor-esm
npm install vite-plugin-monaco-editor -Dimport monacoEditorPlugin from 'vite-plugin-monaco-editor'
export default defineConfig({
plugins: [monacoEditorPlugin({})]
})Remove your manual MonacoEnvironment setup - the plugin handles everything. Solution 2: Manual worker setup import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
self.MonacoEnvironment = {
getWorker(_, label) {
if (label === 'json') return new jsonWorker()
return new editorWorker()
}
}Key point: Dont mix plugin-based setup with manual MonacoEnvironment - pick one approach. |
Beta Was this translation helpful? Give feedback.
-
|
I used this and then in plugins in vite.config.ts and then removed this from main.ts then I get this error |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
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?
and
main.ts
and in vite.congif.ts
Reproduction
https://github.com/microsoft/monaco-editor
Steps to reproduce
No response
System Info
System: OS: Windows 11 10.0.22000 CPU: (4) x64 Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz Memory: 8.94 GB / 32.00 GB Binaries: Node: 22.21.1 - C:\nvm4w\nodejs\node.EXE npm: 10.9.4 - C:\nvm4w\nodejs\npm.CMD Browsers: Edge: Spartan (44.22000.2600.0), Chromium (143.0.3650.80) Internet Explorer: 11.0.22000.2600Used Package Manager
npm
Logs
No response
Validations
Beta Was this translation helpful? Give feedback.
All reactions