4

Trying to run npm start within my React app is causing the following:

  Error: Child compilation failed:
  Module.createRequire is not a function
  
  - child-compiler.js:169 childCompiler.runAsChild
    [fit_with_us]/[html-webpack-plugin]/lib/child-compiler.js:169:18
  
  - Compiler.js:551 finalCallback
    [fit_with_us]/[webpack]/lib/Compiler.js:551:5
  
  - Compiler.js:577 compile
    [fit_with_us]/[webpack]/lib/Compiler.js:577:11
  
  - Compiler.js:1196 hooks.afterCompile.callAsync.err
    [fit_with_us]/[webpack]/lib/Compiler.js:1196:17

I've tried deleting my node_modules and package-lock. I've tried deleting my cache. I've tried upgrading my node version to 14 and 16. No luck. Any ideas?

cachius
  • 1,743
  • 1
  • 8
  • 21
lucss
  • 31
  • 1
  • 2
  • What are your dependencies? Did you also try to update them or npm? – cachius May 09 '22 at 22:19
  • Does this answer your question? [ERROR in Module.createRequire is not a function](https://stackoverflow.com/questions/70390777/error-in-module-createrequire-is-not-a-function) – cachius May 10 '22 at 08:32
  • @cachius Turns out my nvm use command didn't stick. I was using node v10 even after I ran the command. I ran it properly, ran the app, and it worked – lucss May 14 '22 at 06:00

2 Answers2

3

Module.createRequire was added in node 12.2.0. This error means code written for node >=12.2.0 is run with node < 12.2.0.

This can be caused unintentionally by a non sticky nvm use command.

cachius
  • 1,743
  • 1
  • 8
  • 21
0

Update the node version on Linux through the steps below, which fixed the issue I also encountered.

Step1:

sudo apt-get install curl

Step2 (the 18.x is the latest version) :

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Step3:

sudo apt-get install nodejs

Step4 (check version):

node -v

Then run npm start that would be fixed.

J7LN
  • 1
  • 2