node.js 当前目录下的 npm install 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14032160/
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-09-02 16:49:42  来源:igfitidea点击:

npm install module in current directory

node.jsnpm

提问by ?kiz Kulelere Dalan Teyyare

When I run:

当我运行时:

npm install my-app

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

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

I also tried

我也试过

npm install -g my-app

But that doesn't work either.

但这也行不通。

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

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

回答by josh3736

npm installinstalls packages either locally or globally:

npm install本地或全局安装软件包:

  • Locally: npmlooks for an existing folder called node_modulesin the current directory and creates a folder for each package you install in that folder. If it can't find an existing node_modulesfolder 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/packagenameis one example. CentOS7 uses /usr/lib/node_modules/packagename.
  • 本地npm查找node_modules在当前目录中调用的现有文件夹,并为您在该文件夹中安装的每个包创建一个文件夹。如果node_modules在此处找不到现有文件夹,则它会查找当前目录的祖先,直到找到一个。如果找不到,则在当前目录中创建一个。
  • Globally:如果您使用-g(global) 选项,则软件包将安装在全局位置。此位置因 Linux 发行版而异,但 /usr/local/lib/node_modules/packagename只是一个示例。CentOS7 使用/usr/lib/node_modules/packagename.

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

您应该只-g在包是您想要用作命令的东西时使用。

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.

In general, the rule of thumb is:

  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 PATHenvironment variable.

就像全局变量有点粗糙,但在某些情况下也是必要的一样,全局包很重要,但如果不需要,最好避免。

一般来说,经验法则是:

  1. 如果您要安装要在程序中使用的东西,请使用require('whatever'),然后将其安装在本地项目的根目录下。
  2. 如果您要在shell 中、命令行或其他地方安装要使用的内容,请全局安装它,以便其二进制文件最终出现在您的PATH环境变量中。

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

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

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

然而,这实际上是一件好事。它使依赖文件从您的应用程序分离,节点自动搜索node_modules,当文件夹中require的东西

回答by achedeuzot

As @dalu said, if you want to have local packages, you'll need a package.jsonfile.

正如@dalu 所说,如果你想拥有本地包,你需要一个package.json文件。

But to create the package.jsonfile, you will have to initialize npmby running npm init.

但是要创建package.json文件,您必须通过运行来初始化 npmnpm init

You can then use npm install <module_name> --save[-dev].

然后您可以使用npm install <module_name> --save[-dev].

回答by johnpolacek

This is how you can install a module to your current directory:

这是将模块安装到当前目录的方法:

npm i --prefix ./ my-app

npm i --prefix ./ my-app

As others have said, the proper way is to configure your project via package.json

正如其他人所说,正确的方法是通过配置您的项目 package.json

回答by johnpolacek

You ought to have a package.jsonin your current directory.

您应该package.json在当前目录中有一个。

Then write npm install <module_name> --save-devor npm install <module_name> --saveand it will install to the node_modules directory

然后写npm install <module_name> --save-devnpm install <module_name> --save它会安装到 node_modules 目录

回答by Lafras Henning

I think the real question, what I and the OP would want, is to install my-app, like you would install an application , i.e. Install a top level application, that I am going to "use" as an application and not "require" as a module.

我认为真正的问题,我和 OP 想要的是安装我的应用程序,就像你安装一个应用程序一样,即安装一个顶级应用程序,我将“使用”作为一个应用程序而不是“需要” "作为一个模块。

The fact that npm installs one level down from my application directory, is a purely aesthetic objection by new npm users.

npm 安装在我的应用程序目录下一层的事实,纯粹是新 npm 用户的审美反对。

When I started using npm (not so long ago), I solved it by having a git project as an installer, clone the git, run the install script, but now I am used to it and it does not bother me to have the app in the "wrong" folder any more.

当我开始使用 npm 时(不久前),我通过将 git 项目作为安装程序来解决它,克隆 git,运行安装脚本,但现在我已经习惯了它并没有打扰我拥有该应用程序在“错误”文件夹中。

Just setup some .sh, .bat or short cuts in the right place and your users, won't notice.

只需在正确的位置设置一些 .sh、.bat 或快捷方式,您的用户就不会注意到。

回答by user2414115

Using Windows 7. I had installed nodejs in a different folder (c:\develop\nodejs) to the one that was suggested while installing (C:\Program Files\nodejs) I am totally new to nodejs or npm. i may not be talking the nodejs or npm language. After installing nodejs. I had to do some commands like this in the main nodejs folder

使用 Windows 7。我将 nodejs 安装在不同的文件夹(c:\develop\nodejs)中,安装时建议的文件夹(C:\Program Files\nodejs)我对 nodejs 或 npm 完全陌生。我可能不是在谈论 nodejs 或 npm 语言。安装 nodejs 后。我必须在主 nodejs 文件夹中执行一些这样的命令

npm init

then I was going to create a local directory (say c:\develop\nodejs\ upload2s3), and create a nodejs package to use as AWS lambda. For this, i went into the local directory (upload2s3), and tried

然后我将创建一个本地目录(比如 c:\develop\nodejs\ upload2s3),并创建一个 nodejs 包以用作 AWS lambda。为此,我进入本地目录(upload2s3),并尝试

npm install aws-sdk

though the command behaved like it installed things, this did not do anything in my upload2s3folder as it was empty. then i just tried this in my local folder.

尽管该命令的行为就像安装了东西,但这并没有在我的upload2s3文件夹中执行任何操作,因为它是空的。然后我只是在我的本地文件夹中尝试了这个。

npm init 

this created some package.json, and may be few other files then i tried

这创建了一些package.json,可能还有一些其他文件,然后我尝试了

npm install aws-sdk

then it created a node-modulesfolders inside my local folder (upload2s3) and installed the aws-sdkpackage. It also updated the package.json file

然后它在我的本地文件夹(upload2s3)中创建了一个node-modules文件夹并安装了aws-sdk包。它还更新了 package.json 文件

{
  "name": "uploadtos32",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.448.0"
  }
}

回答by dee

pack=$(npm pack <package>) ; tar xzvf ${pack} --strip-components=1 ; rm ${pack} ; npm i