node.js 如何 npm install 到指定目录?

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

How to npm install to a specified directory?

node.jsnpm

提问by coudy

Is it possible to specify a target directory when running npm install <package>?

运行时是否可以指定目标目录npm install <package>

回答by coudy

You can use the --prefixoption:

您可以使用以下--prefix选项:

mkdir -p ./install/here/node_modules
npm install --prefix ./install/here <package>

The package(s) will then be installed in ./install/here/node_modules. The mkdiris needed since npm might otherwise choose an already existing node_modulesdirectory higher up in the hierarchy. (See npm documentation on folders.)

软件包将安装在./install/here/node_modules. 这mkdir是必需的,因为 npm 可能会选择node_modules层次结构中更高的现有目录。(请参阅有关文件夹的 npm 文档。)

回答by yunzen

In the documentationit's stated: Use the prefix option together with the global option:

文档中指出:将前缀选项与全局选项一起使用

The prefixconfig defaults to the location where node is installed. On most systems, this is /usr/local. On windows, this is the exact location of the node.exe binary. On Unix systems, it's one level up, since node is typically installed at {prefix}/bin/noderather than {prefix}/node.exe.

When the globalflag is set, npm installs things into this prefix. When it is not set, it uses the root of the current package, or the current working directory if not in a package already.

前缀配置的默认值到安装节点的位置。在大多数系统上,这是/usr/local。在 Windows 上,这是 node.exe 二进制文件的确切位置。在 Unix 系统上,它是上一级,因为 node 通常安装在{prefix}/bin/node而不是 {prefix}/node.exe

全球标志设置,NPM安装的东西到这个前缀。如果未设置,则使用当前包的根目录,如果不在包中,则使用当前工作目录。

(Emphasis by them)

(他们强调)

So in your root directory you could install with

所以在你的根目录中你可以安装

npm install --prefix <path/to/prefix_folder> -g

and it will install the node_modulesfolder into the folder

它会将node_modules文件夹安装到文件夹中

<path/to/prefix_folder>/lib/node_modules

回答by Rohit Sharma

As of npm version 3.8.6, you can use

从 npm 3.8.6 版开始,您可以使用

npm install --prefix ./install/here <package>

to install in the specified directory. NPM automatically creates node_modulesfolder even when a node_modulesdirectory already exists in the higher up hierarchy. You can also have a package.jsonin the current directory and then install it in the specified directory using --prefixoption:

安装到指定目录。node_modules即使node_modules目录已经存在于更高的层次结构中,NPM也会自动创建文件夹。您也可以package.json在当前目录中有一个,然后使用--prefix选项将其安装在指定目录中:

npm install --prefix ./install/here

As of npm 6.0.0, you can use

从 npm 6.0.0 开始,您可以使用

npm install --prefix ./install/here ./

to install the package.json in current directory to "./install/here" directory. There is one thing that I have noticed on Mac that it creates a symlink to parent folder inside the node_modules directory. But, it still works.

将当前目录中的 package.json 安装到“./install/here”目录。我在 Mac 上注意到一件事,它在 node_modules 目录中创建了一个指向父文件夹的符号链接。但是,它仍然有效。

NOTE: NPM honours the path that you've specified through the --prefixoption. It resolves as per npm documentation on folders, only when npm installis used without the --prefixoption.

注意:NPM 遵循您通过--prefix选项指定的路径。它按照文件夹上的 npm 文档解析,仅在npm install不带--prefix选项的情况下使用。

回答by Malachy

I am using a powershell build and couldn't get npm to run without changing the current directory.

我正在使用 powershell 构建并且无法在不更改当前目录的情况下运行 npm。

Ended up using the start command and just specifying the working directory:

最终使用 start 命令并仅指定工作目录:

start "npm" -ArgumentList "install --warn" -wo $buildFolder