I’m losing my sanity over this issue.
I’m trying to run Next.js through PM2 cluster, which should be a pretty standard thing to do.
But it’s not.
I have this error
2024-09-21T23:46:38: PM2 log: App [anglio:0] online
▲ Next.js 14.2.11
- Local: http://localhost:3000
✓ Starting...
⨯ Failed to load env from .env.production.local Error: ENOTDIR: not a directory, stat '/app/ecosystem.config.js/.env.production.local'
at Object.statSync (node:fs:1659:25)
at loadEnvConfig (/app/node_modules/@next/env/dist/index.js:1:9602)
at loadConfig (/app/node_modules/next/src/server/config.ts:951:16)
at initialize (/app/node_modules/next/src/server/lib/router-server.ts:82:34)
at getRequestHandlers (/app/node_modules/next/src/server/lib/start-server.ts:66:20)
at Server.<anonymous> (/app/node_modules/next/src/server/lib/start-server.ts:293:34)
at Server.emit (node:events:531:35)
at Server.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/@pm2/io/src/metrics/httpMetrics.ts:167:50)
at emitListeningNT (node:net:1931:10)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
errno: -20,
code: 'ENOTDIR',
syscall: 'stat',
path: '/app/ecosystem.config.js/.env.production.local'
}
Next.js is trying to load env files from /app/ecosystem.config.js/.env.production.local
, /app/ecosystem.config.js/.env.local
, /app/ecosystem.config.js/.env.production
… which dopesn’t make sense.
My ecosystem file is:
module.exports = {
apps: [
{
name: 'anglio', // Name of your app
script: "./node_modules/next/dist/bin/next",
args: "start",
exec_mode: 'cluster', // Cluster mode to enable multi-core usage
instances: 1, // Number of instances
env: {
NODE_ENV: 'production', // Environment variables for production
},
env_development: {
NODE_ENV: 'development', // Environment variables for development
},
},
],
};
And my Docker file ends with:
CMD ["pm2-runtime", "start", "ecosystem.config.js"]
I don’t know what to do