npm安装模块在当前目录中

时间:2021-06-14 23:18:26

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?

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

5 个解决方案

#1


35  

npm install installs packages either locally or globally:

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

  • 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.
  • 本地:npm在当前目录中查找名为node_modules的现有文件夹,并为您在该文件夹中安装的每个包创建一个文件夹。如果它在这里找不到现有的node_modules文件夹,那么它会查看当前目录的祖先,直到找到它为止。如果找不到,则在当前目录中创建一个。
  • 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(全局)选项,则程序包将安装在全局位置。此位置因Linux发行版而异,但/ usr / local / lib / node_modules / packagename是一个示例。 CentOS7使用/ usr / lib / node_modules / packagename。

You should only use -g when 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. 如果您要在程序中安装要使用的东西,请使用require('whatever'),然后在项目的根目录下在本地安装。
  3. 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.
  4. 如果您要在shell中安装要在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_modules folder when you require something.

然而,这实际上是一件好事。它将依赖项的文件与您的应用程序分开,并在您需要时自动搜索node_modules文件夹。

#2


17  

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

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

But to create the package.json file, you will have to initialize npm by running npm init.

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

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

然后,您可以使用npm install --save [-dev]。

#3


4  

You ought to have a package.json in your current directory.

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

Then write npm install <module_name> --save-dev or npm install <module_name> --save and it will install to the node_modules directory

然后编写npm install --save-dev或npm install --save,它将安装到node_modules目录

#4


4  

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配置项目

#5


3  

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或捷径,您的用户就不会注意到。

#1


35  

npm install installs packages either locally or globally:

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

  • 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.
  • 本地:npm在当前目录中查找名为node_modules的现有文件夹,并为您在该文件夹中安装的每个包创建一个文件夹。如果它在这里找不到现有的node_modules文件夹,那么它会查看当前目录的祖先,直到找到它为止。如果找不到,则在当前目录中创建一个。
  • 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(全局)选项,则程序包将安装在全局位置。此位置因Linux发行版而异,但/ usr / local / lib / node_modules / packagename是一个示例。 CentOS7使用/ usr / lib / node_modules / packagename。

You should only use -g when 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. 如果您要在程序中安装要使用的东西,请使用require('whatever'),然后在项目的根目录下在本地安装。
  3. 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.
  4. 如果您要在shell中安装要在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_modules folder when you require something.

然而,这实际上是一件好事。它将依赖项的文件与您的应用程序分开,并在您需要时自动搜索node_modules文件夹。

#2


17  

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

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

But to create the package.json file, you will have to initialize npm by running npm init.

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

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

然后,您可以使用npm install --save [-dev]。

#3


4  

You ought to have a package.json in your current directory.

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

Then write npm install <module_name> --save-dev or npm install <module_name> --save and it will install to the node_modules directory

然后编写npm install --save-dev或npm install --save,它将安装到node_modules目录

#4


4  

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配置项目

#5


3  

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或捷径,您的用户就不会注意到。