What is the difference between local and global module in Node.js? When to use local and global module?

。_饼干妹妹 提交于 2020-02-18 07:20:49

问题


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.


回答1:


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:


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

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




回答3:


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.



来源:https://stackoverflow.com/questions/29530459/what-is-the-difference-between-local-and-global-module-in-node-js-when-to-use-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!