2

enter image description hereenter image description hereIf the main field in package.json file is neccessary for telling the entry point point of packages then why we include this field in package.json in node application's root. Do nodejs considers our node application as a package or a module?

mohit kaushik
  • 363
  • 2
  • 13
  • what do you mean by entry point? it merely tells what dependency your applications has w.r.t to other modules , how to run the app, how to test the app, and the app details - like name, version etc.. – xan_z Oct 31 '18 at 19:32

1 Answers1

1

It's a good practice to provide correct entry point. It's used during module resolutions. What you call 'application' is actually Node package. It can end up being used as such at some point, e.g. as sub-application inside another application.

A common case where this is applicable is testing where root module should be imported:

test/app.spec.js

const appInstance = require('..');
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • actually i am making an application in nodejs using express and in the root folder of the application, i have this package.json that is obviously to store details about my app and the dependencies, but i am confused why this "main" : "index.js" entry is there. What is the use of putting this there? – mohit kaushik Nov 01 '18 at 17:21
  • `index.js` is default value for `main` field. It could be omitted but it's there as a placeholder for custom value. This is what package.json looks like when it's generated from scratch with `npm init`. – Estus Flask Nov 01 '18 at 17:30