Node.js main parameter package.json

Iโ€™ve already searched quite a lot. However, still doubting the main parameter in package.json Node.js.

  • How will be filled in this field? By asking differently, can I run the module in a different style if this field is presented?
  • Can I add more than one script to the main parameter? If so, will they start as two threads? If not, how can I run two scripts in a module and run them in parallel?

I know that the second question is rather strange. This is because I adopted the Node.js application on OpenShift, but the application consists of two main components. One of them is a REST API, and one is a notification delivery service.

I'm afraid that the notification delivery process would block the REST API if they were implemented as a single thread. However, they must be connected to the same MongoDB cartridge. In addition, I would like to keep one gear, if both components can serve on the same gear, if possible.

Any suggestions are welcome.

+44
javascript rest asynchronous openshift
Mar 19 '14 at 16:59
source share
5 answers

From the npm documentation :

The main field is the identifier of the module, which is the main entry point into your program. That is, if your package is called foo and the user installs it and then requires ("foo"), then your main module will export the object.

This should be the module identifier relative to the root of your package folder.

For most modules, it is most advisable to have a main script and often not much more.

In short:

  • You only need the main parameter in package.json if the entry point to your package is different from index.js in the root folder. For example, people often put an entry point in lib/index.js or lib/<packagename>.js , in which case the corresponding script should be described as main in package.json .
  • You cannot have two scripts as main , simply because the require('yourpackagename') entry point must be uniquely defined.
+63
Mar 19 '14 at 17:07
source share

To answer your first question, the way the module is loaded depends on the module entry point and the main package.json parameter.

Say you have the following file structure:

 my-npm-module |-- lib | |-- module.js |-- package.json 

Without the main parameter in package.json, you need to load the module by specifying the module entry point: require('my-npm-module/lib/module.js') .

If you set the main package.json parameter as follows "main": "lib/module.js" , you can load the module as follows: require('my-npm-module') .

+20
Jan 15 '15 at 19:49
source share

If you have, for example, the package.json file:

 { "name": "zig-zag", "main": "lib/entry.js", ... } 

lib/entry.js will be the main entry point to your package. On call

require( 'zig-zag' );

in node, lib/entry.js will be the actual file that is required.

+7
Nov 24 '16 at 18:05
source share

One important function of the main key is that it provides a path for your entry point. This is very useful when working with nodemon . If you work with nodemon and you define the main key in package.json , as you can say "main": "./src/server/app.js" , then you can just start the server by entering nodemon in the CLI with the root as pwd instead of nodemon ./src/server/app.js .

+5
Dec 09 '15 at 2:37
source share

For OpenShift, you get only one pair of PORT and IP for binding (for each application). It looks like you should be able to serve both services from the same nodejs instance, adding internal routes for each service endpoint.

I have information on how OpenShift uses your package.json project to run your application here: https://www.openshift.com/blogs/run-your-nodejs-projects-on-openshift-in-two-simple -steps # package_json

0
Mar 27 '14 at 21:46
source share



All Articles