31

When am trying to do simple animation for div using framer motion. Am getting this following error in browser

/node_modules/framer-motion/dist/es/components/AnimatePresence/index.mjs

Can't import the named export 'Children' from non EcmaScript module (only default export is available)```
Cadin
  • 3,515
  • 1
  • 13
  • 21
Vicky
  • 351
  • 1
  • 3
  • 11
  • 7
    Facing the same issue when upgraded to the latest version of framer motion. It may be a bug related to release. Solved it by downgrading the framer-motion verion at the moment until next release comes. Just add "framer-motion": "^4.1.17" to you package.json and run npm install. – Waleed Tariq Oct 30 '21 at 12:55

11 Answers11

21

I downgraded the Framer motion version to "4.1.17" by changing the package.json file and running npm install and it works for me.

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Dinuka Dilshan
  • 235
  • 2
  • 5
15

This worked for me:

import {AnimatePresence, motion} from 'framer-motion/dist/framer-motion'
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Soyas Limbu
  • 151
  • 4
12

Here's the response to the issue from the Framer Discord

regarding the issue with the current version of create-react-app (CRA) the issue is being tracked on GitHub here: https://github.com/formatjs/formatjs/issues/1395

After testing a bit it seems that the issue is with how CRA handles ESM dependancies and more particularly transitive dependancies are not handled correctly it seems. There is also an outstanding issue with CRA about this https://github.com/facebook/create-react-app/issues/10356.

Options:

  1. This is fixed/doesn't break in the next version of CRA which you can try today (https://github.com/facebook/create-react-app/discussions/11278) take note though its still in alpha.

  2. You can patch CRA to get around the issue as described in a number of tickets from other libraries

Cadin
  • 3,515
  • 1
  • 13
  • 21
4

In my opinion downgrading to older version is a last resort because you can't use newer features framer motion gives you.

What should work in this case is just changing import slightly:

import { motion } from 'framer-motion/dist/framer-motion'
Dharman
  • 26,923
  • 21
  • 73
  • 125
  • 1
    This is basically mentioned already in [this answer](https://stackoverflow.com/a/70387589/6045800) from about 2 weeks ago – Tomerikoo Dec 29 '21 at 12:46
3

Solved using this import:

import {motion} from 'framer-motion/dist/es/index'
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
2

To Solve Can't import the named export 'Children' from non EcmaScript module (only default export is available) Error You Just need to Downgrade Framer motion version to “4.1. 17” And Now, Your error must be solved.

https://exerror.com/cant-import-the-named-export-children-from-non-ecmascript-module-only-default-export-is-available/

npm i framer-motion@4.1.17
2

Additional Information:

For people who are struggling with the Could not find a declaration file for module 'framer-motion/dist/framer-motion'. error after applying the solutions above, depending on where you are importing the functions from, here is the trick to make the type works:

  • Create a declaration file in src, e.g. framer-motion.d.ts.
  • Add the following code inside the declaration file that you just created.
declare module "framer-motion/dist/framer-motion" {
  export * from "framer-motion";
}
  • Change the path "framer-motion/dist/framer-motion" to the path you are importing in your APP.
  • Save the .d.ts file and the type should not be bothering you anymore.
louielyl
  • 111
  • 1
  • 4
1

This works for me.

npm uninstall framer-motion
npm install framer-motion@4.1.17
Kashif Ali
  • 79
  • 1
  • 4
1
import { motion } from 'framer-motion/dist/framer-motion'

importing like this solved it for me

vedanth bora
  • 269
  • 3
  • 3
0

I ran into a similar issue with Storybook. I found a clue by researching a similar error in a Gatsby app:

I was able to fix this by adding gatsby-node.js at the root of my project and the following rule on the webpack:

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: 'javascript/auto',
        },
      ],
    },
  })
}

Storybook uses a slightly different format for its configuration, so I had to add this to .storybook/main.js:

module.exports = {
  ...

  webpackFinal: async (config, { configType }) => {
    // Resolve error when webpack-ing storybook:
    // Can't import the named export 'Children' from non EcmaScript module (only
    // default export is available)
    config.module.rules.push({
      test: /\.mjs$/,
      include: /node_modules/,
      type: "javascript/auto",
    });

    return config;
  }
}

I think you get the idea — add the above rule to webpack config, so that it treats *.mjs files correctly

todofixthis
  • 854
  • 10
  • 20
0

A little bit of confusion with the different solutions presented here. I have read this thread from top to bottom and there isn't a well-defined ultimate solution to this issue. It is either downgrading the version of the framer motion or a change in the import declaration. I had to downgrade initially but was concerned about updated features in higher versions. Then, I considered the import declaration solution. However, I had to review what started the issue for me in the first place. I was trying to use React-scroll though I had already begun using framer motion. After installing React-scroll, the configurations probably became disoriented. That was the point I began searching for solutions. So I had to make a choice since they've decided not to work together - discarded React-scroll to continue with Framer motion. That solved the issue for me.

GJEF
  • 1
  • 2
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31847568) – leepowers May 27 '22 at 22:50