node 5.5.0 already installed but node -v fetches with “v4.2.1” on OS X & homebrew?

后端 未结 2 1442
囚心锁ツ
囚心锁ツ 2020-11-28 00:17

I\'m trying to update my nodejs v4.2.1 to the latest version and successfully got that. But node -v still gives me \'-v4.2.1\'.

相关标签:
2条回答
  • 2020-11-28 00:29

    Your path contains /usr/local/bin before /usr/local/Cellar. Since the version of node you're using is the homebrew one, fix it with:

    PATH="/usr/local/Cellar:$PATH"
    

    Next time you have a problem with brew, run brew doctor, it would have told you what to do

    0 讨论(0)
  • 2020-11-28 00:37

    Probably the older Node is in your PATH before the newer one. You can run in your shell:

    which node
    

    to see where is the Node binary that is run by default (v4.2.1 in your case). You can see what is your PATH by running:

    echo $PATH
    

    It will show something like:

    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    

    Those are directories that are searched in order whenever you type "node" or any other command. If your PATH doesn't have the directory where you have your new Node installed, or if it is after the directory where you have your old Node, then the new Node will not be run. Fixing the problem may be as simple as running:

    PATH="/usr/local/bin:$PATH"
    

    if your new Node is installed in /usr/local/bin (or with some other path if it is installed somewhere else). You need to add this line in .profile or .bashrc in your HOME to have the PATH set up correctly every time you log in start a new shell.

    To see if you have the correct Node version in /usr/local/bin run:

    /usr/local/bin/node -v
    

    Update

    Looking at your comment and updated answer my bet would be that you have installed Node 4.2.1 manually (not with brew) and now brew doesn't update the binary in /usr/local/bin.

    What I would recommend is to install it manually and have control over the versions. I'll show you the commands to download the source, configure, build and install in a versioned directory, and update the PATH.

    According to the Node download page the current version is v6.1.0 but if you want specifically 5.5.0 or any other version (the latest 5.x is v5.9.1) then just change the commands below to the verson that you want. (All versions are listed here.)

    # change dir to your home:
    cd ~
    # download the source:
    curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
    # extract the archive:
    tar xzvf node-v6.1.0.tar.gz
    # go into the extracted dir:
    cd node-v6.1.0
    # configure for installation:
    ./configure --prefix=/opt/node-v6.1.0
    # build and test:
    make && make test
    # install:
    sudo make install
    # make a symlink to that version:
    sudo ln -svf /opt/node-v6.1.0 /opt/node
    

    and finally add PATH="/opt/node/bin:$PATH" to your .profile or .bashrc (The node-v6.1.0 directory in your HOME and the .tar.gz can be removed or kept for later use).

    At this point which node should return /opt/node/bin/node and the version is the one that you want (6.1.0 in this example). If you want to test another version then install it in another directory under /opt/node-vXXX and update the symlink. You won't have to update PATH, just the symlink.

    This is more work than with brew but you have total control over what gets installed and where. What I like about this solution is that your versions never get mixed or confused and you can install/remove versions any time and quickly set any version as default.

    0 讨论(0)
提交回复
热议问题