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**
Your /public/static/locales/es
files are not added to your default-lambda function source code. Presuming that you are using the @sls-next/serverless-component
serverless component to deploy your Next.js infra, I had the same issue. I solved it with help from @dphang on the GitHub repo issues. postBuildCommands
are now supported in the serverless.yml
inputs. These will run after building your app and before deploying it. Modify your code as follows:
serverless.yml
myNextApp:
component: "@sls-next/[email protected]"
inputs:
timeout: 30
build:
postBuildCommands: ["node serverless-post-build.js"]
memory: 1024
Add a post-build script to copy the files over:
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");
You will need to edit the above script for your use case:
// 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");