Motion Animation Library
Motion is a high-performance animation library designed for JavaScript, React, and Vue applications, currently at version 12.38.0. It offers a simple, declarative API that leverages a hybrid engine, combining JavaScript's flexibility with native browser APIs for GPU-accelerated, 120fps animations. Key differentiators include its production-readiness with comprehensive TypeScript support, extensive test suite, tree-shakability, and a tiny footprint. It provides a rich set of features out-of-the-box, such as gestures (drag), spring physics, layout transitions, scroll-linked effects, and animation timelines. While the core `motion` package targets vanilla JavaScript, it also provides specific packages and import paths for seamless integration with React (`motion/react`) and Vue (`motion-v`), making it a versatile choice for a wide range of web projects. Its evolution from Framer Motion signifies a streamlined approach to imports and usage across modern web frameworks.
Common errors
-
Module not found: Can't resolve 'framer-motion' in 'your-project-path'
cause You have installed the new `motion` package but your code still contains `import` statements referencing the deprecated `framer-motion` package.fixGlobally search and replace all instances of `import ... from 'framer-motion'` with `import ... from 'motion'` or `import ... from 'motion/react'` as appropriate for your code. -
TypeError: (0 , motion_react__WEBPACK_IMPORTED_MODULE_2__.motion) is not a function
cause This error typically indicates that the `motion` component from `motion/react` was not imported correctly or is being used outside of a valid React component context, often due to an incorrect default vs. named import, or attempting to use vanilla JS `motion` in a React component.fixEnsure you are using a named import for the `motion` component: `import { motion } from 'motion/react'`. Also, verify that `motion.div`, `motion.span`, etc., are being rendered within a React component's return statement. -
SyntaxError: Named export 'animate' not found. The requested module 'motion' is a CommonJS module, which may not support all module.exports as named exports.
cause This error occurs in environments where the bundler or runtime is trying to consume `motion` (an ESM package) as if it were a CommonJS module, and you're trying to destructure named exports from it.fixEnsure your build setup or Node.js environment correctly processes ESM. For Node.js, add `"type": "module"` to `package.json`. For bundlers, ensure your configuration is up-to-date and correctly handles module resolution for ESM packages.
Warnings
- breaking The animation library previously known as `framer-motion` has been rebranded and rewritten as `motion`. When migrating, you must update your `package.json` to use `motion` and crucially change all import statements from `framer-motion` to `motion` (for vanilla JS) or `motion/react` (for React components/hooks). Failing to update import paths will result in 'Module not found' errors.
- gotcha When developing with Motion in a React project, it's critical to import React-specific components and hooks from the `motion/react` subpath. Importing them directly from the base `motion` package will lead to runtime errors or unexpected behavior, as the base package provides vanilla JavaScript utilities not designed for React's component model.
- gotcha Motion is distributed as an ECMAScript Module (ESM) package. Direct usage in CommonJS environments (e.g., older Node.js scripts or tooling without explicit ESM support) can lead to `SyntaxError: Cannot use import statement outside a module` or similar issues.
Install
-
npm install motion -
yarn add motion -
pnpm add motion
Imports
- animate
const { animate } = require('motion')import { animate } from 'motion' - motion (React component)
import { motion } from 'framer-motion'import { motion } from 'motion/react' - useSpring
import { useSpring } from 'motion'import { useSpring } from 'motion/react' - Variants (type)
import type { Variants } from 'motion/types'
Quickstart
import { motion } from "motion/react";
import { useState } from 'react';
function AnimatedBox() {
const [isHovered, setIsHovered] = useState(false);
return (
<motion.div
style={{
width: '100px',
height: '100px',
backgroundColor: isHovered ? '#ff0088' : '#0099ff',
borderRadius: '10px',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
color: 'white',
cursor: 'pointer'
}}
initial={{ x: 0, scale: 1 }}
animate={{ x: isHovered ? 50 : 0, scale: isHovered ? 1.1 : 1 }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
onHoverStart={() => setIsHovered(true)}
onHoverEnd={() => setIsHovered(false)}
>
Hover Me
</motion.div>
);
}
// Example of how you would render this component in a client-side React application:
// import ReactDOM from 'react-dom/client';
// const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
// root.render(<AnimatedBox />);