Node.js中本地模块和全局模块有什么区别?何时使用本地和全局模块?

时间:2020-12-28 16:48:52

We can access local module using require function but cannot access global module through it. I read somewhere that to use global module we need to make it local then import it through require function. So if we cannot access global module directly, then what is the need of using it.

我们可以使用require函数访问本地模块,但不能通过它访问全局模块。我在某处读到要使用全局模块,我们需要将其设置为本地,然后通过require函数导入它。因此,如果我们无法直接访问全局模块,那么使用它的需求是什么。

3 个解决方案

#1


20  

You should:

你应该:

  • Install a module locally if you're going to require() it.
  • 如果你想要()它在本地安装一个模块。
  • Install a module globally if you're going to run it on the command line.
  • 如果要在命令行上运行它,请全局安装模块。

#2


2  

I think in my opinion the modules which you are going to require in your code must be in local to your project or you can say must be present in your node_modules directory

我认为在您的代码中您需要的模块必须位于项目的本地,或者您可以说必须存在于node_modules目录中

and the modules which works as command must be installed globally. examples are exress-generator,jsdocs,mocha

必须全局安装作为命令工作的模块。例子是exress-generator,jsdocs,mocha

#3


1  

A general use of global node module to my experience will be:

根据我的经验,全局节点模块的一般用途是:

If all my applications that uses that same node module (of the same version or I do not care which version , latest is fine for me), then I will install that node-module globally. One good example.example will be node module that I use it for testing -- e.g. mocha.

如果我的所有应用程序使用相同的节点模块(相同版本或我不关心哪个版本,最新版本对我来说),那么我将全局安装该节点模块。一个很好的例子。例子是我用它来测试的节点模块 - 例如摩卡。

#1


20  

You should:

你应该:

  • Install a module locally if you're going to require() it.
  • 如果你想要()它在本地安装一个模块。
  • Install a module globally if you're going to run it on the command line.
  • 如果要在命令行上运行它,请全局安装模块。

#2


2  

I think in my opinion the modules which you are going to require in your code must be in local to your project or you can say must be present in your node_modules directory

我认为在您的代码中您需要的模块必须位于项目的本地,或者您可以说必须存在于node_modules目录中

and the modules which works as command must be installed globally. examples are exress-generator,jsdocs,mocha

必须全局安装作为命令工作的模块。例子是exress-generator,jsdocs,mocha

#3


1  

A general use of global node module to my experience will be:

根据我的经验,全局节点模块的一般用途是:

If all my applications that uses that same node module (of the same version or I do not care which version , latest is fine for me), then I will install that node-module globally. One good example.example will be node module that I use it for testing -- e.g. mocha.

如果我的所有应用程序使用相同的节点模块(相同版本或我不关心哪个版本,最新版本对我来说),那么我将全局安装该节点模块。一个很好的例子。例子是我用它来测试的节点模块 - 例如摩卡。

相关文章