react-ace: Could not load worker
Problem
Couldn’t load worker, JSON syntax validation doesn’t work. I see Could not load worker
warning in the browser console.
Also, I tried to import the json worker
import "ace-builds/src-noconflict/worker-json";
and got the error
TypeError: Cannot read property 'window' of undefined
Sample code to reproduce the issue
import React from "react";
import ReactDOM from "react-dom";
import AceEditor from "react-ace";
import "ace-builds/src-noconflict/ace";
import "ace-builds/src-noconflict/mode-json";
import "ace-builds/src-noconflict/theme-github";
let text =
'{\n "id": 0,\n ' +
'"script": """\n function add(x, y) {\n return x + y;\n }\n add(1, 2);\n """' +
',\n "descr": "add two numbers"\n}';
function App() {
return (
<div className="App">
<h1>Code Editor Demo</h1>
<AceEditor
mode="json"
theme="github"
onChange={(value, stat) => {
console.log("onChange", value, stat);
}}
value={text}
name="UNIQUE_ID_OF_DIV"
editorProps={{ $blockScrolling: true }}
/>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
References
Codesandbox https://codesandbox.io/embed/ace-worker-3-vrr68
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 8
- Comments: 34 (1 by maintainers)
Commits related to this issue
- fix: do not use workers for code editor They were causing issues (see https://github.com/securingsincity/react-ace/issues/725) and were not necessary anyways. — committed to startup-cto/christmas-delivery by dbartholomae 4 years ago
- chore: Ace 에디터 경고 제거 참고: https://github.com/securingsincity/react-ace/issues/725#issuecomment-543109155 — committed to boostcampwm-2021/web04-canyoufixme by Xvezda 3 years ago
- Fix to import xml See : https://github.com/securingsincity/react-ace/issues/725 — committed to fugerit-org/fj-doc by fugerit79 9 months ago
You need to either include the webpack-resolver
or configure the url for each mode separately
both methods require “file-loader”
also react-ace tries to load brace if ace is not already imported so ace-builds needs to be imported before react-ace.
setOptions={{ useWorker: false }}
works for me. Thanks @khoomeister
Did anyone get this working with create-react-app? Mine gets stuck compiling forever if I add:
I added the last 3 lines to add javascript-worker :
You can copy/paste the file worker-javascript.js in your local environment if you don’t want to be dependant of cdn.jsdelivr.net.
For those of you using Vite, I was able to get it working using Explicit URL Imports (appending
?url
to the end of the worker import):I fixed by adding
useWorker: false
to setOptions if importing modules directly. If this is a good solution,README.md
basic usage should be updated unless I’m missing something.I leave the full
NextJS@9.3
integration here.I’m experiencing something similar for CSS, getting the following error in browser console:
When attempting to import worker directly
My build failed with:
If you want to use the workarounds in Typescript you’ll need to do this to set ace config:
Adding webpack-resolver to my create-react-app project leads to a huge increase in compile time, and adds hundreds of files to the build directory - I assume one for each module in ace-builds that it configures. Configuring the url for just the modules that I need works fine, though.
This seems like it’s an issue with either react-scripts or ace-builds, not react-ace.
@nightwing that works but importing
ace-builds
beforereact-ace
fails in Firefox for me. I got it to work with:But I couldn’t get it to work with my tests. The
webpack-resolver
import line fails in tests withI’m guessing I need mock ace with something like
@Cactusx09 thank you!!!
For those who don’t know how to add webpack configs to a nextjs project, here’s my snippet:
next.config.js:
The following seems to have worked for me. The key is the order of imports and the last three lines (including the comment!) I got it from this reply.
For those searching how to implement workers now, when
file-upload
is deprecated with Webpack 5asset/resource
.webpack config:
component:
To fix the typescript “no module declaration” error, just add the
global.d.ts
file to yoursrc
folder:I added the following test to Webpack
then I was able to import as following
I’m using a create-react-app project.
The only thing I needed to do was to add this in my App.tsx:
Setting useWorker to false disables syntax checking at least, maybe other things, so I didn’t want to use that.
I don’t know yet if this is good enough to make it work with tests as per arjunu’s comment above.
@jan-osch so if I want to use my “customWorker.js” for example, i’ve to set
ace.config.setModuleUrl("ace/mode/myWorker", "/customWorker.js")
? (putting customWorker.js in the /public folder)if you use vite, you can …