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
Asked
Active
Viewed 1,021 times
0

Ahmed Mohammed
- 35
- 1
- 6
-
What do you mean by running in the background? – Likepineapple Jan 17 '23 at 09:44
-
the application run automatically when the server boot or reboot and not using cmd to run it – Ahmed Mohammed Jan 17 '23 at 09:49
-
You might want to have a look at this https://stackoverflow.com/questions/20575257/how-do-i-run-a-powershell-script-when-the-computer-starts – Likepineapple Jan 17 '23 at 09:54
1 Answers
0
I found that to run nextJs application in the background you will need a custom configuration
- you need to download the pm2 globally in your system
- create a file with the name
ecosystem.config.js
in the root folder next to thepackage.json
file - 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 ofpm2
- the problem will be solved when you set the
script
as I did in the code above to be more precise the default run ofpm2
is to go to the node js folder in the system and try to makestart
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 thenpm
and in my example is the argstart
and choose the port for our application too
and now we make our config
NOTES
- you should make
build
before you start the application - to run the project you will open the folder of the project in the
terminal
||cmd
||cmder
and run the commandpm2 start ecosystem.config.js

Ahmed Mohammed
- 35
- 1
- 6