next.js - next-i18next deployed in AWS CloudFront traces error ENOENT: no such file or directory, scandir '/var/task/public/static/'
问题描述
and this uses next-i18next module. In my local server this works fine! for a multilang app. The problem is when I deployed this app to CloudFront as a serverless app. In my CloudWatch logs I can see:
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
My i18n.js is like this:
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')
})
My next.config.js file is like this:
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;
Any help please? How to avoid this error?
**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
输入中支持。这些将在构建您的应用程序之后和部署之前运行。修改您的代码如下:
无服务器.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");
推荐阅读
- json - 使用 python 从 JSON 响应中获取嵌套值
- python - .apply() 函数到数据框并返回新的数据框?
- excel - 如何创建一个新列,它告诉一个值是否唯一包含在另一列中?
- laravel - 如何解决laravel中保存图片的问题
- c# - 从中心点更改窗口尺寸?
- typescript - Testcafe Docker - 错误:TypeScript 编译失败。找不到名称“require”
- python-3.x - 从python3的嵌套数组中的键列表中获取值的优雅方法?
- android - 改造和api,进口问题
- java - 使用 Zerocode 运行 Junit 测试套件
- java - 如何检测 DFA 的错误状态?