Repository: remy/nodemon (js)

Issue:
Argumentless nodemon command in npm project does not run value of "main" from package.json
### Expected behaviour
See stackoverflow question: https://stackoverflow.com/questions/72946313/

npm project js file can be run by the nodemon command without arguments as long as "main" set in package.json file. For example, if main was set to "server.js", when I run "nodemon", then what happens should be equivalent to "nodemon server.js". It shouldn't matter if there is an "index.js" file at the root of my project, the "nodemon" command should still run "nodemon server.js" 

### Actual behaviour
When there is an "index.js" file at the root of my project, it doesn't matter what the "main" attribute is in "package.json": it will always try to run "index.js" at root when I run "nodemon".

### Steps to reproduce
Make a directory
cd to that directory and make a new npm project with 'npm init -y'
in the package.json file, set main to "server.js"
make a server.js file at the root of the project, have it filled with some simple code, perhaps a simple Express app
make a index.js file at root, have it filled with some simple code
run `nodemon`

### Environment
nodemon 2.0.16
macOS Monterey 12.4
zsh 5.8.1 (x86_64-apple-darwin21.0)
npm 1.5.5
node 16.15.0



Pull request description:
Fix for : Giving preference to "main" of package.json for script in nodemon command
Fixes #2051 
Added fix for giving preference `main` of package.json for setting script in execOptions of nodemon command.

- Instead of using `index.js` directly for `script` in `configOptions`, check for `main` in package.json and if `main` does not exists then use `index.js`.

Patch:
diff --git a/lib/config/load.js b/lib/config/load.js
index bd5a03d..29e213b 100644
--- a/lib/config/load.js
+++ b/lib/config/load.js
@@ -16,7 +16,9 @@ var existsSync = fs.existsSync || path.existsSync;
 function findAppScript() {
   // nodemon has been run alone, so try to read the package file
   // or try to read the index.js file
-  if (existsSync('./index.js')) {
+  
+  var pkg = existsSync(path.join(process.cwd(), 'package.json')) && require(path.join(process.cwd(), 'package.json'));
+  if ((!pkg || pkg.main == undefined) && existsSync('./index.js')) {
     return 'index.js';
   }
 }


Test patch:
diff --git a/test/config/load.test.js b/test/config/load.test.js
index 623f2ca..7e472c0 100644
--- a/test/config/load.test.js
+++ b/test/config/load.test.js
@@ -320,5 +320,17 @@ describe('config load', function () {
     })
   });
 
+  it('should give package.main preference for script over index.js', function (done) {
+    var dir = path.resolve(pwd, 'test/fixtures/packages/main-and-index');
+    process.chdir(dir);
+
+    var config = {},
+      settings = {},
+      options = {};
 
+    load(settings, options, config, function (config) {
+      assert.deepEqual(config.execOptions.script, 'server.js');
+      done();
+    });
+  });
 });
diff --git a/test/fixtures/packages/main-and-index/index.js b/test/fixtures/packages/main-and-index/index.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/fixtures/packages/main-and-index/package.json b/test/fixtures/packages/main-and-index/package.json
new file mode 100644
index 0000000..f9962a6
--- /dev/null
+++ b/test/fixtures/packages/main-and-index/package.json
@@ -0,0 +1,3 @@
+{
+  "main": "server.js"
+}