Javascript npm 在哪里安装包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5926672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:32:38  来源:igfitidea点击:

Where does npm install packages?

javascriptnode.jslocationnpm

提问by trnc

Can someone tell me where can I find the Node.js modules, which I installed using npm?

有人能告诉我在哪里可以找到我使用安装的 Node.js 模块npm吗?

回答by Decko

Global libraries

全球图书馆

You can run npm list -gto see which global libraries are installed and where they're located. Use npm list -g | head -1for truncated output showing just the path. If you want to display only main packages not its sub-packages which installs along with it - you can use - npm list --depth=0which will show all packages and for getting only globally installed packages, just add -g i.e. npm list -g --depth=0.

您可以运行npm list -g以查看安装了哪些全局库以及它们所在的位置。使用npm list -g | head -1用于显示人的路截断输出。如果您只想显示主软件包而不是与其一起安装的子软件包 - 您可以使用 -npm list --depth=0这将显示所有软件包并仅获取全局安装的软件包,只需添加 -g ie npm list -g --depth=0

On Unix systems they are normally placed in /usr/local/lib/nodeor /usr/local/lib/node_moduleswhen installed globally. If you set the NODE_PATHenvironment variable to this path, the modules can be found by node.

在 Unix 系统上,它们通常放置在全局中/usr/local/lib/node/usr/local/lib/node_modules在全局安装时。如果将NODE_PATH环境变量设置为此路径,则可以通过节点找到模块。

Windows XP - %USERPROFILE%\AppData\npm\node_modules
Windows 7, 8 and 10 - %USERPROFILE%\AppData\Roaming\npm\node_modules

Windows XP - %USERPROFILE%\AppData\npm\node_modules
Windows 7、8 和 10 -%USERPROFILE%\AppData\Roaming\npm\node_modules

Non-global libraries

非全局库

Non-global libraries are installed the node_modulessub folder in the folder you are currently in.

非全局库安装在node_modules您当前所在文件夹中的子文件夹中。

You can run npm listto see the installed non-global libraries for your current location.

您可以运行npm list以查看当前位置已安装的非全局库。

When installing use -g option to install globally

安装时使用 -g 选项全局安装

npm install -g pm2- pm2 will be installed globally. It will then typically be found in /usr/local/lib/node_modules(Use npm root -gto check where.)

npm install -g pm2- pm2 将被全局安装。然后通常会在/usr/local/lib/node_modulesnpm root -g用于检查位置。)

npm install pm2- pm2 will be installed locally. It will then typically be found in the local directory in /node_modules

npm install pm2- pm2 将安装在本地。然后通常会在本地目录中找到它/node_modules

回答by Joe Hawkins

The command npm rootwill tell you the effective installation directory of your npm packages.

该命令npm root将告诉您 npm 包的有效安装目录。

If your current working directory is a node package or a sub-directory of a node package, npm rootwill tell you the local installation directory. npm root -gwill show the global installation root regardless of current working directory.

如果你当前的工作目录是节点包或者节点包的子目录,npm root会告诉你本地安装目录。npm root -g无论当前工作目录如何,都将显示全局安装根目录。

Example:

例子:

$ npm root -g
/usr/local/lib/node_modules

See the documentation.

请参阅文档。

回答by jakub.g

For globally-installed modules:

对于全局安装的模块:

The other answers give you platform-specific responses, but a generic one is this:

其他答案为您提供特定于平台的响应,但通用的响应是这样的:

When you install global module with npm install -g something, npm looks up a config variable prefixto know where to install the module.

使用 安装全局模块时npm install -g something,npm 会查找配置变量prefix以了解模块的安装位置。

You can get that value by running npm config get prefix

您可以通过运行获得该值 npm config get prefix

To display all the global modules available in that folder use npm ls -g --depth 0(depth 0to not display their dependencies).

要显示该文件夹中可用的所有全局模块,请使用npm ls -g --depth 0depth 0不显示它们的依赖项)。

If you want to change the global modules path, use npm config editand put prefix = /my/npm/global/modules/prefixin the file or use npm config set prefix /my/npm/global/modules/prefix.

如果要更改全局模块路径,请使用npm config edit并放入prefix = /my/npm/global/modules/prefix文件或使用npm config set prefix /my/npm/global/modules/prefix.

When you use some tools like nodist, they change the platform-default installation path of global npm modules.

当您使用诸如nodist 之类的工具时,它们会更改全局 npm 模块的平台默认安装路径。

回答by 9swampy

On windows I used npm list -gto find it out. By default my (global) packages were being installed to C:\Users\[Username]\AppData\Roaming\npm.

在 Windows 上,我曾经npm list -g找到它。默认情况下,我的(全局)包被安装到C:\Users\[Username]\AppData\Roaming\npm.

回答by Ken Goodridge

If you are looking for the executable that npm installed, maybe because you would like to put it in your PATH, you can simply do

如果你正在寻找 npm 安装的可执行文件,也许是因为你想把它放在你的 PATH 中,你可以简单地做

npm bin

or

或者

npm bin -g

回答by Nick Campbell

In earlier versions of NPM modules were always placed in /usr/local/lib/node or wherever you specified the npm root within the .npmrc file. However, in NPM 1.0+ modules are installed in two places. You can have modules installed local to your application in /.node_modules or you can have them installed globally which will use the above.

在较早版本的 NPM 中,模块始终放置在 /usr/local/lib/node 或您在 .npmrc 文件中指定 npm 根目录的任何位置。但是,在 NPM 1.0+ 中,模块安装在两个地方。您可以在 /.node_modules 中为您的应用程序本地安装模块,或者您可以将它们全局安装,这将使用上述内容。

More information can be found at https://github.com/isaacs/npm/blob/master/doc/install.md

更多信息可以在https://github.com/isaacs/npm/blob/master/doc/install.md找到

回答by Alireza Fattahi

Not direct answer but may help ....

不是直接的答案,但可能会有所帮助....

The npm also has a cache folder, which can be found by running npm config get cache(%AppData%/npm-cacheon Windows).

npm 还有一个缓存文件夹,可以通过运行npm config get cache(%AppData%/npm-cache在 Windows 上) 找到它。

The npm modules are first downloaded here and then copied to npm global folder (%AppData%/Roaming/npmon Windows) or project specific folder (your-project/node_modules).

npm 模块首先在此处下载,然后复制到 npm 全局文件夹(%AppData%/Roaming/npm在 Windows 上)或项目特定文件夹 ( your-project/node_modules)。

So if you want to track npm packages, and some how, the list of all downloaded npm packages (if the npm cache is not cleaned) have a look at this folder. The folder structure is as {cache}/{name}/{version}

所以如果你想跟踪 npm 包,以及一些如何,所有下载的 npm 包的列表(如果 npm 缓存没有清理)看看这个文件夹。文件夹结构如下{cache}/{name}/{version}

This may help also https://docs.npmjs.com/cli/cache

这也可能有帮助https://docs.npmjs.com/cli/cache

回答by Mohanraj Balasubramaniam

If module was installed with global (-g) flag, you can get parent location by running following command

如果模块安装了 global ( -g) 标志,则可以通过运行以下命令来获取父位置

npm get prefix

or

或者

npm ls -g --depth=0

which will print location along with list of installed modules

这将打印位置以及已安装模块的列表

Cheers :-)

干杯:-)

回答by Tharaka

The easiest way would be to do

最简单的方法是做

npm list -g

npm 列表 -g

to list the package and view their installed location.

列出软件包并查看它们的安装位置。

I had installed npm via chololatey, so the location is

我已经通过 chololatey 安装了 npm,所以位置是

C:\MyProgramData\chocolatey\lib\nodejs.commandline.0.10.31\tools\node_modules

C:\MyProgramData\chocolatey\lib\nodejs.commandline.0.10.31\tools\node_modules

C:\MyProgramData\ is chocolatey repo location.

C:\MyProgramData\ 是巧克力回购位置。

回答by Adam Bubela

To get a compact list without dependencies simply use

要获得没有依赖关系的紧凑列表,只需使用

npm list -g --depth 0