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

next-i18next deployed in AWS CloudFront traces error ENOENT: no such file or directory, scandir '/var/task/public/static/'

这使用了 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')
}) 

我的next.config.js文件是这样的:

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 文件没有添加到您的 default-lambda 函数源代码中。假设您正在使用 @sls-next/serverless-component 无服务器组件来部署您的 Next.js 基础设施,我遇到了同样的问题。我在 @dphang 的帮助下解决了 GitHub 回购问题。 postBuildCommands 现在在 serverless.yml 输入中受支持。这些将 运行 在构建您的应用程序之后和部署它之前。修改您的代码如下:

serverless.yml

myNextApp:
  component: "@sls-next/serverless-component@1.19.0-alpha.0"
  inputs:
    timeout: 30
    build:
      postBuildCommands: ["node serverless-post-build.js"]
  memory: 1024

添加一个 post-build 脚本来复制文件:

无服务器-post-build.js

// post-build.js
const fs = require("fs-extra");
console.log("-> Copying locales directory...");
// Path to locales directory
const localeSrc = "./static/locales";
// Path to default-lambda destination
const localeDest = "./.serverless_nextjs/default-lambda/static/locales";
// Copy Files over recursively
fs.copySync(localeSrc, localeDest, { recursive: true });
console.log("Locale directory was copied successfully");

您需要为您的用例编辑上述脚本:

// post-build.js
const fs = require("fs-extra");
console.log("-> Copying locales directory...");
// Path to locales directory
const localeSrc = "./public/static/locales";
// Path to default-lambda destination
const localeDest = "./.serverless_nextjs/default-lambda/public/static/locales";
// Copy Files over recursively
fs.copySync(localeSrc, localeDest, { recursive: true });
console.log("Locale directory was copied successfully");