node.js 有虚拟环境吗?

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

is there an virtual environment for node.js?

node.jsvirtualenvlanguage-comparisons

提问by Frangossauro

I've searched the wiki modules page, but I can't find anything similar to virtualenv (python) or rvm.

我搜索了 wiki 模块页面,但找不到任何类似于 virtualenv (python) 或 rvm 的内容。

Anyone here separates node.js in their own env? I really don't like to install npm system-wide.

这里有人在他们自己的环境中分离 node.js 吗?我真的不喜欢在系统范围内安装 npm。

采纳答案by bxjx

If having system wide npm packages is your main issue, then maybe consider using the very cool 'bundle' command with npm. This is closer to freezing gems or using bundler in rails, rather than rvm.

如果拥有系统范围的 npm 包是您的主要问题,那么也许可以考虑将非常酷的“bundle”命令与 npm 结合使用。这更接近于冻结 gems 或在 rails 中使用 bundler,而不是 rvm。

It's super easy. Just create a package.json file:

这非常容易。只需创建一个 package.json 文件:

{ "name": "yourapp", "version": "0.0.1", "dependencies": {"jade": "0.4.1"}}

and then run:

然后运行:

npm bundle vendor

or if your npm version is >= 1.0 run:

或者如果您的 npm 版本 >= 1.0 运行:

npm install

to freeze into the vendor directory. and then use:

冻结到供应商目录中。然后使用:

require.paths.unshift('./vendor');

回答by shorrty

nodeenv- virtual environment for node.js ( Analog virtualenv)

nodeenv- node.js 的虚拟环境(模拟virtualenv

回答by Epeli

There are also some Node version management systems that can help.

还有一些 Node 版本管理系统可以提供帮助。

Check out Nave https://github.com/isaacs/nave

查看 Nave https://github.com/isaacs/nave

NVM could also be used https://github.com/creationix/nvm

NVM 也可以使用https://github.com/creationix/nvm

There is also one called n https://github.com/visionmedia/n

还有一个叫n https://github.com/visionmedia/n

回答by isaacs

bxjx's answer is conceptually accurate. However, please note that the bundle command no longer takes a directory. It alwaysdrops packages into the node_modulesfolder, so that npm knows where to find them later (and can avoid double-installing dependencies).

bxjx 的答案在概念上是准确的。但是,请注意 bundle 命令不再需要目录。它总是将包放入node_modules文件夹中,以便 npm 知道以后在哪里可以找到它们(并且可以避免重复安装依赖项)。

Any solution will probably involve installing npm and nave "system-wide" (that is, in your PATH, which could be in ~somewhere), but then only installing your specific dependenciesin the virtual environment.

任何解决方案可能会涉及到安装NPM和中堂“全系统”(也就是,在你的PATH,这可能是在~某个地方),但后来只有在安装特定的相关虚拟环境。

I responded more thoroughly on the github issue.

在 github 问题上回答得更彻底。

tl;dr: The use case is valid and helpful, and while it's mostly there, it's not as well served as it could be. We should make sure to think it through and do it right.

tl;dr:用例是有效且有用的,虽然它大部分都在那里,但它并没有达到应有的水平。我们应该确保仔细考虑并正确地做。

回答by gitaarik

You don't always need to install dependencies globally. Usually it's recommended because then you can use the commands an npm packages provides, but if you install it locally (in the node_modules) directory, you can also use these commands, they only wind up in the node_modules/.bin/directory, so you'll have to type node_modules/.bin/<command>, which is annoying, but you can of course add this path to your PATHenvironment variable:

您并不总是需要全局安装依赖项。通常建议使用,因为这样您就可以使用 npm 包提供的命令,但是如果您将其安装在本地(在node_modules)目录中,您也可以使用这些命令,它们只会出现在node_modules/.bin/目录中,因此您必须键入node_modules/.bin/<command>,这很烦人,但您当然可以将此路径添加到您的PATH环境变量中:

export PATH=node_modules/.bin:$PATH

Then you can just type <command>and it works!

然后你就可以打字<command>,它的工作原理!

There's actually an npm command that returns an absolute path to the .bindirectory:

实际上有一个 npm 命令返回.bin目录的绝对路径:

$ npm bin
/path/to/node_modules/.bin

This command also works when you're in a subdirectory of the project, it will return the first node_modules/.bindirectory it finds in it's parent directories.

当您位于项目的子目录中时,此命令也有效,它将返回node_modules/.bin它在其父目录中找到的第一个目录。

You can add this alias in your .bashrcto automatically add the .bin/directory to your PATH:

您可以在您的中添加此别名.bashrc以自动将.bin/目录添加到您的PATH

alias nodebin='export PATH=$(npm bin):$PATH'

So when you're in a directory of a project that has a node_modules/directory in the root, you can type nodebinand then you can use all the commands that are in the .bin/directory!

所以当你在一个根目录有目录的项目的node_modules/目录中时,你可以输入nodebin,然后你就可以使用目录中的所有命令.bin/

回答by Gohan

looks there is a better way:

看起来有更好的方法:

Installing Node.js and npm into a Python Virtualenv

将 Node.js 和 npm 安装到 Python Virtualenv 中

now I can use node tools without mess the global bin environment

现在我可以使用节点工具而不会弄乱全局 bin 环境

回答by Lars C. Magnusson

If you like it simple, I truely recommend visionmedia's n, could not be easier!

如果你喜欢简单,我真的推荐visionmedia的n,再简单不过了!

https://github.com/visionmedia/n

https://github.com/visionmedia/n