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 7 years ago
- Reactions: 9
- Comments: 35 (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 6 years ago
- chore: Ace 에디터 경고 제거 참고: https://github.com/securingsincity/react-ace/issues/725#issuecomment-543109155 — committed to boostcampwm-2021/web04-canyoufixme by Xvezda 5 years ago
- Fix to import xml See : https://github.com/securingsincity/react-ace/issues/725 — committed to fugerit-org/fj-doc by fugerit79 3 years 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:
For those of you using Vite, I was able to get it working using Explicit URL Imports (appending
?urlto the end of the worker import):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.
I fixed by adding
useWorker: falseto setOptions if importing modules directly. If this is a good solution,README.mdbasic usage should be updated unless I’m missing something.I leave the full
NextJS@9.3integration 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-buildsbeforereact-acefails in Firefox for me. I got it to work with:But I couldn’t get it to work with my tests. The
webpack-resolverimport 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: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.
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-uploadis deprecated with Webpack 5asset/resource.webpack config:
component:
To fix the typescript “no module declaration” error, just add the
global.d.tsfile to yoursrcfolder:I added the following test to Webpack
then I was able to import as following
if you use vite, you can …
@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)