Aliasing react as preact throws Cannot find module: 'react/jsx-runtime'

Viewed 2864

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)
1 Answers

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",
  ...
Related