如何在package.json中使用'main'参数? [英] How to use the 'main' parameter in package.json?

查看:2286
本文介绍了如何在package.json中使用'main'参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多搜索.但是,对于Node项目的package.json中的"main"参数仍然存在疑问.

I have done quite some search already. However, still having doubts about the 'main' parameter in the package.json of a Node project.

  1. 填写此字段有什么帮助?用另一种方式问,如果此字段出现,我可以以其他样式启动模块吗?
  2. 我可以在主要参数中填写多个脚本吗?如果是,它们是否将作为两个线程启动?如果没有,如何在一个模块中启动两个脚本并使它们并行运行?

我知道第二个问题很奇怪.这是因为我在OpenShift上托管了Node.js应用程序,但是该应用程序包含两个主要组件.一种是REST API,一种是通知传递服务.

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.

我担心如果将通知API实施为单个线程,则通知传递过程将阻止REST API.但是,它们必须连接到相同的MongoDB卡式盒.此外,如果可能的话,两个组件都可以在同一个齿轮中使用,我想节省一个齿轮.

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.

欢迎提出任何建议.

推荐答案

来自 npm文档:

主要字段是模块ID,是您的主要入口点 程序.也就是说,如果您的软件包名为foo,并且有用户安装 它,然后确实require("foo"),然后您的主模块的导出 对象将被返回.

The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

这应该是相对于包根目录的模块ID 文件夹.

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

对于大多数模块,拥有主脚本和 通常不会太多.

For most modules, it makes the most sense to have a main script and often not much else.

简而言之:

  1. 如果包的入口点与其根文件夹中的index.js不同,则仅在package.json中需要一个main参数.例如,人们经常将入口指向lib/index.jslib/<packagename>.js,在这种情况下,相应的脚本必须在package.json中描述为main.
  2. 您不能将两个脚本作为main,只是因为必须明确定义入口点require('yourpackagename').
  1. You only need a main parameter in your package.json if the entry point to your package differs from index.js in its root folder. For example, people often put the entry point to lib/index.js or lib/<packagename>.js, in this case the corresponding script must be described as main in package.json.
  2. You can't have two scripts as main, simply because the entry point require('yourpackagename') must be defined unambiguously.

这篇关于如何在package.json中使用'main'参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆