create-react-app 返回错误:执行时找不到module“react-scripts/scripts/init.js”

IT技术 node.js reactjs npm create-react-app yarnpkg
2021-05-23 19:44:16

当我尝试使用 npm 和 yarn 创建react项目时,它显示以下错误:

我尝试通过运行 npm install -g create-react-app@latest 重新安装 node 并确保它是最新的,以及 create-react-app。我还删除了 npm 和 npm-cache 文件夹和环境变量,但对我没有任何作用,我一直收到相同的错误。

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'react-scripts/scripts/init.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at [eval]:2:20
    at Script.runInThisContext (vm.js:122:20)
    at Object.runInThisContext (vm.js:329:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at evalScript (internal/bootstrap/node.js:589:27)

Aborting installation.
  node  has failed.

Deleting generated file... node_modules
Deleting generated file... package.json
Deleting generated file... yarn.lock
Deleting test/ from C:\Users\Esper\projects
Done.

我认为问题是由于操作系统问题将我的 Windows 10 恢复到以前的状态,因为在恢复它之前,当我运行 create-react-app 时,它可以正常工作。

create-react-app --version 3.1.1

节点 -v v10.16.3

npm -v 6.9.0

SO Windows 10 教育版

3个回答

安装 package.json with npm init(press enter until is installed) 然后
安装 react-scripts with npm i react-scripts
然后你可以安装 react with npx create-react-app your-app
将目录更改为“your-app”
再次运行npm i react-scripts,它应该可以工作。

希望我没有错过任何一步,如果有效,请告诉我:)

只需在 git bash 上运行此代码:

npm install
npm install react-scripts
npx create-react-app your-app-name

也许它会帮助别人。

如果您的路径中有一些无效字符,则在使用 create-react-app 后可能会发生此错误。

&从您的路径中删除类似字符

例如,从这里更改:

C:\Users\...\Documents\Section 3 Components & Start\my-app

对此:

C:\Users\...\Documents\Section 3 Components Start\my-app