create-react-app + gatsby packages in monorepo won't both run
Asked Answered
P

0

10

I'm trying to create a monorepo with these packages:

  • a web app created using create-react-app
  • a gatsby site
  • a library of components etc. that the web app and gatsby app will share (which is my main reason for using a monorepo).

So far I have been unable to set it up so that both the react app and the gatsby site will run.

To show the problem I've set up a minimal monorepo, with just these packages:

  • cra created using create-react-app
  • gat created with gatsby new gat https://github.com/gatsbyjs/gatsby-starter-default

After the initial setup (with no nohoisting), starting the gatsby site returned this:

 ERROR

Error in "/Users/angus/coding/monorepo-trials/cra-gatsby-monorepo/node_modules/gatsby-plugin-gatsby-cloud/gatsby-node.js": Cannot read property 'RawSource' of undefined



  TypeError: Cannot read property 'RawSource' of undefined
  
  - WebpackAssetsManifest.js:17 Object.<anonymous>
    [cra-gatsby-monorepo]/[webpack-assets-manifest]/src/WebpackAssetsManifest.js:17:47
  
  - v8-compile-cache.js:192 Module._compile
    [cra-gatsby-monorepo]/[v8-compile-cache]/v8-compile-cache.js:192:30
  
  - loader.js:1092 Object.Module._extensions..js
    internal/modules/cjs/loader.js:1092:10
  
  - loader.js:928 Module.load
    internal/modules/cjs/loader.js:928:32
  
  - loader.js:769 Function.Module._load
    internal/modules/cjs/loader.js:769:14
  
  - loader.js:952 Module.require
    internal/modules/cjs/loader.js:952:19
  
  - v8-compile-cache.js:159 require
    [cra-gatsby-monorepo]/[v8-compile-cache]/v8-compile-cache.js:159:20
  
  - gatsby-node.js:5 Object.<anonymous>
    [cra-gatsby-monorepo]/[gatsby-plugin-gatsby-cloud]/gatsby-node.js:5:53
  
  - v8-compile-cache.js:192 Module._compile
    [cra-gatsby-monorepo]/[v8-compile-cache]/v8-compile-cache.js:192:30
  
  - loader.js:1092 Object.Module._extensions..js
    internal/modules/cjs/loader.js:1092:10
  
  - loader.js:928 Module.load
    internal/modules/cjs/loader.js:928:32
  
  - loader.js:769 Function.Module._load
    internal/modules/cjs/loader.js:769:14
  
  - loader.js:952 Module.require
    internal/modules/cjs/loader.js:952:19
  
  - v8-compile-cache.js:159 require
    [cra-gatsby-monorepo]/[v8-compile-cache]/v8-compile-cache.js:159:20
  
  - resolve-module-exports.ts:197 resolveModuleExports
    [cra-gatsby-monorepo]/[gatsby]/src/bootstrap/resolve-module-exports.ts:197:26
  
  - validate.ts:349 
    [cra-gatsby-monorepo]/[gatsby]/src/bootstrap/load-plugins/validate.ts:349:31
  

not finished load plugins - 0.930s

npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] develop: `gatsby develop` npm ERR! Exit status 1 npm ERR!  npm ERR! Failed at the [email protected] develop 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!     /Users/angus/.npm/_logs/2021-05-16T14_05_14_891Z-debug.log error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So then I tried nohoisting everything from each of the packages, adding the following to the package.json of each package:

  "workspaces": {
    "nohoist": [
      "**"
    ]
  },

...And then at the top-level doing lerna clean and lerna bootstrap.

At that point, the gatsby site fails with different errors:

$ yarn start
yarn run v1.22.10
$ yarn develop
$ gatsby develop
success open and validate gatsby-configs - 0.081s
success load plugins - 0.912s
success onPreInit - 0.041s
success initialize cache - 0.008s
success copy gatsby files - 0.135s
success onPreBootstrap - 0.020s
success createSchemaCustomization - 0.006s
success Checking for changed pages - 0.004s
success source and transform nodes - 0.073s
success building schema - 0.571s
success createPages - 0.001s
success createPagesStatefully - 0.089s
info Total nodes: 43, SitePage nodes: 6 (use --verbose for breakdown)
success Checking for changed pages - 0.001s
success update schema - 0.080s
success write out redirect data - 0.002s
success Build manifest and related icons - 0.073s
success onPostBootstrap - 0.079s
info bootstrap finished - 5.402s
success onPreExtractQueries - 0.001s
success extract queries from components - 1.843s
success write out requires - 0.007s
success run page queries - 0.019s - 1/1 53.60/s

 ERROR 

(node:9884) [DEP_WEBPACK_MAIN_TEMPLATE_OUTPUT_OPTIONS] DeprecationWarning: MainTemplate.outputOptions is deprecated (use Compilation.outputOptions
instead)
(Use `node --trace-deprecation ...` to show where the warning was created)


 ERROR 

(node:9884) [DEP_WEBPACK_MAIN_TEMPLATE_REQUIRE] DeprecationWarning: MainTemplate.hooks.require is deprecated (use
JavascriptModulesPlugin.getCompilationHooks().renderRequire instead)


 ERROR 

(node:9884) [DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK] DeprecationWarning: Compilation.hooks.normalModuleLoader was moved to
NormalModule.getCompilationHooks(compilation).loader


 ERROR #98124  WEBPACK

Generating development JavaScript bundle failed

undefined

If you're trying to use a package make sure that 'undefined' is installed. If you're trying to use a local file make sure that the path is correct.


 ERROR #98124  WEBPACK

Generating development JavaScript bundle failed

undefined

If you're trying to use a package make sure that 'undefined' is installed. If you're trying to use a local file make sure that the path is correct.


 ERROR #98124  WEBPACK

Generating development JavaScript bundle failed

undefined

If you're trying to use a package make sure that 'undefined' is installed. If you're trying to use a local file make sure that the path is correct.


 ERROR #98124  WEBPACK

Generating development JavaScript bundle failed

undefined

If you're trying to use a package make sure that 'undefined' is installed. If you're trying to use a local file make sure that the path is correct.


 ERROR #98124  WEBPACK

Generating development JavaScript bundle failed

undefined

If you're trying to use a package make sure that 'undefined' is installed. If you're trying to use a local file make sure that the path is correct.


 ERROR #98124  WEBPACK

Generating development JavaScript bundle failed

undefined

If you're trying to use a package make sure that 'undefined' is installed. If you're trying to use a local file make sure that the path is correct.

failed Building development bundle - 3.780s
ERROR in commons
Module not found: TypeError: Cannot read property 'indexOf' of undefined

ERROR in commons
Module not found: TypeError: Cannot read property 'indexOf' of undefined

ERROR in commons
Module not found: TypeError: Cannot read property 'indexOf' of undefined

ERROR in polyfill
Module not found: TypeError: Cannot read property 'indexOf' of undefined

ERROR in polyfill
Module not found: TypeError: Cannot read property 'indexOf' of undefined

ERROR in polyfill
Module not found: TypeError: Cannot read property 'indexOf' of undefined

webpack compiled with 6 errors

..although cra runs.

By chance I discovered that if I then additionally install gatsby at the top-level of the monorepo using yarn add gatsby -W then the gatsby site runs, but the cra package fails as follows:

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/angus/coding/monorepo-trials/cra-gatsby-monorepo/node_modules/babel-loader (version: 8.2.2) 

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/angus/coding/monorepo-trials/cra-gatsby-monorepo/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!

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So it seems to me that gatsby and react-scripts are somehow confusing their required versions of webpack..? I would have thought that nohoisting everything should mean that each package would run entirely self-contained, i.e. as if they were individual 'normal' (i.e. not mono-) repos - but this does not appear to be the case..?

I am a noob in webpack and monorepos in general, so probably I am misunderstanding something...? Any insight very much appreciated!

Some versions:

yarn           1.22.10
lerna          4.0.0
gatsby         3.5.0
react-scripts  4.0.3
react          17.0.2
Pugnacious answered 16/5, 2021 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.