Next.js next-i18next部署在AWS CloudFront中跟踪错误eNONT:没有这样的文件或目录,scandir'/var/task/public/static/';

Next.js next-i18next部署在AWS CloudFront中跟踪错误eNONT:没有这样的文件或目录,scandir'/var/task/public/static/';,next.js,amazon-cloudfront,i18next,aws-lambda-edge,Next.js,Amazon Cloudfront,I18next,Aws Lambda Edge,这使用next-i18next模块。在我的本地服务器上,这工作正常!对于多语言应用程序。 问题是当我将此应用程序作为无服务器应用程序部署到CloudFront时。 在我的CloudWatch日志中,我可以看到: da9949dd-1822-4d5c-b0d3-aeb56b6468ee ERROR Invoke Error { "errorType": "Error", "errorMessage": &

这使用next-i18next模块。在我的本地服务器上,这工作正常!对于多语言应用程序。 问题是当我将此应用程序作为无服务器应用程序部署到CloudFront时。 在我的CloudWatch日志中,我可以看到:

da9949dd-1822-4d5c-b0d3-aeb56b6468ee    ERROR   Invoke Error    
{
    "errorType": "Error",
    "errorMessage": "ENOENT: no such file or directory, scandir '/var/task/public/static/locales/es'",
    "code": "ENOENT",
    "errno": -2,
    "syscall": "scandir",
    "path": "/var/task/public/static/locales/es",
    "stack": [
        "Error: ENOENT: no such file or directory, scandir '/var/task/public/static/locales/es'",
        "    at Object.readdirSync (fs.js:955:3)",
        "    at getAllNamespaces (/var/task/pages/index.js:53691:19)",
        "    at createConfig (/var/task/pages/index.js:53696:27)",
        "    at new NextI18Next (/var/task/pages/index.js:66340:48)",
        "    at Object.k7Sn (/var/task/pages/index.js:54121:18)",
        "    at __webpack_require__ (/var/task/pages/index.js:31:31)",
        "    at Module.IlR1 (/var/task/pages/index.js:24470:12)",
        "    at __webpack_require__ (/var/task/pages/index.js:31:31)",
        "    at Module.NIeY (/var/task/pages/index.js:27944:17)",
        "    at __webpack_require__ (/var/task/pages/index.js:31:31)"
    ]
}

2020-10-23T18:56:15.679Z da9949dd-182
我的i18n.js是这样的:

const NextI18Next = require('next-i18next').default
const { localeSubPaths } = require('next/config').default().publicRuntimeConfig
const path = require('path')
path.resolve('./public/static/locales');

module.exports = new NextI18Next({
  otherLanguages: ['en'],
  localeSubPaths,
  defaultLanguage: process.env.NEXT_PUBLIC_MAIN_LANG, 
  localePath: path.resolve('./public/static/locales')
}) 
const withSass = require("@zeit/next-sass");

const localeCountries = [
     { label: 'es', name: 'España', lang: 'es' },
     { label: 'uk', name: 'United Kingdom', lang: 'en' },
     { label: 'mx', name: 'México', lang: 'es' }
];
const localeSubPaths = {
     es: 'es',
     en: 'en'
};
// Extend your Next config for advanced behavior
// See https://nextjs.org/docs/api-reference/next.config.js/introduction
let nextConfig = {
     serverRuntimeConfig: {
          PROJECT_ROOT: __dirname
     },
     publicRuntimeConfig: {
          localeCountries,
          staticFolder: '/static', 
     }
};

// Add the Next SASS plugin
nextConfig = withSass(nextConfig);

module.exports = nextConfig;
我的下一个.config.js文件如下所示:

const NextI18Next = require('next-i18next').default
const { localeSubPaths } = require('next/config').default().publicRuntimeConfig
const path = require('path')
path.resolve('./public/static/locales');

module.exports = new NextI18Next({
  otherLanguages: ['en'],
  localeSubPaths,
  defaultLanguage: process.env.NEXT_PUBLIC_MAIN_LANG, 
  localePath: path.resolve('./public/static/locales')
}) 
const withSass = require("@zeit/next-sass");

const localeCountries = [
     { label: 'es', name: 'España', lang: 'es' },
     { label: 'uk', name: 'United Kingdom', lang: 'en' },
     { label: 'mx', name: 'México', lang: 'es' }
];
const localeSubPaths = {
     es: 'es',
     en: 'en'
};
// Extend your Next config for advanced behavior
// See https://nextjs.org/docs/api-reference/next.config.js/introduction
let nextConfig = {
     serverRuntimeConfig: {
          PROJECT_ROOT: __dirname
     },
     publicRuntimeConfig: {
          localeCountries,
          staticFolder: '/static', 
     }
};

// Add the Next SASS plugin
nextConfig = withSass(nextConfig);

module.exports = nextConfig;
需要帮忙吗?如何避免这个错误

**ENOENT: no such file or directory, scandir '/var/task/public/static/locales/es**

您的
/public/static/locales/es
文件不会添加到默认lambda函数源代码中。假设您正在使用
@sls next/serverless组件
serverless组件部署next.js infra,我也遇到了同样的问题。我在GitHub回购问题的帮助下解决了这个问题<现在,
serverless.yml
输入中支持code>postBuildCommands
。这些将在构建应用程序之后和部署应用程序之前运行。按如下方式修改您的代码:

serverless.yml

mynextap:
组件:“@sls next/serverless”-component@1.19.0-α.0“
投入:
超时时间:30
建造:
PostBuild命令:[“node serverless post build.js”]
内存:1024
添加生成后脚本以将文件复制到:

无服务器后期构建.js

//post-build.js
常数fs=要求(“fs额外”);
log(“->复制区域设置目录…”);
//区域设置目录的路径
const localeSrc=“./static/locales”;
//默认lambda目标的路径
const localeDest=“../.serverless_nextjs/default lambda/static/locales”;
//以递归方式复制文件
copySync(localeSrc,localeDest,{recursive:true});
log(“已成功复制区域设置目录”);
您需要为您的用例编辑上述脚本:

//post-build.js
常数fs=要求(“fs额外”);
log(“->复制区域设置目录…”);
//区域设置目录的路径
const localeSrc=“/public/static/locales”;
//默认lambda目标的路径
const localeDest=“../.serverless_nextjs/default lambda/public/static/locales”;
//以递归方式复制文件
copySync(localeSrc,localeDest,{recursive:true});
log(“已成功复制区域设置目录”);

您的
/public/static/locales/es
文件不会添加到默认的lambda函数源代码中。假设您正在使用
@sls next/serverless组件
serverless组件部署next.js infra,我也遇到了同样的问题。我在GitHub回购问题的帮助下解决了这个问题<现在,
serverless.yml
输入中支持code>postBuildCommands
。这些将在构建应用程序之后和部署应用程序之前运行。按如下方式修改您的代码:

serverless.yml

mynextap:
组件:“@sls next/serverless”-component@1.19.0-α.0“
投入:
超时时间:30
建造:
PostBuild命令:[“node serverless post build.js”]
内存:1024
添加生成后脚本以将文件复制到:

无服务器后期构建.js

//post-build.js
常数fs=要求(“fs额外”);
log(“->复制区域设置目录…”);
//区域设置目录的路径
const localeSrc=“./static/locales”;
//默认lambda目标的路径
const localeDest=“../.serverless_nextjs/default lambda/static/locales”;
//以递归方式复制文件
copySync(localeSrc,localeDest,{recursive:true});
log(“已成功复制区域设置目录”);
您需要为您的用例编辑上述脚本:

//post-build.js
常数fs=要求(“fs额外”);
log(“->复制区域设置目录…”);
//区域设置目录的路径
const localeSrc=“/public/static/locales”;
//默认lambda目标的路径
const localeDest=“../.serverless_nextjs/default lambda/public/static/locales”;
//以递归方式复制文件
copySync(localeSrc,localeDest,{recursive:true});
log(“已成功复制区域设置目录”);

非常感谢!!!!现在可以了!!!!非常感谢!!!!现在可以了!!!!