I have done quite some search already. However, still having doubts about the main parameter in package.json of Node.js.
- How would filling in this field helps? Asking in another way, can I start the module in a different style if this field presents?
- Can I have more than one scripts being filled into the main parameter? If yes, would they being started as two threads? If no, how can I start two scripts in a module and having them run in parallel?
I know that the second question is quite weird. It is because I have hosted a Node.js application on OpenShift but the application consists of two main components. One being a REST API and one being a notification delivering service.
I afraid that the notification delivering process would block the REST API if they were implemented as a single thread. However, they have to connect to the same MongoDB cartridge. Moreover, I would like to save one gear if both the components could be serving in the same gear if possible.
Any suggestions are welcome.
If you have for instance in your
package.json
file:lib/entry.js
will be the main entry point to your package. When callingrequire( 'zig-zag' );
in node,
lib/entry.js
will be the actual file that is required.One important function of the
main
key is that it provides the path for your entry point. This is very helpful when working withnodemon
. If you work withnodemon
and you define themain
key in yourpackage.json
as let say"main": "./src/server/app.js"
, then you can simply crank up the server with typingnodemon
in the CLI with root as pwd instead ofnodemon ./src/server/app.js
.To answer your first question, the way you load a module is depending on the module entry point and the main parameter of the package.json.
Let's say you have the following file structure:
Without main parameter in the package.json, you have to load the module by giving the module entry point:
require('my-npm-module/lib/module.js')
.If you set the package.json main parameter as follows
"main": "lib/module.js"
, you will be able to load the module this way:require('my-npm-module')
.For OpenShift, you only get one PORT and IP pair to bind to (per application). It sounds like you should be able to serve both services from a single nodejs instance by adding internal routes for each service endpoint.
I have some info on how OpenShift uses your project's package.json to start your application here: https://www.openshift.com/blogs/run-your-nodejs-projects-on-openshift-in-two-simple-steps#package_json
Just think of it as the "starting point".
In a sense of object-oriented programming, say C#, it's the init() or constructor of the object class, that's what "entry point" meant.
For example
From the npm documentation:
To put it short:
main
parameter in yourpackage.json
if the entry point to your package differs fromindex.js
in its root folder. For example, people often put the entry point tolib/index.js
orlib/<packagename>.js
, in this case the corresponding script must be described asmain
inpackage.json
.main
, simply because the entry pointrequire('yourpackagename')
must be defined unambiguously.