Troubleshooting "Module not found" error with react-dom/server in a Slack thread.
44 replies
Last updated: Jun 8, 2020
L
How do i solve:
• i have react-dom installed in package.json
Seems it tries to find it in:
Module not found: Error: Can't resolve 'react-dom/server'
Seems it tries to find it in:
studio/schemas/objects
Jun 8, 2020, 9:35 AM
P
How are you importing it at the moment?
Jun 8, 2020, 10:38 AM
L
import ReactDOMServer from "react-dom/server";
Jun 8, 2020, 10:39 AM
L
As stated here:
Jun 8, 2020, 10:39 AM
P
And it’s in your
package.jsonunder
dependenciesor
devDependenciesfor example?
Jun 8, 2020, 10:42 AM
L
"react-dom": "^16.13.1",
Jun 8, 2020, 10:43 AM
P
Could you try removing your
Also, what Node version are you using (
node_modulesfolder and
package-lock.jsonfile before running
npm installto see if that resolves it?
Also, what Node version are you using (
node -v)?
Jun 8, 2020, 10:50 AM
L
v12.16.3
Jun 8, 2020, 10:51 AM
L
It works on my website using React, so it must be something specific to Sanity
Jun 8, 2020, 10:56 AM
L
Updated to latest node version, no change
Jun 8, 2020, 10:59 AM
L
Here is the entire error if that helps:
Jun 8, 2020, 11:00 AM
P
Have you tried the above procedure as well with your
node_modulesfolder and the
package-lock.jsonfile?
Jun 8, 2020, 11:00 AM
L
yes
Jun 8, 2020, 11:01 AM
L
Tried using both Npm and Yarn
Jun 8, 2020, 11:01 AM
P
Alright, thanks for confirming and testing with Yarn as well. I’ll try to reproduce and see if we have a workaround 🙂
Jun 8, 2020, 11:01 AM
L
Thank you (Removed Name) 🙂
Jun 8, 2020, 11:08 AM
L
Do you want me to send you the code?
Jun 8, 2020, 11:09 AM
P
No need, thanks - was able to reproduce. We’re looking it 🙂
Jun 8, 2020, 11:33 AM
L
Awesome 🙂
Jun 8, 2020, 11:33 AM
L
Thanks for the quick responses
Jun 8, 2020, 11:33 AM
P
Could you say a bit more about your use case for ReactDOMServer? Maybe there’s a workaround while this is not yet resolved?
Jun 8, 2020, 11:42 AM
L
I’m currently exporting icon names using icon-picker in Sanity, but this means i have to load all SVG icons on my website, which causes bloat.
Therefore i want it to just send the SVG code of the react-icon instead of just the name
Therefore i want it to just send the SVG code of the react-icon instead of just the name
Jun 8, 2020, 11:43 AM
L
this:
Jun 8, 2020, 11:44 AM
P
We’ll work on resolving the issue, but to have a workaround for now, I just managed to get this to function with a relative path instead:
[You’d have to adapt this a bit depending on where you’re importing from]
import ReactDOMServer from '../node_modules/react-dom/server'
Jun 8, 2020, 12:07 PM
L
Thank you, i will try that 🙂
Jun 8, 2020, 12:21 PM
L
I’m trying to do this:
And loading like this in schema:
But getting this error:
{iconsArray.icons.map((icon) => { const Icon = ReactIcons[icon]; const rendered = ReactDOMServer.renderToString(Icon()); return ( <> <span className="btn" onClick={() => clearSearch()}> <button style={{ background: "transparent", borderRadius: "0.1rem", margin: "0.1rem", }} onClick={() => onChange(createPatchFrom(rendered)) } > <Icon /> </button> </span> </> ); })}
{ title: "Velg et hovedikon", description: "Søk etter ikoner:", name: "icon", type: "string", inputComponent: IconPicker, },
Jun 8, 2020, 12:36 PM
L
I get it to render the SVG code as a string in Sanity, but i cannot manage to get it to set it as a value:
Jun 8, 2020, 12:48 PM
P
What does your
createPatchFromlook like for the custom input component?
Jun 8, 2020, 12:51 PM
P
(or the entire
IconPickercomponent)
Jun 8, 2020, 12:51 PM
L
// The patch function that sets data on the document const createPatchFrom = (value) => PatchEvent.from(value === "" ? unset() : set(String(value)));
Jun 8, 2020, 12:52 PM
L
Entire code looks like this:
Jun 8, 2020, 1:03 PM
L
import React from "react"; import ReactDOMServer from "../../node_modules/react-dom/server"; import PatchEvent, { set, unset } from "part:@sanity/form-builder/patch-event"; import FormField from "part:@sanity/components/formfields/default"; import * as ai from "react-icons/ai"; import * as bs from "react-icons/bs"; import * as di from "react-icons/di"; import * as fa from "react-icons/fa"; import * as fc from "react-icons/fc"; import * as fi from "react-icons/fi"; import * as gi from "react-icons/gi"; import * as go from "react-icons/go"; import * as gr from "react-icons/gr"; import * as io from "react-icons/io"; import * as md from "react-icons/md"; import * as ri from "react-icons/ri"; import * as ti from "react-icons/ti"; import * as wi from "react-icons/wi"; const ReactIcons = { ...ai, ...bs, ...di, ...fa, ...fc, ...fi, ...gi, ...go, ...gr, ...io, ...md, ...ri, ...ti, ...wi, }; // The patch function that sets data on the document const createPatchFrom = (value) => PatchEvent.from(value === "" ? unset() : set(String(value))); const IconPicker = (props) => { const { type, value = undefined, onChange } = props; const [iconsArray, setIconsArray] = React.useState({ icons: [] }); const [pickerValue, setPickerValue] = React.useState([]); const [searchQuery, setSearchQuery] = React.useState(); const handleChange = (query) => { setPickerValue(query); const arr = []; Object.keys(ReactIcons).map((go) => { if ( go.toLowerCase().includes(query.toLowerCase()) && query.length > 2 ) { arr.push(go); setIconsArray({ icons: arr }); } }); }; // If deleting searchquery, clear icons React.useEffect(() => { if (pickerValue.length < 2) { setIconsArray({ icons: [] }); } }, [pickerValue]); // On icon select const getIcon = (icon) => { const Icon = ReactIcons[icon]; return ( <> <Icon /> </> ); }; const clearSearch = () => { setPickerValue([]); setSearchQuery(""); }; return ( <> <FormField label={type.title} description={ value !== undefined ? "Valgt ikon: " : type.description } > {value !== undefined ? ( <div style={{ position: "relative" }}> <p style={{ fontSize: "2rem" }}> {getIcon(value)} <b>{value}</b> </p> <button style={{ fontSize: "1.5rem", position: "absolute", top: 0, right: 0, background: "transparent", color: "red", border: 0, }} onClick={() => onChange(createPatchFrom(""))} > {getIcon("AiFillCloseCircle")} </button> </div> ) : ( "" )} <input type="text" onChange={(event) => handleChange(event.target.value)} className="DefaultTextInput_input_2wVzD text-input_textInput_31n9_ text-input_root_1xAqy" /> {iconsArray.icons.map((icon) => { const Icon = ReactIcons[icon]; const iconRendered = ReactDOMServer.renderToString(Icon()); return ( <> <span className="btn" onClick={() => clearSearch()}> <button style={{ background: "transparent", borderRadius: "0.1rem", margin: "0.1rem", }} // Working: // onClick={() => // onChange(createPatchFrom(icon)) // } // Not working onClick={() => onChange(createPatchFrom(iconRendered)) } > <Icon /> </button> </span> </> ); })} </FormField> </> ); }; export default IconPicker;
Jun 8, 2020, 1:03 PM
L
Any idea what causes this?
Jun 8, 2020, 1:49 PM
P
createPatchFromexpects a
Stringvalue in this case, which it is likely not receiving from where
createPatchFromis called - with
iconyes, but with
iconRenderedno.
This might be surprising, given that you use
renderToString()to generate
iconRendered. Could you try to console log the type of
iconRenderedto confirm it’s actually a string?
Jun 8, 2020, 2:09 PM
L
It says string
Jun 8, 2020, 2:11 PM
L
Do you think patchEvent gets the value before it is rendered to string somehow?
Jun 8, 2020, 2:25 PM
L
Would it be possible to use a promise or something?
Jun 8, 2020, 2:29 PM
P
What happens if you change the patch function to:
Also, what value is logged here, simply
const createPatchFrom = value => PatchEvent.from(value === '' ? unset() : set(value))
undefined?
Jun 8, 2020, 2:46 PM
L
i will check
Jun 8, 2020, 2:48 PM
L
same error
Jun 8, 2020, 2:49 PM
P
And if you change
In this earlier reply, if you change the icon to a different one, does it successfully update the SVG output directly?
https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1591620484416700?thread_ts=1591608917.403700&cid=C9Z7RC3V1
renderedIconto
iconit still works with the original patch function?
In this earlier reply, if you change the icon to a different one, does it successfully update the SVG output directly?
https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1591620484416700?thread_ts=1591608917.403700&cid=C9Z7RC3V1
Jun 8, 2020, 2:54 PM
L
I can output it as string and JSX underneath the search as you can see
Jun 8, 2020, 2:58 PM
P
Could you DM me a zip of your schema file for this plus the custom input component? Happy to have a look later today if I can spot anything 🙂
Jun 8, 2020, 3:00 PM
L
Of course 🙂 Incredible service
Jun 8, 2020, 3:01 PM
P
(Resolved in DM - rendering issue with
getIcon()in the end, patch was perfect!) 🚀
Jun 8, 2020, 3:56 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.