这个错误在使用AWS Amplify,React和Next.js构建应用程序时可能会出现。通常情况下,它是由尝试在同一个目录中同时构建本地和云资源而引起的。为了解决这个问题,您可以将构建目录移动到一个不同的位置。
以下是一个示例代码片段,展示了如何使用AWS Amplify,React和Next.js:
import React from 'react';
import Amplify from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
class MyApp extends React.Component {
render() {
return (
My App
);
}
}
export default withAuthenticator(MyApp, true);
如果您在同一个目录中同时构建本地和云资源,您可能会遇到错误:
Error: Source and destination must not be the same.
要解决此错误,请将构建目录移动到一个不同的位置。例如,在您的项目根目录中创建一个名为“build”的文件夹,并将本地和云资源的构建输出分别保存在该文件夹中的“local”和“cloud”子文件夹中:
amplify init
amplify add hosting
amplify publish
然后在package.json中启用预构建和构建命令:
"scripts": {
"prebuild": "mkdir -p ./build/local && mkdir -p ./build/cloud",
"build": "next build && amplify publish"
},
现在,当您运行npm run build时,您的本地和云资源将构建到不同的目录中,并且您应该不再遇到“Source and destination must not be the same”错误。