create-react-app版本
react: v17.0.1
react-scripts: v4.0.1
故事板版本
@storybook/react: v6.1.6
@storybook/addon-docs: v6.1.6
@storybook/core: v6.1.6
我可以运行yarn start
来启动React应用程序,也可以运行start-storybook -p 9009 -s public
来启动storybook。
当涉及构建React应用程序时,会出现问题。请参见下文:
- 运行"yarn run build"实际上在运行
react-app-rewired start
。 - 它显示如下:
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"babel-loader": "8.1.0"
Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:
/Users/yejinlei/Documents/playground/personal/react-temp/node_modules/babel-loader (version: 8.2.1)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if /Users/yejinlei/Documents/playground/personal/react-temp/node_modules/babel-loader is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls babel-loader in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed babel-loader.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
我运行了
npm ls babel-loader
命令,看到以下输出:react-temp@0.1.0 /Users/yejinlei/Documents/playground/personal/react-temp
├─┬ @storybook/addon-docs@6.1.6
│ └─┬ @storybook/core@6.1.6
│ └── babel-loader@8.2.1 deduped
└── babel-loader@8.2.1
3. 我按照指示运行
yarn add babel-loader@8.1.0
。等待完成后,我看到以下信息:react-temp@0.1.0 /Users/yejinlei/Documents/playground/personal/react-temp
├─┬ @storybook/addon-docs@6.1.6
│ └─┬ @storybook/core@6.1.6
│ └── babel-loader@8.2.1
└── babel-loader@8.1.0
- 然后运行
yarn run build
,实际上运行的是react-app-rewired build
,会出现以下错误
playground/personal/react-temp/node_modules/react-scripts/scripts/build.js:19
throw err;
^
TypeError: aGeneratedCode.split is not a function
at Function.SourceNode_fromStringWithSourceMap [as fromStringWithSourceMap] (playground/personal/react-temp/node_modules/source-map/lib/source-node.js:64:41)
无法构建
然后我将目标转向代码 node_modules/source-map/lib/source-node.js
并查看错误代码:
/node_modules/source-map/lib/source-node.js
64 var remainingLines = aGeneratedCode.split(REGEX_NEWLINE);
我运行并分析了代码。aGeneratedCode将会是"object",因此我更改了代码如下。
var remainingLines = typeof(aGeneratedCode) === "string" ? aGeneratedCode.split(REGEX_NEWLINE) : [];
现在我可以运行CRA应用程序和Storybook的构建/启动。
但是,如何永久修复它?为什么会出现这种情况?