react native更改默认端口8081,配置多端口

React Native 2019-12-24 阅读 298 评论 0

我们在本地开发react native项目时候,通常会先运行:npm start 或者 yarn start。

此命令将启动8081端口,用于提供JSBundle包和一些静态资源。有时候8081被占用,或者需要启动多个react native项目,就需要修改默认端口8081。

查看项目根目录下的 package.json

{
  "name": "Demo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "react-native start",    // 注意这一行
    "test": "jest",
    "lint": "eslint ."
  },
...
}

我们在运行 npm start 或者 yarn start 时,其实是运行了 react-native start。

修改默认端口,可以运行:

react-native start --port 9002
## 或者
yarn start --port 9002

将使用9002端口,比如: 

$ react-native start --port 9002
┌──────────────────────────────────────────────────────────────────────────────┐
│                                                                              │
│  Running Metro Bundler on port 9002.                                         │
│                                                                              │
│  Keep Metro running while developing on any JS projects. Feel free to        │
│  close this tab and run your own Metro instance if you prefer.               │
│                                                                              │
│  https://github.com/facebook/react-native                                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Looking for JS files in
   /Volumes/develop/Demo

Loading dependency graph, done.



最后更新 2019-12-24