From bab656d609455fc6754ab3ec9915235f7ce3fb6e Mon Sep 17 00:00:00 2001 From: Abhinav <119372409+Abhinav5383@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:57:17 +0000 Subject: [PATCH 1/3] throw an error if the provided `cwd` doesn't exist --- lib/Common.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Common.js b/lib/Common.js index 438628655..2e935a81a 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -145,6 +145,10 @@ Common.prepareAppConf = function(opts, app) { cwd && (cwd[0] != '/') && (cwd = path.resolve(process.cwd(), cwd)); cwd = cwd || opts.cwd; + if (!fs.existsSync(cwd)) { + return new Error(`Provided cwd: "${cwd}" does not exist!`); + } + // Full path script resolution app.pm_exec_path = path.resolve(cwd, app.script); From 17917a67961c79f22bcb6235e46144f3286c6638 Mon Sep 17 00:00:00 2001 From: Abhinav <119372409+Abhinav5383@users.noreply.github.com> Date: Wed, 8 Apr 2026 15:49:51 +0000 Subject: [PATCH 2/3] test if it throws an error on invalid cwd --- test/programmatic/programmatic.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/programmatic/programmatic.js b/test/programmatic/programmatic.js index 9c31109ec..2cd7fcba5 100644 --- a/test/programmatic/programmatic.js +++ b/test/programmatic/programmatic.js @@ -95,6 +95,16 @@ describe('PM2 programmatic calls', function() { }); }); + it('should throw if cwd doesn\'t exist', function(done) { + pm2.start({ + command: 'echo $PWD', + cwd: "./IMAGINARY_DIR", + }, function(err, data) { + should.exists(err); + done(); + }) + }); + it('should notice error if wrong file passed', function(done) { pm2.start('./UNKNOWN_SCRIPT.js', { force : true, From 82fa4886779270eaa6b0777227067c7851a83a56 Mon Sep 17 00:00:00 2001 From: Abhinav <119372409+Abhinav5383@users.noreply.github.com> Date: Wed, 8 Apr 2026 15:55:00 +0000 Subject: [PATCH 3/3] better error message for invalid cwd --- lib/Common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Common.js b/lib/Common.js index 2e935a81a..f5f3e9fbd 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -146,7 +146,7 @@ Common.prepareAppConf = function(opts, app) { cwd = cwd || opts.cwd; if (!fs.existsSync(cwd)) { - return new Error(`Provided cwd: "${cwd}" does not exist!`); + return new Error(`No such directory, cwd '${cwd}'`); } // Full path script resolution