relay-compiler doesn't work with core-js 3 installed

Created on 1 Apr 2019  路  11Comments  路  Source: facebook/relay

Relay compiler internally seems to rely on the presence of core-js 2, but this dependency is not explicit and so breaks if it finds core-js 3. I have core-js@3 installed in my app root alongside relay-compiler (tested with v2 and v3). Running relay-compiler fails with this stack:

Error: Cannot find module 'core-js/es6' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15) at Function.Module._load (internal/modules/cjs/loader.js:507:25) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (./node_modules/relay-compiler/bin/relay-compiler:16721:18) at __webpack_require__ (./node_modules/relay-compiler/bin/relay-compiler:30:30)

If i switch my version of core-js to 2 it works.

This could probably be fixed by making the dependency explicit.

Most helpful comment

We have a workaround in a project that's using yarn workspaces. We solved it by using yarn workspace's nohoist option. It allows to keep core-js@3 for workspaces that require it and core-js@2 for the package that uses relay-compiler.

In our case, we have a monorepo with multiple projects using yarn workspaces. We have a workspace that uses create-react-app that has relay-compiler as a dependency and another workspace with storybook which requires core-js@3.

This setup caused the error described in https://github.com/facebook/relay/issues/2701#issue-427469247

To fix our problem, we configured our workspaces with the following settings.

  "workspaces": {
    "packages": ["packages/*"],
    "nohoist": ["**/core-js", "**/core-js/**", "**/relay-compiler"]
  },

"nohoist": ["**/core-js", "**/core-js/**"] - ensures that core-js remains part of their respective workspaces.

"nohoist": ["**/relay-compiler"] - ensures that relay-compiler remains in the workspace that uses it which allows it to use core-js@2.

I hope this helps someone who encounters a similar issue.

All 11 comments

I had the same issue, I was able to fix it by changing import paths on the 'node_modules/relay-compiler/bin/relay-compiler.js' on lines 14659 through 14707. I ran it with no problems by changing 'core-js/fn' to 'core-js/es'.

it didnt work also after changing to 'core-js/es'

Same here, do we have some solution?

import graphql from 'babel-relay-plugin/macro' instead of importing it from 'react-relay' if you are developing react-app using create-react-app

If monkey patching the imports didn't work, I would recommend what @Scimonster said and use core-js@2.

We had issues with core-js module not found, happened when we upgraded storybook to 5.1.x (to fix storybook's own issues with core-js in 5.0.11) and again when I upgraded some babel packages to the latest (7.5/7.2) versions.

I couldn't figure out a way to have both core-js versions installed (2.x and 3.x) and the monkey patching suggested above didn't work either, so our solution was to create a new app repository with only the relay packages installed. Then in our package.json write a command to point to our actual app repository

"scripts": {
        "relay": "relay-compiler --src ../app/src/ --schema ../app/public.graphql
},

It works for now until Relay is fixed!

Worth checking your package-lock.json for any dependencies between relay-compiler and 'core-js' as well.
Removed the package-lock and npm installed and now all working normally on my end with core-js 3 at the top level.

Under relay-compiler dependencies we have the below

"core-js": {
          "version": "2.6.9",
          "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.9.tgz",
          "integrity": "sha512",
          "dev": true
        },

We have a workaround in a project that's using yarn workspaces. We solved it by using yarn workspace's nohoist option. It allows to keep core-js@3 for workspaces that require it and core-js@2 for the package that uses relay-compiler.

In our case, we have a monorepo with multiple projects using yarn workspaces. We have a workspace that uses create-react-app that has relay-compiler as a dependency and another workspace with storybook which requires core-js@3.

This setup caused the error described in https://github.com/facebook/relay/issues/2701#issue-427469247

To fix our problem, we configured our workspaces with the following settings.

  "workspaces": {
    "packages": ["packages/*"],
    "nohoist": ["**/core-js", "**/core-js/**", "**/relay-compiler"]
  },

"nohoist": ["**/core-js", "**/core-js/**"] - ensures that core-js remains part of their respective workspaces.

"nohoist": ["**/relay-compiler"] - ensures that relay-compiler remains in the workspace that uses it which allows it to use core-js@2.

I hope this helps someone who encounters a similar issue.

For non-workspace projects, the workaround is even easier:

  "resolutions": {
    "relay-compiler/core-js": "2.6.9"
  }

BTW, this appears to be fixed on master. Would it be possible to get a point release so we can upgrade?

Was this page helpful?
0 / 5 - 0 ratings