当前目录中的npm install模块 [英] npm install module in current directory

查看:256
本文介绍了当前目录中的npm install模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我跑步时:

npm install my-app

该应用已安装到node_modules/my-app/...

The app is installed into node_modules/my-app/...

我也尝试过

npm install -g my-app

但这也不起作用.

如何将模块直接安装到当前目录中?

How can I install the module straight into the current directory?

推荐答案

npm install安装软件包

  • 本地:npm在当前目录中查找一个名为node_modules的现有文件夹,并为该文件夹中安装的每个软件包创建一个文件夹.如果在这里找不到现有的node_modules文件夹,则它将浏览当前目录的祖先,直到找到一个.如果找不到,则会在当前目录中创建一个.
  • 全局:如果使用-g(全局)选项,则程序包将安装在全局位置.该位置因Linux发行版而异,但是/usr/local/lib/node_modules/packagename是一个示例. CentOS7使用/usr/lib/node_modules/packagename.
    • Locally: npm looks for an existing folder called node_modules in the current directory and creates a folder for each package you install in that folder. If it can't find an existing node_modules folder here, it then looks through the current directory's ancestors until it finds one. If it can't find one, it creates one in the current directory.
    • Globally: if you use the -g (global) option, the package is installed in a global location. This location varies per Linux distribution, but /usr/local/lib/node_modules/packagename is one example. CentOS7 uses /usr/lib/node_modules/packagename.

    仅当要使用该软件包作为命令时,才应使用-g.

    You should only use -g when the package is something you'd want to use as a command.

    就像全局变量如何是总值一样,但也是必需的 在某些情况下,全局软件包很重要,但最好避免使用 需要.

    Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.

    通常,经验法则是:

    1. 如果您要安装要在程序中使用的内容, 使用require('whatever'),然后将其安装在本地 您的项目.
    2. 如果您要安装要使用的产品 您的 shell ,在命令行或其他方式上进行全局安装,因此 它的二进制文件最终出现在您的PATH环境变量中.
    1. If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
    2. If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

    npm不会将软件包的文件直接安装到当前目录中.

    npm will not install a package's files directly into the current directory.

    但是,这实际上是一件好事.它将依赖项的文件与您的应用程序分开,并且当您node_modules文件夹. >某物.

    However, this is actually a good thing. It keeps dependencies' files separate from your app, and Node automatically searches the node_modules folder when you require something.

    这篇关于当前目录中的npm install模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆