Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error importing Framer Motion v5 in React (with create-react-app)

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)```
like image 218
Vicky Avatar asked Oct 29 '21 13:10

Vicky


People also ask

Can't import the named export framer motion?

Solution: You Just need to Downgrade the Framer motion version to “4.1. 17” in your package. json file and then run npm install Now, Your error must be solved.

How do I downgrade framer motion to 4.1 17?

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.

Can I use framer motion without react?

Framer Motion improves upon and simplifies the API in a way that couldn't have been done without breaking changes and rewriting. One difference is that whereas Framer Motion only has support for React, Pose has support for React-Native and Vue.

Does framer use react?

We built the new Framer library on React Hooks because it is simpler and ensures that beginners won't have to learn about classes, this, etc right off the bat. It's quite elegant really. TLDR; use functions and learn about Hooks if you plan to get more advanced with React.

Do I need framer for react app?

You don't need framer, you just need framer-motion for React App. Try this sandbox or this The version is "framer-motion": "3.10.6". You installed framer-motion right? Then you will have to import from that lib itself! Yes.Module '"framer-motion"' has no exported member 'Frame'.ts (2305)An error will be displayed.

What is framer motion in react?

Framer Motion is an animation library for creating declarative animations in React. It provides production-ready animations and a low-level API to help simplify the process of integrating animations into an application.

How to use React create app with backend?

Create React App doesn’t handle backend logic or databases; it just creates a frontend build pipeline, so you can use it with any backend you want. Under the hood, it uses Babel and webpack, but you don’t need to know anything about them. When you’re ready to deploy to production, running npm run build will create an optimized build ...

How to install framer motion on npm?

Just add "framer-motion": "^4.1.17" to you package.json and run npm install. 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. import {AnimatePresence, motion} from 'framer-motion/dist/framer-motion'


Video Answer


7 Answers

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.

like image 185
Dinuka Dilshan Avatar answered Oct 27 '22 00:10

Dinuka Dilshan


This worked for me:

import {AnimatePresence, motion} from 'framer-motion/dist/framer-motion'
like image 35
Soyas Limbu Avatar answered Oct 27 '22 00:10

Soyas Limbu


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

  • https://github.com/formatjs/formatjs/issues/1395#issuecomment-518823361
  • https://github.com/reactioncommerce/reaction-component-library/issues/399#issuecomment-467860022
like image 33
Cadin Avatar answered Oct 27 '22 01:10

Cadin


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.
like image 38
louielyl Avatar answered Oct 27 '22 00:10

louielyl


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 [email protected]
like image 23
Kimi Raikkonen 9790 Avatar answered Oct 26 '22 23:10

Kimi Raikkonen 9790


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'
like image 33
Tomasz Ostroga Avatar answered Oct 26 '22 23:10

Tomasz Ostroga


Solved using this import:

import {motion} from 'framer-motion/dist/es/index'
like image 39
Abdalrahman Daas Avatar answered Oct 26 '22 23:10

Abdalrahman Daas