linux一键安装node+npm
生活随笔
收集整理的這篇文章主要介紹了
linux一键安装node+npm
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
分享一個linux下一鍵安裝node+npm腳本。
使用方式為: ./install-node.sh,然后輸入版本號,node.js版本查詢
切記不需要加 sudo 執(zhí)行!!!
默認安裝10.15.0。
#! /bin/bash############################################################ # 此腳本為一鍵安裝node-npm腳本 # # 2018-08-24 # # 使用方式為:./install-node.sh # ############################################################nodeVersion="10.15.0" # 安裝的默認版本 VERSIONPATTERN="[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}"echo -n "Please input a node version number (Enter 10.15.0): " read customVersion # user custom version, eg. 10.15.0if [ ! -z $customVersion ] # 如果輸入的版本不為空 thenmacthResult=$(echo $customVersion | grep -E -x $VERSIONPATTERN )if [ -z $macthResult ]thenecho "Please input a right version number. eg. 8.11.4 or 10.15.0"exit 1finodeVersion=$customVersion fi # 下載網(wǎng)址 downloadAddress="https://nodejs.org/download/release/v${nodeVersion}/node-v${nodeVersion}-linux-x64.tar.gz" downloadPath="/opt/" # 默認安裝的路徑nodePath="/opt/node/" packageName="node.tar.gz" sysPathFileForNode="/etc/profile.d/node.sh" # system PATH file name for node.currentUser=$(whoami)if [[ $currentUser == "root" ]] thenecho "please execute script by user! Not root!"exit fiisReinstall="n" # Check if node is already installed. checkNodeIsExist() {# 1.Check whether the node command exists.if [[ $(checkCmd node) == "y" ]]thenecho -n "The node command already exists,whether to reinstall [y/n]? :"read isReinstallif [[ "${isReinstall}" == "y" ]] || [[ "{$isReinstall}" == "Y" ]] # not reinstallthenechoelseexit 1fifiecho "Check local node has been completed." }download() {echo "Download version is $nodeVersion"echosudo wget -O $packageName $downloadAddressif [ $? -ne "0" ]thenecho "The node package download faild !"exit 1fisudo mv ./$packageName $downloadPathecho "The node v${nodeVersion} has been downloaded." }decompress() {sudo tar -zxf ${downloadPath}${packageName} -C $downloadPath# check isReinstallif [[ "$isReinstall" == "y" ]] || [[ "$isReinstall" == "Y" ]]then# update node binary filesudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/bin/node ${downloadPath}node/bin/# update npmsudo rm -rf ${downloadPath}node/lib/node_modules/npmsudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/lib/node_modules/npm/ ${downloadPath}node/lib/node_modules/sudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/bin/npm ${downloadPath}node/bin/# update npx link filesudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/bin/npx ${downloadPath}node/bin/# remove include/node foldersudo rm -rf ${downloadPath}node/include/node# update include/node flder sudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/include/node ${downloadPath}node/include/# remove share foldersudo rm -rf ${downloadPath}node/share# update share foldersudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/share ${downloadPath}node/# update CHANGELOG.md 、LICENSE 、README.md filesudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/CHANGELOG.md ${downloadPath}node/sudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/LICENSE ${downloadPath}node/sudo mv ${downloadPath}node-v${nodeVersion}-linux-x64/README.md ${downloadPath}node/sudo rm -rf ${downloadPath}node-v${nodeVersion}-linux-x64sudo rm -rf ${downloadPath}${packageName}elsesudo mv ${downloadPath}node-v${nodeVersion}-linux-x64 ${downloadPath}nodesudo rm ${downloadPath}${packageName}if [ $? -ne "0" ]thenecho "Faild to decompressed!"exit 1fifiecho "The node package has been decompressed." }changePermission() {sudo chown ${currentUser}:${currentUser} ${nodePath} -Recho "The node folder permission has been changed." }# Configure system environment variables and export executable paths of node and NPM. configSysPath() {local tempFile="node.sh"touch $tempFileecho 'export NODE_HOME=/opt/node' > $tempFileecho 'export PATH=$PATH:$NODE_HOME/bin' >> $tempFileecho 'export NODE_PATH=$PATH:$NODE_HOME/lib/node_modules' >> $tempFilesudo mv ./$tempFile $sysPathFileForNode }checkCmd() {local cmd=$1which $cmd > /dev/null 2>&1if [[ $? == 0 ]]thenecho 'y'elseecho 'n'fi }npmCompletion() {local temp="/tmp/npm_completion"echo 'if type complete &>/dev/null; then_npm_completion () {local words cwordif type _get_comp_words_by_ref &>/dev/null; then_get_comp_words_by_ref -n = -n @ -n : -w words -i cwordelsecword="$COMP_CWORD"words=("${COMP_WORDS[@]}")filocal si="$IFS"IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \COMP_LINE="$COMP_LINE" \COMP_POINT="$COMP_POINT" \npm completion -- "${words[@]}" \2>/dev/null)) || return $?IFS="$si"if type __ltrim_colon_completions &>/dev/null; then__ltrim_colon_completions "${words[cword]}"fi}complete -o default -F _npm_completion npm elif type compdef &>/dev/null; then_npm_completion() {local si=$IFScompadd -- $(COMP_CWORD=$((CURRENT-1)) \COMP_LINE=$BUFFER \COMP_POINT=0 \npm completion -- "${words[@]}" \2>/dev/null)IFS=$si}compdef _npm_completion npm elif type compctl &>/dev/null; then_npm_completion () {local cword line point words siread -Ac wordsread -cn cwordlet cword-=1read -l lineread -ln pointsi="$IFS"IFS=$'\n' reply=($(COMP_CWORD="$cword" \COMP_LINE="$line" \COMP_POINT="$point" \npm completion -- "${words[@]}" \2>/dev/null)) || return $?IFS="$si"}compctl -K _npm_completion npm fi' > $tempsudo mv $temp /etc/bash_completion.d/ }main() {echo "------------------------START-------------------------"# 1. check node installationcheckNodeIsExist# 2. download node packagedownload# 3. decompree node packagedecompress# 4. change node folder permissionchangePermission# 5. install npm completionnpmCompletion# 6. configure node configconfigSysPathsource $sysPathFileForNode # Enable the configuration to take effect immediately.echo "------------------------END-------------------------"echo -e "Successfully installed node and NPM.\nPlease try : node -v and npm -v" }main總結(jié)
以上是生活随笔為你收集整理的linux一键安装node+npm的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问题 D: 最小生成树II
- 下一篇: 13-容器的端口映射