CRA从4升级到5,必须将package.json移至src/,现在CRA找不到index.js

问题描述 投票:0回答:1

我一直在尝试将应用程序从 Create-React-App v4 更新到 v5。

由于新的安全限制,我不得不将

package.json
和关联的
node_modules
文件夹移动到我的
src/
文件夹中的
myProject
文件夹中(由于 Webpack 中的新安全性,它不允许相对导入,看起来).

但现在的问题是,当我启动 React-Scripts(或 Craco)时,Webpack 似乎正在寻找

index.js
作为
myProject/src/src/index.js
,而不是在当前文件夹中找到它(
myProject/src/index.js
)。

这是一条错误消息,导致我将

package.json
(因此
node_modules
)移至
myProject/src
,而不是将其留在
myProject
中,即 Create-React-App v4 中的位置:

ERROR in ./src/views/HomePage/HomePage.js 6:0-53
Module not found: Error: You attempted to import /Users/moryl/Projects/fp-directory/node_modules/react which falls outside of the project src/ directory. 
Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

我没有看到任何可以配置应用程序来查找

index.js
的地方。因此,这是我得到的错误:

/usr/local/bin/yarn run start
yarn run v1.22.15
$ craco start
Could not find a required file.
  Name: index.js
  Searched in: /Users/moryl/Projects/fp-directory/src/src
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

为了证明Craco本身不是问题,我添加了一个命令来使用react-scripts而不是Craco启动:

/usr/local/bin/yarn run startcra
yarn run v1.22.15
$ react-scripts start
Could not find a required file.
  Name: index.js
  Searched in: /Users/moryl/Projects/fp-directory/src/src
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

如果找不到解决方案,我可能会尝试使用最新的 CRA 从头开始创建一个新项目,然后将我的应用程序移入其中。我猜这很简单,但所有 StackOverflow 帖子都讨论了消除限制,而不是迁移到新的、更安全的做事方式。我认为这是一个值得解决的问题。与我的不同,大型应用程序更难以简单地迁移到新创建的 CRA 应用程序。

webpack create-react-app webpack-dev-server craco
1个回答
0
投票

看起来在尝试重组文件夹结构时,您已将 package.json 移至 src 文件夹内,这并不理想。如果您将 package.json 从 src 文件夹中取出,它应该已修复。

© www.soinside.com 2019 - 2024. All rights reserved.