反应更改文件而无需重建和重新启动

时间:2018-07-11 02:53:12

标签: node.js reactjs

我基于node.js表达和反应创建了一个小项目。

我的项目结构很简单

enter image description here

然后我将node作为服务器和所有反应代码winthin client文件夹运行。

我的节点package.json如下所示:

"scripts": {
    "start": "concurrently \"npm run server\" \"npm run client\"",
    "server": "node ./bin/www",
    "client": "node start-client.js",
    "lint:client": "(cd ./client && npm run lint)",
    "lint:app": "eslint . --ext .js,.jsx --ignore-pattern 'client/*'",
    "lint": "npm run lint:client && npm run lint:app",
    "dev:client": "(cd ./client && npm start)",
    "dev:app": "nodemon ./bin/www",
    "build": "(cd ./client && npm run build)",
    "test:client": "(cd ./client && CI=true npm test)",
    "test:app": "jest --forceExit",
    "test": "npm run test:client && npm run test:app",
    "coverage:client": "cd ./client && npm test -- --coverage",
    "coverage:app": "jest --coverage",
    "coverage": "npm run coverage:client && npm run coverage:app",
    "install": "(cd ./client && npm install --quiet)"
  },
客户端中的

package.json如下所示:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "lint:scss": "stylelint 'src/**/*.scss' --syntax scss",
    "lint:scss:fix": "stylefmt --recursive 'src/**/*.scss'",
    "lint:js": "eslint . --ignore-path .gitignore --ext .js,.jsx",
    "lint:js:fix": "npm run lint:js -- --fix",
    "lint": "npm run lint:js && npm run lint:scss",
    "eject": "react-scripts eject"
  }

然后我在根文件夹中运行npm start以启动两个node.js并响应构建。

我的问题是我在更改任何反应代码时需要重新运行npm start

所以,我的问题是有什么办法可以监视我的代码更改,而无需重新启动 文件更改时?

1 个答案:

答案 0 :(得分:2)

如果您询问如何编辑代码并实时重新加载代码,建议您使用nodemon。查看github页面:https://github.com/remy/nodemon

我有一个类似的项目,我通过在concurrently脚本中使用package.json处理了实时重载:

"scripts": {
        "serve": "nodemon index.js",
        "client": "npm run start --prefix client",
        "dev": "concurrently \"npm run serve\" \"npm run client\""
    }

--prefix client是我的react目录的名称。 client脚本运行我的React Server的基本npm run start,而dev脚本同时运行我的后端和后端。