How is use of npm to install global packages that don't even get used in Node applications justified?

后端 未结 4 1084
小鲜肉
小鲜肉 2021-01-06 00:52

My knowledge of npm is this:

It is a package manager for Node.js applications. What this means is that when you need someone else\'s librar

4条回答
  •  被撕碎了的回忆
    2021-01-06 01:49

    • See @mmocny's answer for why cca is a (global) npm package, despite not containing modules for use in JavaScript code.
    • This answer shows how global npm packages work.

    npm packages installed globally, with -g, typically contain executables (whether binary or not) that are to be added to a folder expected to be in your system's $PATH in order to make them globally available in your shell (from the command line) - independently of Node.js.

    As npm help folder puts it succinctly (emphasis mine):

    • Install it locally if you're going to require() it.
    • Install it globally if you're going to run it on the command line.

    Global package installation roughly works as follows:

    Note: What directory {prefix} represents varies by platform (e.g., /usr on Linux) - you can query its value with npm get prefix or npm prefix -g.
    The default Node.js installations on Unix systems install to shared locations, so that root privileges (via sudo) are required for installing packages globally.
    By contrast, if you are using Unix-based multi-version managers such as n or nvm, {prefix} may be a user-specific directory such as ~ or ~/.nvm/v0.10.28, so that root privileges are not required for installing packages globally.
    The following description is based on Unix platforms, with differing behavior on Windows noted separately, where needed.

    • Global packages are installed in a package-specific subfolder of {prefix}/lib/node_modules - e.g., /usr/local/lib/node_modules.

    • Symlinks to the executables from the package's bin subfolder (typically; as defined in the "bin" property of the package's package.json file) are then created in {prefix}/bin, e.g., usr/local/bin - which is what makes them globally available, given that {prefix}/bin is assumed to be in the $PATH.

      • Windows: .cmd batch files are used in lieu of symlinks, and they are placed directly in {prefix} (which is added to %PATH% during installation). These batch files are created based on analyzing a given executable's shebang line and explicitly invoke it with the interpreter found there, allowing for a seamless cross-platform experience.
    • man pages from the package, if defined, are symlinked to {prefix}/share/man, e.g., /usr/local/share/man

      • Windows: man pages are not installed at all.

    See also http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

提交回复
热议问题