【问题标题】:next-i18next deployed in AWS CloudFront traces error ENOENT: no such file or directory, scandir '/var/task/public/static/'部署在 AWS CloudFront 中的 next-i18next 跟踪错误 ENOENT:没有这样的文件或目录,scandir '/var/task/public/static/'
【发布时间】:2021-02-06 21:24:41
【问题描述】:

这使用了 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**

【问题讨论】:

    标签: next.js amazon-cloudfront i18next aws-lambda-edge


    【解决方案1】:

    您的 /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
    

    添加构建后脚本以复制文件:

    serverless-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");
    

    【讨论】:

    • 嗨,现在,我已经按照您的指南进行了配置。但它似乎只适用于 getStaticProps。使用 getServerSideProps。我得到了 404 not found 页面。
    猜你喜欢
    • 2017-12-28
    • 2021-04-27
    • 2021-12-05
    • 2020-04-04
    • 2021-12-04
    • 1970-01-01
    • 2022-09-30
    • 2022-06-28
    • 2022-01-25
    相关资源
    最近更新 更多