npm 在哪里安装包?
有人可以告诉我在哪里可以找到我使用 npm
安装的 Node.js 模块吗?
Can someone tell me where can I find the Node.js modules, which I installed using npm
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
全局库
您可以运行
npm list -g
来查看安装了哪些全局库以及它们所在的位置。使用 npm list -g | head -1 用于仅显示路径的截断输出。如果您只想显示主包而不是与其一起安装的子包 - 您可以使用 -npm list --depth=0
它将显示所有包并仅获取全局安装的包,只需添加 -g 即npm list -g --depth=0
。在 Unix 系统上,全局安装时它们通常放置在
/usr/local/lib/node
或/usr/local/lib/node_modules
中。如果将NODE_PATH
环境变量设置为此路径,则可以通过节点找到模块。Windows XP -
%USERPROFILE%\AppData\npm\node_modules
Windows 7、8 和 10 -
%USERPROFILE%\AppData\Roaming\npm\node_modules
非全局库
非全局库安装在您所在文件夹中的
node_modules
子文件夹中 。您可以运行
npm list
来查看当前位置已安装的非全局库安装时使用 -g 选项全局安装
npm install -g pm2
- pm2 将全局安装。通常可以在/usr/local/lib/node_modules
中找到它(使用npm root -g
检查位置。)npm install pm2
- pm2 将在本地安装。然后通常可以在/node_modules
的本地目录中找到它Global libraries
You can run
npm list -g
to see which global libraries are installed and where they're located. Usenpm list -g | head -1
for 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=0
which will show all packages and for getting only globally installed packages, just add -g i.e.npm list -g --depth=0
.On Unix systems they are normally placed in
/usr/local/lib/node
or/usr/local/lib/node_modules
when installed globally. If you set theNODE_PATH
environment variable to this path, the modules can be found by node.Windows XP -
%USERPROFILE%\AppData\npm\node_modules
Windows 7, 8 and 10 -
%USERPROFILE%\AppData\Roaming\npm\node_modules
Non-global libraries
Non-global libraries are installed the
node_modules
sub folder in the folder you are currently in.You can run
npm list
to see the installed non-global libraries for your current location.When installing use -g option to install globally
npm install -g pm2
- pm2 will be installed globally. It will then typically be found in/usr/local/lib/node_modules
(Usenpm root -g
to check where.)npm install pm2
- pm2 will be installed locally. It will then typically be found in the local directory in/node_modules
命令
npm root
将告诉您 npm 软件包的有效安装目录。如果您当前的工作目录是节点包或节点包的子目录,npm root 会告诉您本地安装目录。
npm root -g
将显示全局安装根目录,无论当前工作目录如何。示例:
请参阅文档。
The command
npm root
will tell you the effective installation directory of your npm packages.If your current working directory is a node package or a sub-directory of a node package,
npm root
will tell you the local installation directory.npm root -g
will show the global installation root regardless of current working directory.Example:
See the documentation.
对于全局安装的模块:
其他答案为您提供特定于平台的响应,但通用的答案是:
当您使用
npm install -g some
安装全局模块时,npm 会查找配置变量prefix
了解模块的安装位置。您可以通过运行
npm config get prefix
来获取该值。要显示该文件夹中可用的所有全局模块,请使用
npm ls -g --depth 0
(深度 0
不显示其依赖项)。如果要更改全局模块路径,请使用
npm config edit
并将prefix = /my/npm/global/modules/prefix
放入文件中或使用npm 配置设置前缀 /my/npm/global/modules/prefix
。当您使用 nodist 等工具时,它们会更改全局 npm 模块的平台默认安装路径。
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 variableprefix
to know where to install the module.You can get that value by running
npm config get prefix
To display all the global modules available in that folder use
npm ls -g --depth 0
(depth 0
to not display their dependencies).If you want to change the global modules path, use
npm config edit
and putprefix = /my/npm/global/modules/prefix
in the file or usenpm 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.
在 Windows 上,我使用 npm list -g 来查找它。默认情况下,我的(全局)包被安装到 C:\Users\[用户名]\AppData\Roaming\npm。
On windows I used
npm list -g
to find it out. By default my (global) packages were being installed toC:\Users\[Username]\AppData\Roaming\npm
.如果您正在寻找 npm 安装的可执行文件,也许是因为您想将其放入 PATH 中,您可以简单地执行
或
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
or
如果模块是使用全局 (
-g
) 标志安装的,则可以通过运行以下命令获取父位置:或者
它将打印该位置以及已安装模块的列表。
If a module was installed with the global (
-g
) flag, you can get the parent location by running:or
which will print the location along with the list of installed modules.
不是直接答案,但可能会有所帮助......
npm 还有一个缓存文件夹,可以通过运行
npm config get cache
找到该文件夹(%AppData%/npm-cache
在 Windows 上)。npm 模块首先在此处下载,然后复制到 npm 全局文件夹(Windows 上的
%AppData%/Roaming/npm
)或项目特定文件夹(your-project/node_modules
) 。因此,如果您想跟踪 npm 包以及如何跟踪,请查看此文件夹,查看所有下载的 npm 包的列表(如果 npm 缓存未清理)。文件夹结构为
{cache}/{name}/{version}
这也可能有帮助 https://docs.npmjs.com/cli/cache
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-cache
on Windows).The npm modules are first downloaded here and then copied to npm global folder (
%AppData%/Roaming/npm
on Windows) or project specific folder (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}
This may help also https://docs.npmjs.com/cli/cache
在早期版本的 NPM 模块中,始终放置在 /usr/local/lib/node 中或在 .npmrc 文件中指定 npm 根目录的任何位置。然而,在 NPM 1.0+ 中,模块安装在两个地方。您可以将模块安装在 /.node_modules 中的应用程序本地,也可以将它们全局安装,这将使用上述内容。
更多信息请访问 https://github.com/isaacs/npm/blob/master/文档/安装.md
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.
More information can be found at https://github.com/isaacs/npm/blob/master/doc/install.md
要获得没有依赖关系的紧凑列表,只需使用
To get a compact list without dependencies simply use
正如其他答案所说,最好的方法是
但是,如果您安装了大量
npm
软件包,则此命令的输出可能会很长,并且向上滚动会很痛苦(有时甚至不可能向后滚动那么远)。在这种情况下,将输出通过管道传输到
more
程序,如下所示As the other answers say, the best way is to do
However, if you have a large number of
npm
packages installed, the output of this command could be very long and a big pain to scroll up (sometimes it's not even possible to scroll that far back).In this case, pipe the output to the
more
program, like this最简单的方法是
列出软件包并查看其安装位置。
我已经通过 chololaty 安装了 npm,所以位置是
C:\MyProgramData\chocolatey\lib\nodejs.commandline.0.10.31\tools\node_modules
C:\MyProgramData\ 是巧克力存储库位置。
The easiest way would be to do
to list the package and view their installed location.
I had installed npm via chololatey, so the location is
C:\MyProgramData\chocolatey\lib\nodejs.commandline.0.10.31\tools\node_modules
C:\MyProgramData\ is chocolatey repo location.
回显配置:
npm config ls
或npm config list
显示所有配置设置:
npm config ls -l
或npm config ls --json
打印有效的node_modules文件夹:
npm root
或npm root -g
打印本地前缀:
npm prefix
> 或npm 前缀 -g
(这是包含 package.json 文件或 node_modules 目录的最近父目录)
Echo the config:
npm config ls
ornpm config list
Show all the config settings:
npm config ls -l
ornpm config ls --json
Print the effective node_modules folder:
npm root
ornpm root -g
Print the local prefix:
npm prefix
ornpm prefix -g
(This is the closest parent directory to contain a package.json file or node_modules directory)
我在寻找真正的配置时开始发疯,所以这里是 linux 上所有配置文件的列表:
:
然后在此文件中配置前缀:
在 linux 中前缀默认为 /usr,${APPDATA }\npm in windows
节点模块位于 $prefix 树下,路径应包含 $prefix/bin
可能存在问题:
/root/.npmrc可以使用
!/home/youruser/.npmrc
。npm set -g prefix /usr
时,它会设置/etc/npmrc全局,但不会覆盖本地这是所有丢失的信息查找在哪里配置了什么。希望我已经详尽无遗。
I was beginning to go mad while searching for the real configuration, so here is the list of all configuration files on linux:
on windows:
Then in this file the prefix is configured:
The prefix is defaulted to /usr in linux, to ${APPDATA}\npm in windows
The node modules are under $prefix tree, and the path should contain $prefix/bin
There may be a problem :
/root/.npmrc
may be used!/home/youruser/.npmrc
.npm set -g prefix /usr
it sets the /etc/npmrc global, but doesn't override the localHere is all the informations that were missing to find what is configured where. Hope I have been exhaustive.
您可以通过命令查找全局已安装的模块
它将为您提供node.js模块的安装位置。
如果您在文件夹中本地安装 Node.js 模块,则可以键入以下命令来查看位置。
You can find globally installed modules by the command
It will provide you the location where node.js modules have been installed.
If you install node.js modules locally in a folder, you can type the following command to see the location.
扩展其他答案。
将显示全局安装的软件包的位置。
如果您想将该列表输出到一个文件,然后可以在文本编辑器中轻松搜索:
Expanding upon other answers.
will show you the location of globally installed packages.
If you want to output that list to a file that you can then easily search in your text editor:
来自 文档:
您可以使用
npm config get prefix
获取您的{prefix}
。 (当您使用 nvm 安装节点时很有用)。From the docs:
You can get your
{prefix}
withnpm config get prefix
. (Useful when you installed node with nvm).来自文档:
您可以使用
npm config get prefix
获取您的{prefix}
。 (当您使用 nvm 安装节点时很有用)。了解本地。
了解全球。
From the docs:
You can get your
{prefix}
withnpm config get prefix
. (Useful when you installed node with nvm).Read about locally.
Read about globally.
Windows 10:当我运行
npm prefix -g
时,我注意到安装位置位于我用来安装的 git shell 路径内。即使将该位置添加到路径中,也无法识别全局安装的软件包中的命令。修复方法:npm config edit
,Windows 10: When I ran
npm prefix -g
, I noticed that the install location was inside of the git shell's path that I used to install. Even when that location was added to the path, the command from the globally installed package would not be recognized. Fixed by:npm config edit
在 Ubuntu 14.04 中,它们安装在
/usr/lib/node_modules
In Ubuntu 14.04 they are installed at
/usr/lib/node_modules
适用于 Windows 7、8 和 10 -
%USERPROFILE%\AppData\Roaming\npm\node_modules
注意:
如果您位于文件夹中的某个位置,请输入
cd ..
直至进入C:
目录。然后,输入 cd %USERPROFILE%\AppData\Roaming\npm\node_modules。然后,%USERPROFILE%
会神奇地变成Users\YourUserProfile\
。我只是想澄清
Decko
在第一个回复中提到的想法。npm list -g
将列出您已全局安装的所有位。如果您需要查找与项目相关的npm 包
,则cd 'your Angular Project xyz'
,然后运行 npm list
。它将显示 npm 包中的模块列表。它还将为您提供缺少的依赖项
列表,并且您可能需要有效运行该项目。For Windows 7, 8 and 10 -
%USERPROFILE%\AppData\Roaming\npm\node_modules
Note:
If you are somewhere in folder type
cd ..
until you are inC:
directory. Then, typecd %USERPROFILE%\AppData\Roaming\npm\node_modules
. Then, magically,%USERPROFILE%
will change intoUsers\YourUserProfile\
.I just wanted to clarify on ideas referred by
Decko
in the first response.npm list -g
will list all the bits you have globally installed. If you need to find your project relatednpm package
thencd 'your angular project xyz'
, then runnpm list
. It will show list of modules innpm package
. It will also give you list ofdependencies
missing, and you may require to effectively run that project.顺便说一句,如果在本地找不到,npm 将在父文件夹(直到根目录)中查找node_modules。
Btw, npm will look for node_modules in parent folders (up to very root) if can not find in local.
如果您尝试从代码访问全局目录,则可以从
进程回溯。 execPath
。例如,要查找位于{NODE_GLOBAL_DIR}/bin/wsproxy
中的wsproxy
,您可以:还有
npm
cli 的工作原理@ec9fcc1
/lib/npm.js#L254 其中:另请参阅
ec9fcc1
/lib/install.js#L521:其中
globalPrefix
在ec9fcc1< 中设置了默认值/code>/lib/config/defaults.js#L92-L105
的:
If you're trying to access your global dir from code, you can backtrack from
process.execPath
. For example, to findwsproxy
, which is in{NODE_GLOBAL_DIR}/bin/wsproxy
, you can just:There's also how the
npm
cli works @ec9fcc1
/lib/npm.js#L254 with:See also
ec9fcc1
/lib/install.js#L521:Where
globalPrefix
has a default set inec9fcc1
/lib/config/defaults.js#L92-L105 of:如果您安装了 Visual Studio,您会发现它带有自己的节点副本,与您自己安装节点时路径上的副本不同 - 我的位于 C:\Program Files (x86)\Microsoft Visual Studio\2019 \Community\MSBuild\Microsoft\VisualStudio\NodeJs。
如果您从此目录中运行 npm 命令,您将发现 Visual Studio 中安装了哪些节点模块。
If you have Visual Studio installed, you will find it comes with its own copy of node separate from the one that is on the path when you installed node yourself - Mine is in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\NodeJs.
If you run the npm command from inside this directory you will find out which node modules are installed inside visual studio.