在 OS X 上安装 brew、node.js、io.js、nvm、npm 的建议方法是什么?

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

What is the suggested way to install brew, node.js, io.js, nvm, npm on OS X?

node.jsmacosnpmhomebrewio.js

提问by ohho

I am trying to use homebrew as much as possible. What's the suggested way to install the following on OS X?

我正在尝试尽可能多地使用自制软件。在 OS X 上安装以下内容的建议方法是什么?

and hopefully supports development for:

并希望支持以下方面的开发:

回答by alexpods

  1. Using homebrewinstall nvm:

    brew update
    brew install nvm
    source $(brew --prefix nvm)/nvm.sh
    

    Add the last command to the .profile, .bashrcor .zshrcfile to not run it again on every terminal start. So for example to add it to the .profilerun:

    echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile
    

    If you have trouble with installing nvmusing brewyou can install it manually (see here)

  2. Using nvminstall nodeor iojs(you can install any version you want):

    nvm install 0.10
    # or
    nvm install iojs-1.2.0
    
  3. npmis shipping with node(or iojs), so it will be available after installing node(or iojs). You may want to upgrade it to the latest version:

    $ npm install -g npm@latest
    

    UPDPrevious version was npm update -g npm. Thanks to @Metallica for pointing to the correct way (look at the comment bellow).

  4. Using npminstall ionic:

    npm install -g ionic
    
  5. What about ngCordova: you can install it using npmor bower. I don't know what variant is more fit for you, it depends on the package manager you want to use for the client side. So I'll describe them both:

    1. Using npm: Go to your project folder and install ng-cordovain it:

      npm install --save ng-cordova
      
    2. Using bower: Install bower:

       npm install -g bower
      

      And then go to your project folder and install ngCordovain it:

       bower install --save ngCordova
      
  1. 使用homebrew安装nvm

    brew update
    brew install nvm
    source $(brew --prefix nvm)/nvm.sh
    

    将最后一个命令添加到.profile,.bashrc.zshrc文件,以免在每次终端启动时再次运行它。例如,将其添加到.profile运行中:

    echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile
    

    如果你有安装麻烦的nvm使用brew,你可以手动安装(见这里

  2. 使用nvminstall nodeor iojs(你可以安装任何你想要的版本):

    nvm install 0.10
    # or
    nvm install iojs-1.2.0
    
  3. npmnode(或iojs)一起发货,因此在安装node(或iojs) 后即可使用。您可能希望将其升级到最新版本:

    $ npm install -g npm@latest
    

    UPD以前的版本是npm update -g npm. 感谢@Metallica 指出正确的方法(查看下面的评论)。

  4. 使用npm安装ionic

    npm install -g ionic
    
  5. 怎么样ngCordova:您可以使用npm或安装它bower。我不知道哪种变体更适合您,这取决于您要用于客户端的包管理器。所以我将描述它们:

    1. 使用npm:转到您的项目文件夹并ng-cordova在其中安装:

      npm install --save ng-cordova
      
    2. 使用bower:安装凉亭:

       npm install -g bower
      

      然后转到您的项目文件夹并ngCordova在其中安装:

       bower install --save ngCordova
      

PS

聚苯乙烯

  1. Some commands may require superuser privilege
  2. Short variant of npm install some_moduleis npm i some_module
  1. 某些命令可能需要超级用户权限
  2. npm install some_moduleis 的简短变体npm i some_module

回答by Abhijit Mazumder

2019 update: Use NVM to install node, not Homebrew

2019 更新:使用 NVM 安装节点,而不是 Homebrew

In most of the answers , recommended way to install nvm is to use Homebrew

在大多数答案中,推荐的安装 nvm 的方法是使用Homebrew

Do not do that

不要那样做

At Github Pagefor nvm it is clearly called out:

在nvm 的Github 页面上,它清楚地标明:

Homebrew installation is not supported. If you have issues with homebrew-installed nvm, please brew uninstall it, and install it using the instructions below, before filing an issue.

不支持 Homebrew 安装。如果您对 homebrew 安装的 nvm 有问题,请在提交问题之前 brew uninstall 它,并使用下面的说明安装它。

Use the following method instead

改用下面的方法

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

该脚本将 nvm 存储库克隆到 ~/.nvm 并将源代码行添加到您的配置文件(~/.bash_profile、~/.zshrc、~/.profile 或 ~/.bashrc)。

And then use nvm to install node. For example to install latest LTS version do:

然后使用 nvm 安装节点。例如要安装最新的 LTS 版本,请执行以下操作:

nvm install v8.11.1

Clean and hassle free. It would mark this as your default node version as well so you should be all set

干净又省心。它也会将此标记为您的默认节点版本,因此您应该全部设置

回答by Serge Seletskyy

I'm using n (Node version management)

我正在使用 n(节点版本管理)

You can install it in two ways

您可以通过两种方式安装它

brew install n

or

或者

npm install -g n

You can switch between different version of node and io. Here's an example from my current env when I call n without params:

您可以在不同版本的 node 和 io 之间切换。这是我当前环境中的一个示例,当我在没有参数的情况下调用 n 时:

$ n

  io/3.3.1
  node/0.12.7
  node/4.0.0
  node/5.0.0
ο node/5.10.1 

回答by garrettmac

I'm super late to this but I didn't like the other answers

我太晚了,但我不喜欢其他答案

Installing Homebrew

安装 Homebrew

For brewrun

对于 brew运行

"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing node & npm

安装节点和 npm

You SHOULD NOTuse brewto install nodeand npm.

不应该使用brew安装故宫

I've seen a few places suggested that you should use Homebrew to install Node (like alexpods answer and in this Team Treehouse blog Post) but installing this way you're more prone to run into issues as npmand breware both package managersand you should have a package managermanage another package managerthis leads to problems, like this bug offical npmissues Error: Refusing to delete: /usr/local/bin/npmor this Can't uninstall npm module on OSX

我已经看到一些地方建议您应该使用 Homebrew 来安装 Node(如 alexpods 的回答和此Team Treehouse 博客帖子中的答案),但是以这种方式安装您更容易遇到问题,因为npm并且brew都是包管理器,您应该让一个包管理器管理另一个包管理器,这会导致问题,比如这个 bug 官方npm问题错误:拒绝删除:/usr/local/bin/npm无法在 OSX 上卸载 npm 模块

You can read more on the topic in DanHerbert's post Fixing npm On Mac OS X for Homebrew Users, where he goes on to say

您可以在 DanHerbert 的文章Fixing npm On Mac OS X for Homebrew Users 中阅读有关该主题的更多信息 ,他继续说

Also, using the Homebrew installation of npm will require you to use sudo when installing global packages. Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.

此外,使用 npm 的 Homebrew 安装将要求您在安装全局包时使用 sudo。由于 Homebrew 背后的核心思想之一是可以在不授予 root 访问权限的情况下安装应用程序,因此这是一个坏主意。

For Everything else

对于其他一切

I'd use npm; but you really should just follow the install instruction for each modules following the directions on there website as they will be more aware of any issue or bug they have than anyone else

我会使用 npm;但是您真的应该按照网站上的说明按照每个模块的安装说明进行操作,因为他们会比其他任何人都更了解他们遇到的任何问题或错误

回答by l3x

If you have previously installed node using brew, then you will have a bunch of extra files that you should clean up before installing node "the right way". Plus, I had to add a few settings to my startup script to make things work smoothly.

如果您之前使用 brew 安装了 node,那么您将有一堆额外的文件,您应该在“以正确的方式”安装 node 之前清理这些文件。另外,我必须在我的启动脚本中添加一些设置才能使事情顺利进行。

I wrote a script to make this easy.

我写了一个脚本来简化这个过程。

# filename:  install-nvm-npm-node
# author:    Lex Sheehan
# purpose:   To cleanly install NVM, NODE and NPM
# dependencies:  brew

NOW=$(date +%x\ %H:%M:%S)
CR=$'\n'
REV=$(tput rev)
OFF=$(tput sgr0)
BACKUP_DIR=$HOME/backups/nvm-npm-bower-caches/$NOW
MY_NAME=$(basename 
nvm install iojs-v1.7.1
) NODE_VER_TO_INSTALL= if [ "$NODE_VER_TO_INSTALL" == "" ]; then NODE_VER_TO_INSTALL=v0.12.2 fi if [ "`echo "$NODE_VER_TO_INSTALL" | cut -c1-1`" != "v" ]; then echo """$CR""Usage: $ $MY_NAME <NODE_VERSION_TO_INSALL>" echo "Example: $ $MY_NAME v0.12.1" echo "Example: $ $MY_NAME $CR" exit 1 fi echo """$CR""First, run: $ brew update" echo "Likely, you'll need to do what it suggests." echo "Likely, you'll need to run: $ brew update$CR" echo "To install latest node version, run the following command to get the latest version: $ nvm ls-remote" echo "... and pass the version number you want as the only param to $MY_NAME. $CR" echo "Are you ready to install the latest version of nvm and npm and node version $NODE_VER_TO_INSTALL ?$CR" echo "Press CTL+C to exit --or-- Enter to continue..." read x echo """$REV""Uninstalling nvm...$CR$OFF" # Making backups, but in all likelyhood you'll just reinstall them (and won't need these backups) if [ ! -d "$BACKUP_DIR" ]; then echo "Creating directory to store $HOME/.nvm .npm and .bower cache backups: $BACKUP_DIR" mkdir -p $BACKUP_DIR fi set -x mv $HOME/.nvm $BACKUP_DIR 2>/dev/null mv $HOME/.npm $BACKUP_DIR 2>/dev/null mv $HOME/.bower $BACKUP_DIR 2>/dev/null { set +x; } &>/dev/null echo "$REV""$CR""Uninstalling node...$CR$OFF" echo "Enter your password to remove user some node-related /usr/local directories" set -x sudo rm -rf /usr/local/lib/node_modules rm -rf /usr/local/lib/node rm -rf /usr/local/include/node rm -rf /usr/local/include/node_modules rm /usr/local/bin/npm rm /usr/local/lib/dtrace/node.d rm -rf $HOME/.node rm -rf $HOME/.node-gyp rm /opt/local/bin/node rm /opt/local/include/node rm -rf /opt/local/lib/node_modules rm -rf /usr/local/Cellar/nvm brew uninstall node 2>/dev/null { set +x; } &>/dev/null echo "$REV""$CR""Installing nvm...$CR$OFF" echo "++brew install nvm" brew install nvm echo '$(brew --prefix nvm)/nvm.sh' source $(brew --prefix nvm)/nvm.sh echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc):$CR$OFF" echo "export NVM_DIR=\"$(brew --prefix nvm)\"; [ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\"$CR" NVM_DIR="$(brew --prefix nvm)" echo """$CR""Using nvm install node...$CR" echo "++ nvm install $NODE_VER_TO_INSTALL" nvm install $NODE_VER_TO_INSTALL NODE_BINARY_PATH="`find /usr/local/Cellar/nvm -name node -type d|head -n 1`/$NODE_VER_TO_INSTALL/bin" echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc) and then restart your shell:$CR$OFF" echo "export PATH=$PATH:$NODE_BINARY_PATH:$HOME/.node/bin" echo """$CR""Upgrading npm...$CR" echo '++ install -g npm@latest' npm install -g npm@latest { set +x; } &>/dev/null echo "$REV""$CR""Insert following line in your $HOME/.npmrc file:$OFF" echo """$CR""prefix=$HOME/.node$CR" echo "Now, all is likley well if you can run the following without errors: npm install -g grunt-cli$CR" echo "Other recommended global installs: bower, gulp, yo, node-inspector$CR"

I wrote a short article herethat details why this is "the right way".

在这里写了一篇简短的文章,详细说明了为什么这是“正确的方法”。

If you need to install iojs, do so using nvm like this:

如果您需要安装 iojs,请像这样使用 nvm:

curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash
cd / && . ~/.nvm/nvm.sh && nvm install 0.10.35
. ~/.nvm/nvm.sh && nvm alias default 0.10.35

To install brew, just see its home page.

要安装 brew,只需查看其主页

See alexpods answer for the rest.

其余的请参阅 alexpods 答案。

回答by Rajan Patel

You should install node.js with nvm, because that way you do not have to provide superuser privileges when installing global packages (you can simply execute "npm install -g packagename" without prepending 'sudo').

你应该使用 nvm 安装 node.js,因为这样你在安装全局包时不必提供超级用户权限(你可以简单地执行“npm install -g packagename”而不在前面加上“sudo”)。

Brew is fantastic for other things, however. I tend to be biased towards Bower whenever I have the option to install something with Bower.

然而,Brew 在其他方面也很棒。每当我可以选择使用 Bower 安装某些东西时,我都会倾向于使用 Bower。

回答by Sam Mikes

I agree with noa -- if you need to have multiple versions of node, io.jsthen brew is not the appropriate solution.

我同意 noa - 如果您需要有多个版本的nodeio.js那么 brew 不是合适的解决方案。

You can help beta-test io.jssupport in nvm: https://github.com/creationix/nvm/pull/616

您可以帮助io.jsnvm 中的beta 测试支持:https: //github.com/creationix/nvm/pull/616

If you just want io.jsand are not switching versions, then you can install the binary distribution of io.jsfrom https://iojs.org/dist/v1.0.2/iojs-v1.0.2-darwin-x64.tar.gz; that includes npmand you will not need nvmif you are not switching versions.

如果您只是想要io.js而不是切换版本,那么您可以安装io.js来自https://iojs.org/dist/v1.0.2/iojs-v1.0.2-darwin-x64.tar.gz的二进制分发版;这包括npmnvm如果您不切换版本,则不需要。

Remember to update npmafter installing: sudo npm install -g npm@latest

npm安装后记得更新:sudo npm install -g npm@latest

回答by paulmelnikow

Here's what I do:

这是我所做的:

brew install nvm

No Homebrew for this one.

这个没有自制软件。

nvmsoon will support io.js, but not at time of posting: https://github.com/creationix/nvm/issues/590

nvm很快将支持 io.js,但在发布时不支持:https: //github.com/creationix/nvm/issues/590

Then install everything else, per-project, with a package.jsonand npm install.

然后安装其他的一切,每个项目,具有package.jsonnpm install

回答by David Dehghan

For install with zsh and Homebrew:

使用 zsh 和 Homebrew 安装:

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

Then Add the following to ~/.zshrc or your desired shell configuration file:

然后将以下内容添加到 ~/.zshrc 或您想要的 shell 配置文件:

nvm install 7.10.1
nvm use 7.10.1

Then install a node version and use it.

然后安装一个节点版本并使用它。

##代码##