本文简要讲述node的安装和使用

安装

centos端安装

可以随意替换版本。例如10.23.0

  1. # 卸载系统可能自带的低版本node
  2. yum remove nodejs npm -y
  3. # 下载、解压
  4. wget http://mirrors.ustc.edu.cn/node/v14.17.4/node-v14.17.4-linux-x64.tar.gz
  5. tar -zxvf node-v14.17.4-linux-x64.tar.gz
  6. rm node-v14.17.4-linux-x64.tar.gz
  7. mv node-v14.17.4-linux-x64 /usr/local/node
  8. # 配置环境变量
  9. sduo vim /etc/profile
  10. #set for nodejs
  11. export NODE_HOME=/usr/local/node
  12. export PATH=$NODE_HOME/bin:$PATH
  13. EOF
  14. source /etc/profile

windows端

官网下载:https://nodejs.org/en/

mac

  1. brew install node

包管理工具

npm

尽量使用npm,不容易出现问题,尽量不要使用淘宝镜像,会出现莫名的问题。嫌慢就开全局代理

  1. # 安装示例
  2. npm i webpack # 在module文件中安装
  3. npm i webpack -S # 常用,添加到package
  4. npm i webpack -g # 全局
  5. # 卸载
  6. npm un webpack -g
  7. # 更新
  8. npm update webpack -g
  9. npm i npm@latest -g # 更新npm自己到最新
  10. # npm版本
  11. npm -v

yarn

tyarn 为淘宝源,可以直接使用。

  1. npm i yarn tyarn -g
  2. tyarn -v

使用

  1. # 初始化项目
  2. yarn
  3. # 安装
  4. yarn add react
  5. # 卸载
  6. yarn remove react
  7. # 安装到dev
  8. yarn add react --dev
  9. # 更新项目包
  10. yarn upgrade
  11. # 全局安装
  12. yarn global add umi
  13. # yarn升级自己
  14. yarn self-update # 这个命令用来升级Yarn到最新的版本。
  15. yarn self-update [tag] # 命令将会安装指定版本的Yarn。

yarn全局安装的工具默认是没有加入到环境变量的。

  1. # 获取环境变量
  2. yarn global bin
  3. # mac 系统:
  4. $ sudo vi ~/.bashrc
  5. # 在 .bashrc 中添加下面一行:
  6. export PATH="$PATH:`yarn global bin`"
  7. # windows系统:
  8. # 获取 global bin 的路径
  9. $ yarn global bin
  10. C:\Users\Administrator\AppData\Local\Yarn\bin
  11. # 复制上面的 global bin 的路径,添加到系统环境变量 PATH。

其他

serve

快速搭建一个本地服务器,够方便。

安装: yarn global add serve

使用:

  1. $ serve dist/
  2. INFO: Accepting connections at http://localhost:5000

now

把本地web项目推送到远程服务器,让更多人可以访问。
因为发布的代码是开源的,所以我只当做临时使用,更详细的教程请参考这里

准备工作:

  1. 安装:yarn global add now
  2. 注册:https://zeit.co/dashboard
  3. 本地登录一下: now login your@gamil.com ,然后登录你的邮箱验证就可以了。

正常使用:

  1. $ now ./dist/
  2. > Deploying D:\我的坚果云\code\web\ant-design\myapp\dist under tianyun
  3. > Using project dist
  4. > WARN! This is the first deployment in the dist project. It will be promoted to production.
  5. https://dist-gxcomv0zz.now.sh- Finalizing...
  6. > Ready! Deployed to https://dist.tianyun.now.sh [in clipboard] [34s]

pm2

后台管理并运行node项目。

安装:yarn global add pm2

使用:

  1. 待完善

问题

全局安装后找不到命令

1、查看全局目录

  1. npm root -g

2、 添加到环境变量