36

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
  • 4,275
  • 1
  • 14
  • 22
Vicky
  • 401
  • 1
  • 5
  • 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

18 Answers18

29

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
  • 18,379
  • 16
  • 47
  • 61
Dinuka Dilshan
  • 552
  • 6
  • 12
20

This worked for me:

import {AnimatePresence, motion} from 'framer-motion/dist/framer-motion'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Soyas
  • 229
  • 1
  • 8
13

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
  • 4,275
  • 1
  • 14
  • 22
6

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
  • 745
  • 7
  • 9
6

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
  • 1,072
  • 1
  • 12
  • 25
3

Solved using this import:

import {motion} from 'framer-motion/dist/es/index'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
3

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
3

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
  • 30,962
  • 25
  • 85
  • 135
Tomasz Ostroga
  • 119
  • 1
  • 4
  • 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
2

This works for me.

npm uninstall framer-motion
npm install framer-motion@4.1.17
Kashif Ali
  • 187
  • 2
  • 9
1
import { motion } from 'framer-motion/dist/framer-motion'

importing like this solved it for me

vedanth bora
  • 634
  • 4
  • 7
1

I have faced the similar issue while working on latest version of framer motion. I have upgraded my react react-scripts to the latest version and it works for me.

Here is my dependencies.

"dependencies": {
    "framer-motion": "^7.2.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "^5.0.1",
},

1.Remove node_modules

   rm -rf node_modules

2.Install latest

npm install react react-dom react-scripts framer-motion

or

yarn install react react-dom react-scripts framer-motion

3. import framer motion

import { motion } from "framer-motion";
habib610
  • 11
  • 3
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
  • 11
  • 4
  • 1
    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
0

If you are using Create React App (CRA) and want to use Framer on the latest version (as I am writing version 7.2.0) you can use Craco to not eject.

  1. Install Craco
  2. In a new file called craco.config.js add this line that will call a plugin that we will create:
const { ProvidePlugin } = require("webpack");
const webpackFramerTyperScriptPlugin = require("./craco-plugin-framer-typescript");
module.exports = {
  webpack: {
    plugins: [
      new ProvidePlugin({
        React: "react",
      }),
    ],
  },
  plugins: [{ plugin: webpackFramerTyperScriptPlugin }],
};

  1. Create the plugin in a file named craco-plugin-framer-typescript.js
const { loaderByName, addBeforeLoader } = require("@craco/craco");
module.exports = {
  overrideWebpackConfig: ({ webpackConfig, cracoConfig, pluginOptions, context: { env, paths } }) => {
    const ruleToAdd = {
      test: /\.mjs$/,
      include: /node_modules/,
      type: "javascript/auto",
    };
    addBeforeLoader(webpackConfig, loaderByName("file-loader"), ruleToAdd);
    return webpackConfig;
  },
};

Hopefully it can help someone else that want to use CRA + TypeScript + Latest version of Framer.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
0

Check what version of react-scripts you have if react scripts are up-to-date then this wont help you . run npm uninstall react-scripts. I had to manually add them in package.json"react-scripts": "^5.0.1", check what is the latest version go to https://www.npmjs.com/package/react-scripts then type it in the package.json run npm i or install that should work. here is my dependencies list for reference.

"framer-motion": "^7.6.6",
"node-sass": "^8.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "^5.0.1",
"react-icons": "^4.6.0",
"react-tooltip": "^4.5.0",
"web-vitals": "^3.0.4"
0

I faced the similar Issue as here. I didn't downgrade the framer motion since I need new features.so I have done,

change import statement,

import { motion } from 'framer-motion/dist/framer-motion'

and Have to change react script version to latest,

"dependencies": {
    "framer-motion": "^7.2.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "^5.0.1",
}
0

React 18: This works for me to downgrade from version 10 to 6

npm uninstall framer-motion
npm install framer-motion@6.3.15
0

you need to download typescript to call the module:

npm install typescript

N H
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '23 at 10:04
0

For those still experiencing the issue. check your react-script version... Version 5 started to work here.

Umar Bunu
  • 11
  • 1