在 Ubuntu 16 上使用 n 时 Node Bash 路径混乱

在 Ubuntu 16 上使用 n 时 Node Bash 路径混乱

我尝试安装 n 进行节点版本更新这回答了方向。成功将我升级到 node v7,但有一个副作用。现在找到节点:

/usr/local/bin/node 

如果我输入,node -v我会收到 bash 错误,因为它在错误的地方查找(它查找在 中usr/bin/node)。我的最佳前进路径是什么?如何更改终端查找节点的路径,以便我可以运行,node index.js而不会干扰我将来使用 n 升级节点的愿望。

我不确定这是否相关但是,当我在一个项目中输入时,nodemon index.js一切都按预期工作。我认为这是因为 nodemon 安装在安装 node v7 的地方。

其值如下echo $PATH

    /usr/local/heroku/bin:/home/devin/.rbenv/shims:/home/devin/.rbenv/bin:/usr/bin:/home/devin/bin:/home/devin/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

其值如下which node

/usr/local/bin/node

的值locate node使终端过载并结束如下:

    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/node.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/outer_join.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/over.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/regexp.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/right_outer_join.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/select_core.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/select_statement.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/sql_literal.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/string_join.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/table_alias.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/terminal.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/true.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/unary.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/unary_operation.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/unqualified_column.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/update_statement.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/values.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/window.rb
    /var/lib/gems/2.3.0/gems/arel-7.1.2/lib/arel/nodes/with.rb
    /var/lib/gems/2.3.0/gems/execjs-2.7.0/lib/execjs/support/node_runner.js
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/xml_node.c
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/xml_node.h
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/xml_node.o
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/xml_node_set.c
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/xml_node_set.h
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/xml_node_set.o
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/css/node.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node/save_options.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/pp/node.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/html/test_node.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/html/test_node_encoding.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/node
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_node.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_node_attributes.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_node_encoding.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_node_inheritance.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_node_reparenting.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_node_set.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/test_unparented_node.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/node/test_save_options.rb
    /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/test/xml/node/test_subclass.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/script/tree/node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/at_root_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/charset_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/comment_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/content_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/css_import_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/debug_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/directive_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/each_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/error_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/extend_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/for_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/function_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/if_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/import_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/keyframe_rule_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/media_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/mixin_def_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/mixin_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/prop_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/return_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/root_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/rule_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/supports_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/trace_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/variable_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/warn_node.rb
    /var/lib/gems/2.3.0/gems/sass-3.4.22/lib/sass/tree/while_node.rb

答案1

您需要向环境变量中添加另一个 PATH。

如果这是您第一次这样做,我会尝试解释背后的逻辑。

每当您运行命令时,您的终端 shell 都会尝试运行第一个单词。您的 shell 有一个 PATH 环境变量列表,它将在其中查找您运行的命令(例如node),方法是遍历它所知道的路径列表,直到找到它。您可以通过在终端中输入以下内容来检查您的 PATH 是什么:

echo $PATH

该文件的路径(即/usr/local/bin//usr/local/bin/node可能不在列表中。不同的路径用冒号分隔,如下所示:

/home/user/bin:/home/user/.local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

将文件的路径 ( /usr/local/bin/) 附加到 shell 的 rc 文件中的 PATH 中就足够了。shell rc 文件是一个“runco​​m”文件,其字面意思是在 shell 实例启动时“运行命令”。在 bash 中,它位于~/.bashrc。如果您使用其他文件,则需要查找它是什么,例如对于 zsh 它位于~/.zshrc

打开文件并在类似的行上export PATH="...$PATH"添加一个:/usr/local/bin/到其末尾。

它可能看起来像这样:

export PATH="[other paths]:$PATH:/usr/local/bin"

如果文件中任何地方没有这样的行,只需添加一行:

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

保存 rc 文件并关闭并重新打开终端,您将能够使用 node 作为命令。您还应该能够在运行时看到添加的新路径echo $PATH

相关内容