【问题标题】:NodeJS - how to make version 6.9.1 permanent?NodeJS - 如何使版本 6.9.1 永久化?
【发布时间】:2018-06-18 14:39:30
【问题描述】:

我的 CentOS 7.4 上有节点 v6.12.2。但我必须保留 v6.9.1,所以当我遵循它时它可以工作,但在重新启动或注销后它不会保留。

sudo yum install epel-release
sudo yum install nodejs
sudo yum install npm
npm install forever -g
curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash
source ~/.bash_profile
nvm list-remote
nvm install v6.9.1
# nvm use 6.9.1
Now using node v6.9.1

【问题讨论】:

  • 我引用了服务器上每个节点版本的路径(因为我的几个应用程序在不同的节点版本上运行)。例如/home/asykes/.nvm/versions/node/v6.9.1/bin/node。不过,如果您的所有应用程序都在同一版本上运行,我想这很冗长。
  • 或者只是运行nvm alias default 6.9.1 - 简单一点;)

标签: node.js npm centos


【解决方案1】:

有效! CentOS 7.4

第一步

从 NEW 回到 OLD。

curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash
source ~/.bash_profile
nvm list-remote
nvm install v6.7.0
nvm use 6.7.0
Now using node v6.7.0
nvm alias default 6.7.0
whereis node
node: /usr/bin/node /usr/share/node /root/.nvm/v6.7.0/bin/node /usr/share/man/man1/node.1.gz
/root/.nvm/v6.7.0/bin/node --version
v6.7.0

第 2 步

作为服务运行。

cat /etc/systemd/system/node-server1.service
[Unit]
Description=Node-Server1
#Requires=After=mysql.service       # Requires the mysql service to run first

[Service]
ExecStart=/root/.nvm/v6.7.0/bin/node /home/www/html/server.js
Restart=always
RestartSec=1                       # Restart service after 10 seconds if node service crashes
StandardOutput=syslog               # Output to syslog
StandardError=syslog                # Output to syslog
SyslogIdentifier=nodejs-server1
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337

[Install]
WantedBy=multi-user.target

第三步

systemctl enable node-server1.service
systemctl start node-server1.service
tail -f /var/log/messages

【讨论】:

    【解决方案2】:

    nvm alias default 6.9.1


    根据NVM Documentation

    要设置在任何新 shell 中使用的默认节点版本,请使用 别名default:

    nvm alias default node
    

    请注意,这会将最新版本设置为默认版本...


    所以在你的情况下,你会这样做:

    # Install the version that you would like 
    nvm install 6.9.1
    
    # Set 6.1.0 (or another version) as default
    nvm alias default 6.9.1
    

    在不同版本下运行不同的应用程序

    或者,如果您需要为服务器上的不同应用程序使用不同的版本,您可以在 crontab 文件中包含类似的内容:

    @reboot forever start -c /home/your-name/.nvm/versions/node/v6.9.1/bin/node /path/to/app1/server.js
    
    @reboot forever start -c /home/your-name/.nvm/versions/node/v0.11.0/bin/node /path/to/retro/app/server.js    
    
    @reboot forever start -c /home/your-nodenode /path/to/modern/app/server.js
    

    个人而言这是我的首选方法,如果你有 20 或 30 个节点的应用程序,指定自己的版本更容易,而不是仅仅依赖服务器当前版本。

    【讨论】:

    • 我收到错误:# nvm install v6.7.0 ######################################################################## 100.0% Checksums empty manpath: can't set the locale; make sure $LC_* and $LANG are correct Now using node v6.7.0
    • 在上述错误后应用仍然有效吗? # nvm alias default 6.7.0 default -&gt; 6.7.0 (-&gt; v6.7.0)
    • 仍然显示# /usr/bin/node --version版本v6.12.2,甚至应用了nvm别名默认6.7.0
    • 嗯,这听起来像是您的安装问题。您是通过官方源 (github.com/creationix/nvm) 还是通过第三方(如自制软件或其他软件包管理器)安装的?
    • 如果这不起作用,你能发布一些关于你的系统的信息吗?操作系统? Bash 或 ZSH,还是别的什么? nvm版本?这会让帮助变得更容易:)
    猜你喜欢
    • 2020-03-16
    • 2011-11-07
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2014-10-25
    相关资源
    最近更新 更多