【问题标题】:Can a repo have multiple package.json in different directory?一个 repo 可以在不同的目录中有多个 package.json 吗?
【发布时间】:2021-01-13 22:39:15
【问题描述】:

假设我将一个客户端文件夹和一个后端文件夹放在一个 repo 中并推送到 git。每个客户端和后端都有自己的 package.json。但是根文件夹有自己的包 JSON。我这样做的原因是我有一些客户端和后端 repo 共有的预提交钩子。对于开发人员来说,将 repo 推送到根目录是有意义的,然后会发生预提交挂钩。所以我正在考虑将预提交挂钩放在 package.json 文件的公共根目录中。项目结构我可以做如下吗?

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

在后端 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": {},
 }

在客户端包.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": {},
 }

在根文件夹中,package.JSON 是这样的:

{
  "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"
     ]
  }
 }

【问题讨论】:

  • 是的,你可以做到。但是,如果您在客户端和服务器上使用相同的模块,这样做会复制模块
  • Yes 一个 repo 可以在不同的目录中有多个 package.json。 为什么你需要 package.json 来做 pre-commit 钩子?
  • 对不起,我刚刚用我的每个包 json 的更多细节更新了我的问题。

标签: node.js npm package.json


【解决方案1】:

这对我有用

  "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 必须安装

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2010-11-20
    • 2023-03-11
    相关资源
    最近更新 更多