4

Say I put one client folder and one backend folder in a single repo and pushed to git. Each client and backend has its own package.json. But the root folder has its own package JSON. The reason I do this is that I have some pre-commit hooks that are common to both the client and backend repo. And it makes sense for developers to push the repo at the root, then the pre-commit hooks will take place. So I am thinking of putting the pre-commit hooks in the common root in a package.json file. Is it possible for me to do as below for project structure?

RootFolder
- client
   - package.json
- backend
   - package.json
package.json

In backend package.JSON:

 {
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "set NODE_ENV=development && src/app.js",
  },
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {},
 }

In client package.JSON:

 {
  "name": "client",
  "version": "1.0.0",
   "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "dependencies": {},
 }

And in the root folder, the package.JSON is something like this:

{
  "name": "root",
  "version": "1.0.0",
  "description": "",
  "scripts": {}, //should be empty
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "husky": {
   "hooks": {
    "pre-commit": "lint-staged"
   }
  },
  "lint-staged": {
     "*/src/**/*.{js,ts}": [
       "prettier --write",
       "eslint --cache --fix"
     ]
  }
 }
4
  • 1
    Yes, you can do it. But, doing so will duplicate the modules if you are using the same modules on both client and server
    – kavigun
    Commented Sep 28, 2020 at 10:45
  • 1
    Yes , you can look at stackoverflow.com/questions/50471757/… Commented Sep 28, 2020 at 10:47
  • Yes a repo can have multiple package.json in different directory. Why do you need package.json for pre-commit hook ? Commented Sep 28, 2020 at 10:54
  • sorry i just updated my question with more details with each of my package json.
    – tnkh
    Commented Sep 28, 2020 at 10:55

1 Answer 1

0

This work for me

  "scripts": {
    "start": "npm-run-all --parallel watch:server start:web",
    "start:web": "react-scripts start",
    "start:server": "node src/server",
    "watch:server": "nodemon --watch src/server src/server",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }

npm-run-all must be installed

Not the answer you're looking for? Browse other questions tagged or ask your own question.