Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with yarn workspaces #2

Closed
teevik opened this issue Oct 24, 2018 · 11 comments
Closed

Usage with yarn workspaces #2

teevik opened this issue Oct 24, 2018 · 11 comments
Assignees

Comments

@teevik
Copy link

teevik commented Oct 24, 2018

The error:

$ yarn run dev
yarn run v1.10.1

$ craco start
module.js:549
    throw err;
    ^

Error: Cannot find module 'C:\WebDev\coding-tracker\packages\client\node_modules\react-scripts/config/paths.js'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.resolve (internal/module.js:18:19)
    at resolveConfigFilePath (C:\WebDev\coding-tracker\node_modules\@craco\craco\lib\cra.js:24:20)
    at Object.<anonymous> (C:\WebDev\coding-tracker\node_modules\@craco\craco\lib\cra.js:38:26)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I'm using yarn workspaces for a monorepo, so all the node_modules is shared in a single folder. My app has a structrue like this:

coding-tracker/
├── node_modules/
└── packages/
    ├── api/
    ├── shared/
    └── client/

Now the problem is that craco looks for the react-scripts files in coding-tracker/packages/client/node_modules instead of coding-tracker/node_modules

I'm pretty sure that's because process.cwd() would be coding-tracker/packages/client: https://github.com/sharegate/craco/blob/master/packages/craco/lib/paths.js#L4

I made a quick workaround in my case by doing this:

function getReactScriptsFolderPath() {
-   let filepath = "";
+   let filepath = "react-scripts";

-   if (args.reactScripts.isOverrided) {
-       filepath = path.resolve(`${projectRoot}/${args.reactScripts.value}/`);
-   } else {
-       filepath = path.resolve(`${nodeModulesPath}/react-scripts/`);
-   }

    log("Found react scripts folder at: ", filepath);

    return filepath;
}
@patricklafrance
Copy link
Contributor

Thank you for your bug report and your solution, I will look into this before the end of the week!

@patricklafrance
Copy link
Contributor

patricklafrance commented Oct 26, 2018

@teevik I will add a configuration value to facilitate the usage of workspace but in the meantime what you can do is:

craco start --react-scripts ../../../node_modules/react-scripts

@patricklafrance patricklafrance self-assigned this Oct 26, 2018
@teevik
Copy link
Author

teevik commented Oct 26, 2018

Alright, thank you so much!

@axelchalon
Copy link

Hi! The test command doesn't seem to take the --react-scripts value into account:

craco test --react-scripts ../../node_modules/react-scripts

 FAIL  src/stores/sendStore.spec.js
  ● Test suite failed to run

    Cannot find module '/home/nicolas/www/fether/packages/fether-react/node_modules/react-scripts/config/paths.js'

craco {build,start} --react-scripts ../../node_modules/react-scripts both work fine though!

@iamawebgeek
Copy link

@axelchalon +1. passing --workspace flag did not help as well

@patricklafrance
Copy link
Contributor

I will look into this as soon as possible. Thank you.

@patricklafrance
Copy link
Contributor

patricklafrance commented Feb 26, 2019

@iamawebgeek @axelchalon could you provide a simple repository that reproduce the case?

It works fine with my sandbox project with either the --workspace flag or a custom --react-scripts path

"test": "craco test --verbose --workspace",
"test-react-scripts": "craco test --verbose --react-scripts ../../node_modules/react-scripts-custom"

Thank you

@axelchalon
Copy link

yes, https://github.com/axelchalon/craco-issue

Notice how yarn start works but yarn test doesn't.

Some observations: tests work if there is only one test file, and tests work if I remove babel: {...} from the export of craco.config.js

@patricklafrance
Copy link
Contributor

patricklafrance commented Feb 27, 2019

Thank you @axelchalon

Oh that explains why, my sandbox project contains a single test file.

@patricklafrance
Copy link
Contributor

patricklafrance commented Apr 3, 2019

It took a while but I finally found some time to work on this!

It was not an easy one.

I published an alpha version, if anyone can try it: https://www.npmjs.com/package/@craco/craco/v/4.0.0

The problem was that the --workspace, --scripts-version and --react-scripts options are provided by the CLI and the custom babel-jest transformer provided by craco doesn't have access to those options since the babel-jest transformer is called directly by Jest in another process.

The solution is to define those options in the craco.config.js file.

You can have a look here for the config: https://github.com/sharegate/craco/blob/add-paths-to-config-file/packages/craco/README.md#configuration-overview

Of course there is some drawback...

  • --workspace, --scripts-version and --react-scripts cannot be specified by the CLI anymore.
  • Custom location for the config file (--config) cannot be specified if you need to use craco support for babel-jest

@axelchalon @iamawebgeek

@axelchalon
Copy link

Can confirm craco {test,build,start} all work fine with craco v4.0.0 and specifying reactScriptsPath in craco.config.js 👍 thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants