WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.55k stars 4.22k forks source link

@wordpress/scripts error "Cannot find module 'ajv/dist/compile/codegen'" #62801

Open justinbachtell opened 5 months ago

justinbachtell commented 5 months ago

Description

When running npm run dev or npm run build in my repo, I am getting the following error: [0] [webpack-cli] Failed to load 'C:\Users\user\repository\node_modules\@wordpress\scripts\config\webpack.config.js' config [0] [webpack-cli] Error: Cannot find module 'ajv/dist/compile/codegen'

If I run npm install ajv the issue is fixed, but there seems to be an issue with the ajv dependency within @wordpress/scripts, which I would prefer to resolve over installing the package as a package.json dependency.

Step-by-step reproduction instructions

  1. When running npm run dev or npm run build an error is thrown:

    Cannot find module 'ajv/dist/compile/codegen'

  2. Update @wordpress/scripts using npm install @wordpress/scripts@latest.

  3. Run npm run dev or npm run build and the same error is thrown.

  4. Delete /node_modules and package-lock.json, and then run npm install.

  5. Run npm run dev or npm run build and the same error is thrown.

Screenshots, screen recording, code snippet

$npm run dev

repository@1.0.0 dev concurrently "wp-scripts start --webpack-copy-php" "grunt watch --force" "npm run sass-watch"`

[1] Running "watch" task [1] Waiting... [2] [2] > repository@1.0.0 sass-watch [2] > sass -w --style=compressed src/styles:public/min/css/ [2] [0] [webpack-cli] Failed to load 'C:\Users\user\repository\node_modules\@wordpress\scripts\config\webpack.config.js' config [0] [webpack-cli] Error: Cannot find module 'ajv/dist/compile/codegen' [0] Require stack: [0] - C:\Users\user\repository\node_modules\ajv-keywords\dist\definitions\typeof.js [0] - C:\Users\user\repository\node_modules\ajv-keywords\dist\keywords\typeof.js [0] - C:\Users\user\repository\node_modules\ajv-keywords\dist\keywords\index.js [0] - C:\Users\user\repository\node_modules\ajv-keywords\dist\index.js [0] - C:\Users\user\repository\node_modules\schema-utils\dist\validate.js [0] - C:\Users\user\repository\node_modules\schema-utils\dist\index.js [0] - C:\Users\user\repository\node_modules\copy-webpack-plugin\dist\index.js [0] - C:\Users\user\repository\node_modules\@wordpress\scripts\config\webpack.config.js [0] - C:\Users\user\repository\node_modules\webpack-cli\lib\webpack-cli.js [0] - C:\Users\user\repository\node_modules\webpack-cli\lib\bootstrap.js [0] - C:\Users\user\repository\node_modules\webpack-cli\bin\cli.js
[0] - C:\Users\user\repository\node_modules\webpack\bin\webpack.js
[0] at Module._resolveFilename (node:internal/modules/cjs/loader:995:15) [0] at Module._load (node:internal/modules/cjs/loader:841:27) [0] at Module.require (node:internal/modules/cjs/loader:1061:19) [0] at require (node:internal/modules/cjs/helpers:103:18) [0] at Object. (C:\Users\user\repository\node_modules\ajv-keywords\dist\definitions\typeof.js:3:19) [0] at Module._compile (node:internal/modules/cjs/loader:1159:14) [0] at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) [0] at Module.load (node:internal/modules/cjs/loader:1037:32) [0] at Module._load (node:internal/modules/cjs/loader:878:12) [0] at Module.require (node:internal/modules/cjs/loader:1061:19) { [0] code: 'MODULE_NOT_FOUND', [0] requireStack: [ [0] 'C:\Users\user\repository\node_modules\ajv-keywords\dist\definitions\typeof.js', [0] 'C:\Users\user\repository\node_modules\ajv-keywords\dist\keywords\typeof.js', [0] 'C:\Users\user\repository\node_modules\ajv-keywords\dist\keywords\index.js', [0] 'C:\Users\user\repository\node_modules\ajv-keywords\dist\index.js', [0] 'C:\Users\user\repository\node_modules\schema-utils\dist\validate.js', [0] 'C:\Users\user\repository\node_modules\schema-utils\dist\index.js', [0] 'C:\Users\user\repository\node_modules\copy-webpack-plugin\dist\index.js', [0] 'C:\Users\user\repository\node_modules\@wordpress\scripts\config\webpack.config.js', [0] 'C:\Users\user\repository\node_modules\webpack-cli\lib\webpack-cli.js', [0] 'C:\Users\user\repository\node_modules\webpack-cli\lib\bootstrap.js', [0] 'C:\Users\user\repository\node_modules\webpack-cli\bin\cli.js', [0] 'C:\Users\user\repository\node_modules\webpack\bin\webpack.js' [0] ] [0] } [0] wp-scripts start --webpack-copy-php exited with code 2 [2] [2024-06-24 11:23] Compiled src\styles\global-editor.scss to public\min\css\global-editor.css. [2] [2024-06-24 11:23] Compiled src\styles\global-utility-classes.scss to public\min\css\global-utility-classes.css. [2] [2024-06-24 11:23] Compiled src\styles\globals.scss to public\min\css\globals.css. [2] Sass is watching for changes. Press Ctrl-C to stop.

Environment info

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

walton-alex commented 4 months ago

I'm having the same issue too.

ZachWatkins commented 4 months ago

I'm also having this issue with every version of @wordpress/scripts back to 27.0.0.

Others seem to fix this by adding ajv@latest to their dev dependencies but that's not ideal.

ocean90 commented 1 month ago

I had the same issue with a .npmrc file which set legacy-peer-deps = true and/or prefer-dedupe = true.