简体   繁体   English

Next JS - 使用 framer-motion 时出现语法错误

[英]Next JS - SyntaxError when using framer-motion

I'm using NextJS version 12.0.3 with framer motion for animations.我正在使用 NextJS 版本 12.0.3 和动画的成帧器动作。 Putting the functionality of the framer-motion library aside, when I tag any html element in my component, I get an error将 framer-motion 库的功能放在一边,当我在组件中标记任何 html 元素时,出现错误

error - SyntaxError: The requested module 'react' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'react';
const { createContext } = pkg;

The file (Login.jsx) causing the error:导致错误的文件(Login.jsx):

import { motion } from "framer-motion";

const Login: React.FC = () => {
  return (
    <motion.div
      className=""
    >
    </motion.div>
  );
};

export default Login;

Oddly enough, when I am routed TO the logo page from a different page, the page loads fine.奇怪的是,当我从另一个页面路由到徽标页面时,页面加载正常。 But when I REFRESH the page, I'm faced with this error.但是当我刷新页面时,我遇到了这个错误。 When I keep the import statement for framer-motion, BUT remove the motion.当我保留 framer-motion 的导入语句时,但删除了该动作。 tags on my div element, the error does not persist when I refresh.我的 div 元素上的标签,刷新时错误不会持续存在。

Is this a common issue with Next JS 12.0.3?这是 Next JS 12.0.3 的常见问题吗? Should I just roll back to previous versions?我应该回滚到以前的版本吗?

Thanks for your time!!谢谢你的时间!!

I had the same errors by importing framer-motion.导入 framer-motion 时我遇到了同样的错误。 I solved this problem change import by require:我通过要求解决了这个问题更改导入:

const { motion } = require("framer-motion"); const {motion } = require("framer-motion");

I think the problem is from the latest update.我认为问题来自最新更新。 This works for me yarn add framer-motion@6.5.1这对我有用 yarn add framer-motion@6.5.1

I was also facing the same issue and looking at the upgrade guide of next js I found that the I was using nodejs version < 12.22.0 which caused this issue.我也面临同样的问题,查看下一个 js 的升级指南,我发现我使用的 nodejs 版本 < 12.22.0 导致了这个问题。 Upgrading it to nodejs 12.22.0 fixed the issue for me.将它升级到 nodejs 12.22.0 为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM