npm 在哪里安装包?

发布于 2024-11-05 21:43:46 字数 75 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

万水千山粽是情ミ 2024-11-12 21:43:46

全局库

您可以运行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. Use npm 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 the NODE_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 (Use npm 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

鸢与 2024-11-12 21:43:46

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

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

示例:

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

请参阅文档。

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:

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

See the documentation.

我很OK 2024-11-12 21:43:46

对于全局安装的模块:

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

当您使用 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 variable prefix 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 put prefix = /my/npm/global/modules/prefix in the file or use 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.

梦里梦着梦中梦 2024-11-12 21:43:46

在 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 to C:\Users\[Username]\AppData\Roaming\npm.

我喜欢麦丽素 2024-11-12 21:43:46

如果您正在寻找 npm 安装的可执行文件,也许是因为您想将其放入 PATH 中,您可以简单地执行

npm bin

npm bin -g

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 bin

or

npm bin -g
离不开的别离 2024-11-12 21:43:46

如果模块是使用全局 (-g) 标志安装的,则可以通过运行以下命令获取父位置:

npm get prefix

或者

npm ls -g --depth=0

它将打印该位置以及已安装模块的列表。

If a module was installed with the global (-g) flag, you can get the parent location by running:

npm get prefix

or

npm ls -g --depth=0

which will print the location along with the list of installed modules.

你如我软肋 2024-11-12 21:43:46

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

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

久隐师 2024-11-12 21:43:46

在早期版本的 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

孤独陪着我 2024-11-12 21:43:46

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

npm list -g --depth 0

To get a compact list without dependencies simply use

npm list -g --depth 0
汐鸠 2024-11-12 21:43:46

正如其他答案所说,最好的方法是

npm list -g

但是,如果您安装了大量 npm 软件包,则此命令的输出可能会很长,并且向上滚动会很痛苦(有时甚至不可能向后滚动那么远)。

在这种情况下,将输出通过管道传输到 more 程序,如下所示

npm list -g | more

As the other answers say, the best way is to do

npm list -g

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

npm list -g | more
吃素的狼 2024-11-12 21:43:46

最简单的方法是

npm 列表 -g

列出软件包并查看其安装位置。

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

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

C:\MyProgramData\ 是巧克力存储库位置。

The easiest way would be to do

npm list -g

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.

奢华的一滴泪 2024-11-12 21:43:46
  • 回显配置:npm config lsnpm config list

  • 显示所有配置设置:npm config ls -lnpm config ls --json

  • 打印有效的node_modules文件夹:npm rootnpm root -g

  • 打印本地前缀:npm prefix > 或 npm 前缀 -g

    (这是包含 package.json 文件或 node_modules 目录的最近父目录)


  • Echo the config: npm config ls or npm config list

  • Show all the config settings: npm config ls -l or npm config ls --json

  • Print the effective node_modules folder: npm root or npm root -g

  • Print the local prefix: npm prefix or npm prefix -g

    (This is the closest parent directory to contain a package.json file or node_modules directory)


┈┾☆殇 2024-11-12 21:43:46

我在寻找真正的配置时开始发疯,所以这里是 linux 上所有配置文件的列表:

  • /etc/npmrc
  • /home/youruser/.npmrc
  • /root/.npmrc
  • ./.npmrc 在当前目录下 Windows 上的package.json 文件(感谢 @CyrillePontvieux)

  • c/Program\ Files/nodejs/node_modules/npm/npmrc

然后在此文件中配置前缀:

prefix=/usr

在 linux 中前缀默认为 /usr,${APPDATA }\npm in windows

节点模块位于 $prefix 树下,路径应包含 $prefix/bin

可能存在问题:

  • 当您全局安装时,您使用“sudo su”,然后使用 /root/.npmrc可以使用
  • 当您在本地使用而不使用 sudo 时:对于您的用户来说,它是 /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:

  • /etc/npmrc
  • /home/youruser/.npmrc
  • /root/.npmrc
  • ./.npmrc in the current directory next to package.json file (thanks to @CyrillePontvieux)

on windows:

  • c/Program\ Files/nodejs/node_modules/npm/npmrc

Then in this file the prefix is configured:

prefix=/usr

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 :

  • When you install globally, you use "sudo su" then the /root/.npmrc may be used!
  • When you use locally without sudo: for your user its the /home/youruser/.npmrc.
  • When your path doesn't represent your prefix
  • When you use npm set -g prefix /usr it sets the /etc/npmrc global, but doesn't override the local

Here is all the informations that were missing to find what is configured where. Hope I have been exhaustive.

岁吢 2024-11-12 21:43:46

您可以通过命令查找全局已安装的模块

npm list -g

它将为您提供node.js模块的安装位置。

C:\Users\[Username]\AppData\Roaming\npm

如果您在文件夹中本地安装 Node.js 模块,则可以键入以下命令来查看位置。

npm list

You can find globally installed modules by the command

npm list -g

It will provide you the location where node.js modules have been installed.

C:\Users\[Username]\AppData\Roaming\npm

If you install node.js modules locally in a folder, you can type the following command to see the location.

npm list
孤凫 2024-11-12 21:43:46

扩展其他答案。

npm list -g

将显示全局安装的软件包的位置。

如果您想将该列表输出到一个文件,然后可以在文本编辑器中轻松搜索:

npm list -g > ~/Desktop/npmfiles.txt

Expanding upon other answers.

npm list -g

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 list -g > ~/Desktop/npmfiles.txt
甜是你 2024-11-12 21:43:46

来自 文档

在 npm 1.0 中,有两种安装方式:

  • 全局 —- 这会在 {prefix}/lib/node_modules 中删除模块,并将可执行文件放入 {prefix}/bin 中,其中 {前缀}通常是
    类似/usr/local。它还安装了手册页
    {prefix}/share/man(如果提供)。

  • 本地 ---- 这会将您的软件包安装在当前工作目录中。节点模块放入 ./node_modules,可执行文件放入
    ./node_modules/.bin/,并且根本没有安装手册页。

您可以使用 npm config get prefix 获取您的 {prefix}。 (当您使用 nvm 安装节点时很有用)。

From the docs:

In npm 1.0, there are two ways to install things:

  • globally —- This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually
    something like /usr/local. It also installs man pages in
    {prefix}/share/man, if they’re supplied.

  • locally —- This installs your package in the current working directory. Node modules go in ./node_modules, executables go in
    ./node_modules/.bin/, and man pages aren’t installed at all.

You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).

萤火眠眠 2024-11-12 21:43:46

来自文档

包被放入前缀下的node_modules文件夹中。
本地安装时,这意味着您可以
require("packagename") 加载其主模块,或者
require("packagename/lib/path/to/sub/module") 加载其他模块。

Unix 系统上的全局安装转到 {prefix}/lib/node_modules。
Windows 上的全局安装转到 {prefix}/node_modules(即,没有
lib 文件夹。)

作用域包的安装方式相同,只不过它们是分组的
与相关的 node_modules 文件夹的子文件夹中
该范围的名称由 @ 符号前缀,例如 npm install
@myorg/package 会将包放在
{前缀}/node_modules/@myorg/package.请参阅范围了解更多详细信息。

如果您希望 require() 一个软件包,请在本地安装它。

您可以使用 npm config get prefix 获取您的 {prefix}。 (当您使用 nvm 安装节点时很有用)。

了解本地
了解全球

From the docs:

Packages are dropped into the node_modules folder under the prefix.
When installing locally, this means that you can
require("packagename") to load its main module, or
require("packagename/lib/path/to/sub/module") to load other modules.

Global installs on Unix systems go to {prefix}/lib/node_modules.
Global installs on Windows go to {prefix}/node_modules (that is, no
lib folder.)

Scoped packages are installed the same way, except they are grouped
together in a sub-folder of the relevant node_modules folder with the
name of that scope prefix by the @ symbol, e.g. npm install
@myorg/package would place the package in
{prefix}/node_modules/@myorg/package. See scope for more details.

If you wish to require() a package, then install it locally.

You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).

Read about locally.
Read about globally.

不必你懂 2024-11-12 21:43:46

Windows 10:当我运行 npm prefix -g 时,我注意到安装位置位于我用来安装的 git shell 路径内。即使将该位置添加到路径中,也无法识别全局安装的软件包中的命令。修复方法:

  1. 运行npm config edit
  2. 将前缀更改为“C:\Users\username\AppData\Roaming\npm”,
  3. 将该路径添加到系统路径变量中,
  4. 使用 -g 重新安装软件包。

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:

  1. running npm config edit
  2. changing the prefix to 'C:\Users\username\AppData\Roaming\npm'
  3. adding that path to the system path variable
  4. reinstalling the package with -g.
迷荒 2024-11-12 21:43:46

在 Ubuntu 14.04 中,它们安装在

/usr/lib/node_modules

In Ubuntu 14.04 they are installed at

/usr/lib/node_modules

仙女山的月亮 2024-11-12 21:43:46

适用于 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 in C: directory. Then, type cd %USERPROFILE%\AppData\Roaming\npm\node_modules. Then, magically, %USERPROFILE% will change into Users\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 related npm package then cd 'your angular project xyz', then run npm list. It will show list of modules in npm package. It will also give you list of dependencies missing, and you may require to effectively run that project.

温柔嚣张 2024-11-12 21:43:46

顺便说一句,如果在本地找不到,npm 将在父文件夹(直到根目录)中查找node_modules。

Btw, npm will look for node_modules in parent folders (up to very root) if can not find in local.

帅的被狗咬 2024-11-12 21:43:46

如果您尝试从代码访问全局目录,则可以从 进程回溯。 execPath。例如,要查找位于 {NODE_GLOBAL_DIR}/bin/wsproxy 中的 wsproxy,您可以:

path.join(path.dirname(process.execPath), 'wsproxy')

还有 npm cli 的工作原理@ ec9fcc1/lib/npm.js#L254 其中:

path.resolve(process.execPath, '..', '..')

另请参阅 ec9fcc1/lib/install.js#L521

var globalPackage = path.resolve(npm.globalPrefix,
                                 'lib', 'node_modules', moduleName(pkg))

其中 globalPrefixec9fcc1< 中设置了默认值/code>/lib/config/defaults.js#L92-L105的:

if (process.env.PREFIX) {
    globalPrefix = process.env.PREFIX
} else if (process.platform === 'win32') {
    // c:\node\node.exe --> prefix=c:\node\
    globalPrefix = path.dirname(process.execPath)
} else {
    // /usr/local/bin/node --> prefix=/usr/local
    globalPrefix = path.dirname(path.dirname(process.execPath))

    // destdir only is respected on Unix
    if (process.env.DESTDIR) {
        globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
    }
}

If you're trying to access your global dir from code, you can backtrack from process.execPath. For example, to find wsproxy, which is in {NODE_GLOBAL_DIR}/bin/wsproxy, you can just:

path.join(path.dirname(process.execPath), 'wsproxy')

There's also how the npm cli works @ ec9fcc1/lib/npm.js#L254 with:

path.resolve(process.execPath, '..', '..')

See also ec9fcc1/lib/install.js#L521:

var globalPackage = path.resolve(npm.globalPrefix,
                                 'lib', 'node_modules', moduleName(pkg))

Where globalPrefix has a default set in ec9fcc1/lib/config/defaults.js#L92-L105 of:

if (process.env.PREFIX) {
    globalPrefix = process.env.PREFIX
} else if (process.platform === 'win32') {
    // c:\node\node.exe --> prefix=c:\node\
    globalPrefix = path.dirname(process.execPath)
} else {
    // /usr/local/bin/node --> prefix=/usr/local
    globalPrefix = path.dirname(path.dirname(process.execPath))

    // destdir only is respected on Unix
    if (process.env.DESTDIR) {
        globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
    }
}
将军与妓 2024-11-12 21:43:46

如果您安装了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文