Access to the global node.js module

The npm documentation says the following:

  • If you install something that you want to use in your program using require ('whatever'), then install it locally, at the root of your project.
  • If you install something that you want to use in your shell, on the command line or something, install it globally so that its binaries are in the PATH environment variable.

I am currently writing --- or at least trying to write --- a genuine command-line program in node intended for use from the shell. Therefore, according to the above, my dependencies should be installed as global modules.

How can I use the global module installed with npm in node? The require() call does not work, of course, because the npm global module directory ( /usr/local/lib/node_modules ) is not specified in the path by default. I can make it work by explicitly adding it to the path at the top of my program, but this is really a lousy solution because it is not portable - it requires knowing where the global npm module directory is located on any given system.

To make life even worse, I have some global modules installed through dpkg. They were put in /usr/lib/nodejs and they just work. This bothers me, because if global modules should not be used for regular applications, I would not expect them to not be in the way; or I would expect both of them to go the way and demand that the global modules just work everywhere. Having one, but not the other, seems very strange. What's going on here?

Update: I must indicate that this program is just a script, with #!/usr/bin/env nodejs at the top; it is not a formal node module that overflows the path for something completely so trivial. Since Debian modules require everything from such a script, it seems reasonable to me that global npm modules should also be required, but I have a feeling that this is debatism ...

+6
source share
2 answers

Therefore, according to the above, my dependencies should be installed as global modules.

Not really.

This meant that your module could be installed as global, so its binaries would be accessible from the shell:

 npm install -g your-module your-module-binary --option etc. 

Its dependencies, on the other hand, should be installed after the 1st point located in the node_modules directory in your project (usually indicated in package.json , so npm can manage them). A.

But global modules are not available (usually) for require . They do not follow Folders from node_modules which npm follows local modules, and their path is usually not specified in NODE_PATH for loading from global folders .

+4
source

So, the instructions you have regarding npm modules, but you are doing local development. Here are some suggestions.

In terms of source code, you only need two types of require statements

 var dep = require('somedep') 

Use this for any core modules (e.g. fs ) and third-party modules that you need libraries that you include via npm (listing them in your .json packages as dependencies). Here you specify the unqualified name of the package and node finds the module according to its search algorithm.

 var mymod = require('./lib/mymod') 

Use this so that other modules in your project themselves along the path relative to the current javascript file.

This is all you need to do to handle javascript dependencies.

OK, now how do you install your dependencies?

For local development (in your original project tree) just enter cd into the project directory and run npm install , which will read your package.json file and install the modules you need in the node_modules subdirectory, and everything will be fine for local development.

If you really published it as an npm module, other users (and you can be either a developer or one of the โ€œother usersโ€) could install it using npm -g if you would like to access the binary utility project on their PATH , which should include /usr/lib/nodejs/lib/node_modules , but in this case npm -g will process both your code and your project dependencies at once.

This is where you get confused.

Therefore, according to the above, my dependencies should be installed as global modules.

You do not need to explicitly set the dependencies as global, only the top-level module that interests you, which in this case is your project. npm will handle dependencies automatically, which is its main goal in life. Project dependencies will not be installed globally for each user, but rather in the node_modules subdirectory of your project, which will be installed globally.

Here are the directories and what lives there:

  • ~/yourproject : local development for your source code
  • ~/yourproject/node_modules : npm modules used by your project during development. Created / populated by running npm install in ~/yourproject
  • /usr/lib/nodejs/lib/node_modules : npm modules (which may eventually include your project if you publish it to the npm registry) that are installed globally
  • /usr/lib/nodejs/lib/node_modules/yourproject/node_modules : the dependencies of your project will be installed here when you run npm install -g yourproject

You can also find my blog post on interpreter management and PATH .

+1
source

Source: https://habr.com/ru/post/926215/


All Articles