0

I have an application built with nextJs and this application should work on a local server (Windows). my customer told me that he needed this application to work in the background after searching I found that I needed to use a package called pm2 and when I used it gives me an error and I found that I needed to make some configurations for it and I can't found any helping resources, please help

1 Answers1

0

I found that to run nextJs application in the background you will need a custom configuration

  1. you need to download the pm2 globally in your system
  2. create a file with the name ecosystem.config.js in the root folder next to the package.json file
  3. you need to put your config data in this file which would be something like this

module.exports = {
    apps: [
        {
            name: "inventory_test",
            script: "node_modules/next/dist/bin/next",
            args: "start -p 3333", //running on port 3000
            watch: false,
        },
    ],
};
  • you should set the name as the name you want to see when you check the list of pm2
  • the problem will be solved when you set the script as I did in the code above to be more precise the default run of pm2 is to go to the node js folder in the system and try to make start for the application using npm directly but this is the problem we need to make it use the node runner from the nextjs itself or something like this so we change the script as above
  • after that, we set the arguments that we should run after the npm and in my example is the arg start and choose the port for our application too

and now we make our config

NOTES

  1. you should make build before you start the application
  2. to run the project you will open the folder of the project in the terminal || cmd || cmder and run the command pm2 start ecosystem.config.js