question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[CRA] dynamic Import don't work

See original GitHub issue

Describe the bug If using dynamic import, throw error Support for the experimental syntax 'dynamicImport' isn't currently enabled. But in CRA don’t have the error.

To Reproduce Steps to reproduce the behavior:

  1. Create a new project with CRA and SB
  2. Use dynamic import
  3. See error

System:

  • OS: [MacOS]
  • Framework: CRA 2.0.5
  • Version: [e.g. 4.0.4]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:24 (20 by maintainers)

github_iconTop GitHub Comments

9reactions
igor-dvcommented, Nov 8, 2018

As a workaround, you can create .babelrc in the .storybook dir with something like this:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": ["@babel/plugin-syntax-dynamic-import"]
}

(Maybe some other plugins/presets will be needed)

@Hypnosphi , WYT about adding this by default ? looks like CRA uses it

4reactions
patrickleetcommented, Jul 2, 2019

Came across this today - took some digging, but eventually found a solution.

The bug is actually in webpack, which storybook uses.

It is this: https://github.com/webpack/webpack/issues/8656

The solution is to install the correct version of acorn

npm i --save-dev acorn@6.1.1

Maybe worth doing the whole rm -rf node_modules && rm package-lock.json thing as well.

For completeness, here is the .babelrc file that worked with our project:

.babelrc

{
  "presets":[
    ["@babel/preset-env"],
    ["@babel/preset-react"],
  ],
  "plugins": [
    ["@babel/plugin-proposal-class-properties"],
    ["@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ],
    ["@babel/plugin-syntax-dynamic-import"]
  ]
}

and relevant devdeps from package.json:

  "devDependencies": {
    "@babel/core": "7.4.5",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/polyfill": "^7.4.4",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "7.4.4",
    "@babel/runtime": "7.4.5",
    "@storybook/addon-actions": "^5.1.9",
    "@storybook/addon-knobs": "^5.1.9",
    "@storybook/addon-links": "^5.1.9",
    "@storybook/addons": "^5.1.9",
    "@storybook/react": "^5.1.9",
    "acorn": "^6.1.1",
    "babel-loader": "^8.0.6",
    ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic import doesn't work with combined CRA and SSR
It works fine but in my code I have a dynamic import await import("localization/" + newLanguage + "/" + newLanguage + ".json"); and...
Read more >
React dynamic imports and route-centric code splitting guide
Learn how to speed up your React apps using dynamic imports, React.lazy(), React.Suspense, React Router, and Loadable Components.
Read more >
Code-Splitting - React
The React.lazy function lets you render a dynamic import as a regular component. Before: import OtherComponent from '.
Read more >
CRA create react app build failure. "The target environment ...
CRA create react app build failure. "The target environment doesn't support dynamic import() syntax so it's not possible to use external type ' ......
Read more >
Dynamic SVG Imports in Create-React-App | by Ryan Hutzley
You have a bunch of SVGs you want to include in your CRA. You want to avoid a gazillion import statements at the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found