create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Cannot create build: TypeError: Ajv is not a constructor

Open vishnusajev opened this issue 2 years ago • 8 comments

Steps To Reproduce

  1. Update to latest react-scripts (5.0.0)
  2. Do npm run build.

The current behavior

home/node/frontend/node_modules/mini-css-extract-plugin/node_modules/schema-utils/dist/validate.js:66
const ajv = new Ajv({
            ^
TypeError: Ajv is not a constructor
    at Object.<anonymous> (/home/node/frontend/node_modules/mini-css-extract-plugin/node_modules/schema-utils/dist/validate.js:66:13)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:11[55](https://firelight.smokescreen.io/v4/frontend/-/jobs/95213#L55):10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/node/frontend/node_modules/mini-css-extract-plugin/node_modules/schema-utils/dist/index.js:6:5)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)

vishnusajev avatar Mar 10 '22 11:03 vishnusajev

Same here...

FredChauviere avatar Mar 10 '22 13:03 FredChauviere

Same problem.

pedrorogger avatar Mar 11 '22 12:03 pedrorogger

Same issue here. Sometimes it works and sometimes it doesn't.

Also happens on npm run start for me

pgrones avatar Mar 30 '22 13:03 pgrones

Same

Resolved by deleting the node_modules folder, npm install and restarting VS Code

sergei-filitov avatar Apr 16 '22 14:04 sergei-filitov

Same here, any update for this issue?

kee0624 avatar Apr 27 '22 01:04 kee0624

I got this fixed after uninstalling npm-force-resolutions and removing all preinstall related to that. And then

  • rm -rf node_modules
  • rm package-lock.json
  • npm install

vishnusajev avatar Apr 28 '22 06:04 vishnusajev

I am having a similar problem. I believe that this error occurs when particular libraries are imported in a create-react-app project.

I did some debugging and found that in some cases, if the library's code (in node_modules) contains require(<<SOME MODULE NAME>>), this require call incorrectly(?) returns a string. In my case, my library code contains require("three") which strangely returns the string "/static/media/three.033a3ceb8fc583972a98.cjs", when it should return a module object with the functions and values inside the three library.

It seems that create-react-app 5's webpack configuration is not correctly set up to handle node_module code that contains require(<<SOME MODULE NAME>>) for some modules. I am not yet sure why. I would like to find a workaround or fix.


I found a very hacky workaround: replace instances of require(<<SOME MODULE NAME>>) that cause issues in library code with relative requires, i.e. require(../../../three).

This is a pretty bad solution because it involves manually changing code in node_modules.

I tried this workaround because I found that the only place that require is used in the node_modules in a fresh CRA project is in react/index.js and is a relative require.


By ejecting and writing a custom webpack loader I determined that this is occurring because the libraries being loaded ("three" in my case) provides a .cjs file which falls into the type: "asset/resource" loader at the bottom of the webpack config loader. I verified that this fix works for me https://github.com/vezwork/create-react-app/compare/main...vezwork:create-react-app:fix-cjs-loading

vezwork avatar Sep 07 '22 14:09 vezwork

I have tried the fix https://github.com/vezwork/create-react-app/compare/main...vezwork:create-react-app:fix-cjs-loading for my similar issue as well. Will this fix be merged soon. Thanks.

sucy6330133 avatar Oct 03 '22 19:10 sucy6330133

I got this fixed after uninstalling npm-force-resolutions and removing all preinstall related to that. And then

  • rm -rf node_modules
  • rm package-lock.json
  • npm install

I followed through with the earlier comment and the npm install worked fine as well. Still don't know what I was doing. But its cause I was moving folders around and really I was down the wrong rabbit hole. Turns out I just needed to install a few packages my team member did

Abel3047 avatar Oct 22 '22 14:10 Abel3047

I got this fixed after uninstalling npm-force-resolutions and removing all preinstall related to that. And then

  • rm -rf node_modules
  • rm package-lock.json
  • npm install

This is the only solution that's working for me now. It would be better if someone can point to a nicer solution. As currently whenever I add a new package, I have to repeat the above steps.

kirandash avatar Nov 03 '22 13:11 kirandash

Same issue.

clovus avatar Nov 29 '22 18:11 clovus

+1 same issue

ManhNV103 avatar Dec 06 '22 00:12 ManhNV103

This is very painful, every time you add a new dependency you have to rm -rf node_modules and install it again.

Any updates on this are very much appreciated 🙏

ferdelamad avatar Feb 10 '23 00:02 ferdelamad

Until this gets fixed we can use a shell alias:

alias fix.ajv='rm -rf node_modules && rm package-lock.json && npm i && git restore package-lock.json'

[$]> fix.ajv && npm start

Gerst20051 avatar Mar 18 '23 10:03 Gerst20051

Not entirely sure if this helps, because I don't have preinstall hooks from npm-force-resolutions, but I seemed to solve the issue by adding "mini-css-extract-plugin": "^2.7.5" to the build. A previous SO post suggested that this worked, but I sense it didn't for react-scripts 5.0.1.

Also, despite not having the preinstall hooks, I did still get this error until I upgraded the module. You might also have to remove package-lock.json and start fresh (as well as removing node_modules). I ran npm install followed by npm start several times to ensure that the app ran appropriately.

nga-27 avatar Mar 21 '23 14:03 nga-27