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

Module not found: Can't resolve '@babel/runtime/helpers/esm/extends' #9746

Closed
HeleneBigSofa opened this issue Mar 22, 2019 · 26 comments
Closed
Assignees
Labels
i: invalid outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@HeleneBigSofa
Copy link

HeleneBigSofa commented Mar 22, 2019

Bug Report

Current Behavior
I created an app within a monorepo (Lerna) using yarn workspace and create-react-app ("react-scripts": "2.0.0-next.3e165448").

The architecture of the app is as follow:

  • node_modules
    - @babel
    - core
    - runtime
    - etc..
    - other modules...
  • package1 (shared components)
    - src
    - package.json
  • package2 (the app consuming the shared components)
    - src
    - package.json
  • package.json

Input Code
Calling
import _extends from '@babel/runtime/helpers/esm/extends';
from package2/index.js (or in any dependencies of package1) results in this error:
Module not found: Can't resolve '@babel/runtime/helpers/esm/extends'

Calling any other modules available under /node_modules works fine, even from @babel/core for instance:
import _default from '@babel/core/lib/tools/build-external-helpers';
works correctly.

Please note, if I replace @babel/runtime/... with the absolute path it would work.

Expected behavior/code
Need to be able to call @babel/runtime/...

Babel Configuration (.babelrc, package.json, cli command)
In the package.json root:

"babel": {
    "presets": [
      [
        "@babel/preset-env"
      ]
    ],
    "plugins": [
      "@babel/plugin-transform-runtime"
    ]
  },

Environment

  • Babel version(s): @babel/runtime 7.4.2, @babel/core 7.4.0
  • Node/npm version: Node v10.15.2
  • OS: OS High Sierra 10.13.6
  • Monorepo: yes, Lerna
@babel-bot
Copy link
Collaborator

Hey @HeleneBigSofa! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

Can you post all your package.jsons?

@HeleneBigSofa
Copy link
Author

HeleneBigSofa commented Mar 22, 2019

Thank you Nicolo to take the time to look at this.

The package.jsons:
In the root

{
  "name": "monorepo",
  "private": true,
  "workspaces": {
    "packages": [
      "package1/",
      "package2/",
      "package2/src/**"
    ],
    "nohoist": [
      "**/eslint",
      "react-scripts",
      "react-scripts/**",
      "**/jest/**"
    ]
  },
  "jest": {
    "testPathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/spa/"
    ],
    "projects": [
      "<rootDir>/playbook/*"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "dependencies": {
    "apollo-boost": "^0.1.16",
    "graphql": "^14.0.2",
    "react-apollo": "^2.2.4"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.2",
    "@babel/core": "^7.1.2",
    "@babel/preset-env": "^7.1.0",
     "@babel/runtime": "^7.4.2",
     "@babel/plugin-transform-runtime": "^7.4.0",
    "babel-eslint": "9",
    "eslint": "^4.17.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-prettier": "^2.6.2",
    "eslint-plugin-react": "^7.6.1",
    "husky": "^1.0.0-rc.13",
    "lerna": "^3.4.3",
    "lint-staged": "^7.2.2",
    "prettier": "^1.14.2",
    "prettier-eslint": "^8.8.1",
    "prettier-eslint-cli": "^4.7.0"
  },
  "lint-staged": {
    "./**/*.{js,jsx}": [
      "yarn eslint",
      "git add"
    ]
  },
  "babel": {
    "presets": [
      [
        "@babel/preset-env"
      ]
    ],
    "plugins": [
      "@babel/plugin-transform-runtime"
    ]
  },
}

In package1:

{
  "name": "package2",
  "version": "1.0.30",
  "private": true,
  "dependencies": {
    "@date-io/moment": "^1.0.2",
    "@material-ui/core": "^3.3.1",
    "@material-ui/icons": "^3.0.1",
    "@material-ui/lab": "^3.0.0-alpha.25",
    "@storybook/react": "^3.4.8",
    "apollo-storybook-react": "^0.1.8",
    "autosuggest-highlight": "3.1.1",
    "axios": "^0.18.0",
    "date-fns": "2.0.0-alpha.27",
    "faker": "^4.1.0",
    "graphql": "^14.0.2",
    "graphql-iso-date": "^3.6.1",
    "graphql-tag": "^2.9.2",
    "graphql-tools": "^3.1.1",
    "jest-dom": "^1.12.0",
    "jest-styled-components": "^6.3.1",
    "lodash.defaults": "^4.2.0",
    "lodash.isequal": "^4.5.0",
    "material-ui": "^0.20.2",
    "material-ui-chip-input": "^1.0.0-beta.14",
    "material-ui-pickers": "^2.2.1",
    "material-ui-slider": "^3.0.0",
    "moment": "^2.24.0",
    "react": "^16.4.1",
    "react-adopt": "^0.6.0",
    "react-autosuggest": "^9.4.3",
    "react-dom": "^16.4.1",
    "react-dropzone": "^5.1.0",
    "react-router": "^5.0.0",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.0.0-next.3e165448",
    "react-testing-library": "^5.0.0",
    "storybook": "^1.0.0",
    "storybook-addon-material-ui": "^0.9.0-alpha.16",
    "storybook-react-router": "^1.0.1",
    "storybook-readme": "4.0.0-beta1",
    "styled-components": "^3.4.5",
    "video.js": "^7.3.0"
  },
  "devDependencies": {
    "@sambego/storybook-state": "^1.3.1",
    "@storybook/addon-a11y": "^3.4.10",
    "@storybook/addon-actions": "^3.4.11",
    "@storybook/addon-info": "^3.4.10",
    "@storybook/addon-jest": "^3.4.10",
    "@storybook/addon-knobs": "^3.4.10",
    "@storybook/addon-links": "^3.4.11",
    "@welldone-software/why-did-you-render": "^2.5.0",
    "apollo-storybook-react": "^0.1.8",
    "axios-mock-adapter": "^1.16.0"
  },
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx}",
      "!/node_modules/",
      "!src/index.js",
      "!src/registerServiceWorker.js",
      "!src/**/stories.{js,jsx}",
      "!src/api/**/*.{js,jsx}"
    ]
  }
}

In package2:

{
  "name": "spa",
  "version": "1.0.30",
  "private": true,
  "dependencies": {
    "apollo-boost": "^0.1.12",
    "graphql": "^0.13.2",
    "react": "^16.4.1",
    "react-apollo": "^2.1.9",
    "react-dom": "^16.4.1",
    "react-scripts": "2.0.0-next.3e165448",
    "source-map-explorer": "^1.6.0"
  },
  "scripts": {
    "start": "PORT=3003 react-scripts start",
    "analyze": "source-map-explorer build/static/js/vendors.*",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "cypress:open": "cypress open",
    "cypress:run": "cypress run"
  },
  "browserslist": {
    "development": [
      "last 2 chrome versions",
      "last 2 firefox versions",
      "last 2 edge versions"
    ],
    "production": [
      ">0.25%",
      "not op_mini all",
      "ie 11"
    ]
  },
  "devDependencies": {
    "cypress": "^3.1.5",
  }
}

@nicolo-ribaudo
Copy link
Member

I think that the problem is that lerna assumes that you aren't using @babel/runtime in package2, because you didn't specify it in its dependencies. For this reason, if any dependency of package2 depends on @babel/runtime, lerna thinks that it can safely hoist that dependency's @babel/runtime to package2/node_modules. If this hoisted version is an old version without the extends helper, it will throw.

I would try adding @babel/runtime to paackage2 (and package1, if needed): even when using a monorepo, every runtime dependency should be listed.

Your package.json also has two other problems:

  1. @babel/runtime is used at runtime (not at build time) so it should be in dependencies, not devDependencies
  2. (maybe this is intentional) The Babel config defined in the root package won't be applied to package1/2. If you want it to be, you should use babel.config.js instead: https://babeljs.io/docs/en/config-files

@HeleneBigSofa
Copy link
Author

Thank you for your reply and solutions Nicolo, well appreciated.
I already tried to install @babel/runtime as a direct dependency of package2, but let me try again, I might have missed something. I also applied a "nohoist" of @babel/runtime in the root package.json but it did not change the issue unfortunately. I'll try point 1 and 2 and come back to you.

@nicolo-ribaudo
Copy link
Member

Point 1 and 2 are just "details", I don't think that they will solve your problem.
What version does package2/node_modules/@babel/runtime/package.json say?

@HeleneBigSofa
Copy link
Author

it says: "version": "7.4.2"

@nicolo-ribaudo
Copy link
Member

And doesn't helpers/esm/extends.js exist inside that package?

@HeleneBigSofa
Copy link
Author

HeleneBigSofa commented Mar 22, 2019

Yes it is definitely there. And if I import it with:

import _extends from '../node_modules/@babel/runtime/helpers/esm/extends';

it's found.

I can import this with no problem for example:
import _default from '@babel/core/lib/tools/build-external-helpers';

@HeleneBigSofa
Copy link
Author

I set up a repo that reproduce the error: https://github.com/htmlene/monorepo

When I first raised the issue, I gave a simplistic example by calling directly @babel/runtime in the package2. The monorepo example is closer to the reality as @babel/runtime is required by a dependency "react-router" (+history/react-router-dom) - I hope it makes sense, let me know if you have any questions.

@nicolo-ribaudo
Copy link
Member

I will check it later today

@nicolo-ribaudo nicolo-ribaudo self-assigned this Mar 22, 2019
@HeleneBigSofa
Copy link
Author

Thank you Nicolo, it's very kind of you

@nicolo-ribaudo
Copy link
Member

I think that this might be a bug with react-scripts (or with the bundler it internally use; I don't know which it is). The Babel package is definetly there (if you go in monorepo/node_modules/history/esm, open a node shell and run require.resolve("@babel/runtime/helpers/esm/extends") it clearly show that that module can be resolved).
I also tried to disable yarn workspaces and use the lerna implementation of workspaces, but it didn't help.

@HeleneBigSofa
Copy link
Author

Thank you Nicolo. Do you think reverting to the previous version of @babel we were on would solve the issue?

@nicolo-ribaudo
Copy link
Member

I'm don't think so, but it might be worth trying

@HeleneBigSofa
Copy link
Author

We have finally managed to solve our issue, using react-app-rewired, customize-cra and a custom config.
Thank your for your help Nicolo!

@yanyu0517
Copy link

We have met the same problem. Was there news about this ?

@nelhu
Copy link

nelhu commented Apr 26, 2019

I have the same problem and did not resolve yet

@fgiarritiello
Copy link

@HeleneBigSofa Do you mind sharing the solution? Thanks

@HeleneBigSofa
Copy link
Author

We just upgraded react-script to version 2.1.8

@fgiarritiello
Copy link

@HeleneBigSofa thank you. Sorry but I have a different question then: how did you manage to get the monorepo to work if react-script dropped its support after the version you were using?

@ivan-kleshnin
Copy link

Same problem with MDX + Next. Tried a few recommended workaround – nothing helps.

@crubier
Copy link

crubier commented Jul 5, 2019

Same problem when building a relatively simple package with rollup... No solution works so far..

@connorpwilliams
Copy link

connorpwilliams commented Jul 11, 2019

Module not found for @babel/runtime/helpers/esm/objectSpread using react-scripts 3.0.1

I assume this is the same root issue and no fix has worked thus far either.

EDIT: Rolling react scripts back to an earlier version (2.1.4 which was what I use in another project) solved the issue for me

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jul 11, 2019

Is the @babel/runtime/helpers/esm/objectSpread file there? If it is, please report this bug to the bundler react-scripts uses.

@gilbertlucas46
Copy link

I fixed mine by

  1. Delete package-lock.json
  2. Delete node-modules
  3. npm install

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: invalid outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests