2

Dont use the guide I linked, it is old- use the one in the answer instead

I am attempting to switch from react to preact by following their guide. I change webpack.config.js alias to add:

  alias: {
    "react": "preact-compat",
    "react-dom": "preact-compat",
    .....

However when I run npm run build I keep getting:

Failed to compile.

.\src\index.tsx
Cannot find module: 'react/jsx-runtime'. Make sure this package is installed.

I have tried:

When I comment out the alias changes in the preact guide everything works fine. Happy to include more info, I'm not sure whats relevant however.

Heres the verbose error log from npm run build:

13 verbose stack Error: website@0.2.5 build: `node scripts/build.js`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:315:20)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:315:20)
13 verbose stack     at maybeClose (internal/child_process.js:1048:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
Rilcon42
  • 9,584
  • 18
  • 83
  • 167

1 Answers1

3

It looks like you were referring to a dated documentation page. Here is the latest one.

Your should use preact/compat instead:

alias: {
  "react": "preact/compat",
  "react-dom": "preact/compat",
  ...
Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
  • 2
    Just to add, `preact-compat` is our legacy compat package, only for v8. v10 we switched to including this in the main library itself specifically so users don't need to go looking for others. – rschristian Apr 07 '21 at 21:50
  • The documentation now mentions a third entry: `"react/jsx-runtime": "preact/jsx-runtime"` – Matthias Jun 20 '22 at 21:54
  • This worked on preact 10 for me, I only changed "-" to "/" (ex.: preact-compat to preact/compat). – Ëuclïdës Ďŕÿ'C Mar 02 '23 at 14:45