关于react.js:reactapprewired问题导致react项目无法启动

52次阅读

共计 2613 个字符,预计需要花费 7 分钟才能阅读完成。

原来运行得好好的 react 我的项目,忽然运行不胜利了,提醒如下谬误

$ npm start

> react-app-rewired start

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

Error: Cannot find module 'D:\my_project\node_modules\react-scripts/config/webpack.config.dev.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:587:15)
    at Function.Module._load (internal/modules/cjs/loader.js:513:25)
    at Module.require (internal/modules/cjs/loader.js:643:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (D:\my_project\node_modules\react-app-rewired\scripts\start.js:18:23)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my_project@0.1.0 start: `react-app-rewired start --scripts-version react-scripts-ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my_project@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\...\AppData\Roaming\npm-cache\_logs\2019-01-19T10_56_58_751Z-debug.log

通过四处寻找,在 github 找到了起因解释,只不过他的解释是针对应用了 ts 下的 react 我的项目,然而原理雷同内容如下

This has been caused by recent changes to CRA (2.1.2) where they merged the webpack.config.dev.js and webpack.config.prod.js into a single file webpack.config.js. See #343 and #345 for more details on the change.

The adjustment to paths made in react-app-rewired in order to continue to be able to be used with CRA looks for a react-scripts version number greater than or equal to 2.1.2. The react-scripts-ts version number has been higher than that for the last ~18 months, so react-app-rewired is treating it according to the merged webpack config instead of the split one.

To solve it, you’ll need to install an older version of react-app-rewired that doesn’t have this change. I believe the last version of react-app-rewired that will be compatible with react-scripts-ts is version 1.6.2. The versions for 2.x have the breaking change to support CRA 2.1.2 and beyond that is misfiring on react-scripts-ts.

Use yarn add react-app-rewired@1.6.2 or npm install react-app-rewired@1.6.2 to step back to the last of the 1.x versions of react-app-rewired – it will install the older version and lock the version number to exactly that version so that it doesn’t get upgraded to a newer version accidentally in future.

大抵意思就是 react-app-rewired 这个插件降级导致了不会独自生成 dev 和 prod 配置文件了,所以导致文件找不到。须要将 react-app-rewired 版本固定在 1.6.2,这样解决能解决下面的问题,

然而又呈现了一个新问题,大抵内容是 can not find module react_script/package.json

剖析应该也是版本问题导致的,所以通过 git 版本回退找到了以前的代码的 package.json 中 react_script 的应用版本是 2.0.3,所以将版本也固定在了 2.0.3

删掉 node_modules 目录,从新执行 npm install

npm start 启动我的项目,胜利启动并关上浏览器 tab 页。

正文完
 0