Next.js Discord

Discord Forum

Build in Local, Run in Production (VPS)

Answered
Kurilian Bobtail posted this in #help-forum
Open in Discord
Kurilian BobtailOP
is it possible to do 'npm run build' in my local computer and copy the project folder (including .next and node modules) then run it on my own VPS without doing 'npm run build' again? I have tried this but the pm2 status shows errored because 'Could not find a production build in the '.next' directory. Try building your app with 'next build' before starting the production server'. I cannot build the project on my VPS because it only has 4GB memory (The load averages could hit 126 104 54 ) while my computer has 16GB. Did I miss something?
Answered by Himalayan
Create the ecosystem.config.js file in the root of your projects and put this there:
module.exports = {
  apps: [
    {
      name: "application",
      script: "./node_modules/next/dist/bin/next",
      exec_mode: "cluster",
      instances: "max",
      args: "start",
      env: {
        NODE_ENV: "production",
        PORT: 3000
      },
    },
  ],
};
View full answer

36 Replies

Spectacled bear
Are you sure you are copying all the files? It should work if you are copying everything
im not sure, but there could be some absolute paths being done/used in the built contents, and when copied it has a diferent path
Kurilian BobtailOP
@Spectacled bear @riský Here is what happen now. I can run 'npm run start' after I change a permission to the targeted file and the app runs properly. But when I try to run script 'node_modules/next/dist/bin/next" with pm2, the pm2 log shows error 'Could not find a production build in the .next'. I have copy the .next folder so that's why it could run the 'npm run start'. Did I choose the wrong path for the pm2?
@Kurilian Bobtail <@1171677685227995197> <@657067112434499595> Here is what happen now. I can run 'npm run start' after I change a permission to the targeted file and the app runs properly. But when I try to run script 'node_modules/next/dist/bin/next" with pm2, the pm2 log shows error 'Could not find a production build in the .next'. I have copy the .next folder so that's why it could run the 'npm run start'. Did I choose the wrong path for the pm2?
Himalayan
Create the ecosystem.config.js file in the root of your projects and put this there:
module.exports = {
  apps: [
    {
      name: "application",
      script: "./node_modules/next/dist/bin/next",
      exec_mode: "cluster",
      instances: "max",
      args: "start",
      env: {
        NODE_ENV: "production",
        PORT: 3000
      },
    },
  ],
};
Answer
Himalayan
for some reason next put it in node_modules
Then in the root of your project run pm2 start ecosystem.config.js
Don't forget if you play with the number of instances in the ecosystem file etc. to always pm2 del all before trying the new config via pm2 start ecosystem.config.js
if there is an issue in your editor try renaming .js to .cjs or .mjs
@Kurilian Bobtail Actually we have the similar approach, but the result remain the same. Is it required to use the path (script) from the root?
Himalayan
pm2 works from anywhere so probably just adjust the path.
I would suggest you to use rsync for copying files though. Are you using rsync?
i still recomend standalone way more then this madness of copying
its simple and way smaller so less of this madness of working out efficiency
@riský i still recomend standalone way more then this madness of copying
Himalayan
it's not madness.
i dont think it adds much buildtime to build, and copying is going to be so much faster
(rn im using gh actions to make standalone and then going to download it into vps - similar thing with specs)
Himalayan
PM2 is completely fine for small to medium projects
stop using github for commercial projects.
use git
instead
learn git
use github for opensource stuff but never commerical stuff
1. im using it for open source
2. github actions are very powerful
3. i didnt say anything against pm2 (id say it can be very useful with standalone)
4. github uses git so...
you don't need github at all
unles you want to share code with the world
which is not how commerical code bases work
but for open source, ok, no problem.
@Himalayan pm2 works from anywhere so probably just adjust the path. I would suggest you to use rsync for copying files though. Are you using rsync?
Kurilian BobtailOP
Ouu I didn't know about rsync, all I did were copy and paste. Thank you for suggestion btw, I'll try it next time
Kurilian BobtailOP
Good news everyone, it finally solved. I changed the pm2 script so it starts from the root and change the permission of some files. Thank you everyone for helping, have a nice day @Himalayan @riský @Clown @Spectacled bear @@ts-ignore
Original message was deleted
@Kurilian Bobtail could you please mark a solution to declare the thread is resolved? thank you
@joulev <@696247533810483210> could you please mark a solution to declare the thread is resolved? thank you
Kurilian BobtailOP
Sorry, I though it's automatic