Create-react-app error : internal/modules/cjs/loader.js:311 ,当使用 npx 创建一个反应应用程序时

问题描述

当使用 npx create-react-app 创建一个新的 react 项目时,进程停止并报错:internal/modules/cjs/loader.js:311。

internal/modules/cjs/loader.js:311 抛出错误; ^ 错误:找不到模块“C:\Users\shonm\Desktop\Web Development\WebSites\test1\node_modules\fs-extra\lib\index.js”。请验证 package.json 是否具有有效的“main” 入口

 PS C:\Users\shonm\Desktop\Web Development\WebSites> npx create-react-app test1

Installing packages. This might take a couple of minutes.
Installing react,react-dom,and react-scripts with cra-template...

yarn add v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.13: The platform "win32" is incompatible with this module.
info "fsevents@1.2.13" is an optional dependency and Failed compatibility check. Excluding it from installation.
info fsevents@2.3.2: The platform "win32" is incompatible with this module.
info "fsevents@2.3.2" is an optional dependency and Failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
.......
Done in 30.17s.
**internal/modules/cjs/loader.js:311
      throw err;
      ^
Error: Cannot find module 'C:\Users\shonm\Desktop\Web Development\WebSites\test1\node_modules\fs-extra\lib\index.js'. Please verify that the package.json has a valid "main" 
entry**
    at tryPackage (internal/modules/cjs/loader.js:303:19)
  
  requestPath: 'fs-extra'
}

Aborting installation.
  node  has Failed.

Deleting generated file... node_modules

解决方法

当全局安装 npm 和 yarn 时会发生这种情况。 create-react-app 使用 yarn 来构建。要解决此问题,请使用 npx create-react-app projectname --use-npm,或从全局包中删除 yarn。

同时验证节点安装是否正确,node -v && npm -v