简体   繁体   中英

create-react-app integrate with storybook 6.1.6 and build has error

create-react-app version

react: v17.0.1
react-scripts: v4.0.1 

storybook version

@storybook/react: v6.1.6
@storybook/addon-docs: v6.1.6
@storybook/core: v6.1.6

and i could run yarn start to run react app and could run start-storybook -p 9009 -s public to start storybook.

when it comes to build react app, it comes issue. look below.

  1. when run "yarn run build", it actually run react-app-rewired start .
  2. it shows below
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!

I run npm ls babel-loader and see below show

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 
  1. and i follow the instruction and run yarn add babel-loader@8.1.0 . when it done i get below:
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 
  1. then run yarn run build , which actually run react-app-rewired build and get below error
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)

can not build

so then i target to the code node_modules/source-map/lib/source-node.js

and see the error code:

/node_modules/source-map/lib/source-node.js

64 var remainingLines = aGeneratedCode.split(REGEX_NEWLINE);

I run and analysis the code. the the aGeneratedCode will be "obejct", so i change the code as below.

    var remainingLines = typeof(aGeneratedCode) === "string" ? aGeneratedCode.split(REGEX_NEWLINE) : [];

then i could run build / start of the cra app and storybook now.

but why and how to permantly fix it?

I was able to work it around with "resolutions" property ( https://classic.yarnpkg.com/en/docs/package-json/#toc-resolutions )

"resolutions": {
  "babel-loader": "8.1.0"
 },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM