简体   繁体   English

前后端可以共享同一个package.json吗?

[英]Can the frontend and backend share the same package.json?

I have a small personal project which I'm developing in one single repo.我有一个小的个人项目,我正在一个单一的回购协议中开发。

The backend is a Node.js server and the front is a Vue.js application.后端是Node.js服务器,前端是Vue.js应用。

I want both of them to share the same package.json我希望他们都共享相同的 package.json

The only reason I want to do that is because I want to use the "scripts: {}" of that one common package.json to execute commands that refer to either backend or frontend modules.我想这样做的唯一原因是因为我想使用那个常见的 package.json 的"scripts: {}"来执行引用后端或前端模块的命令。

Is this possible?这可能吗?

Would this structure make sense and work:这种结构是否有意义并且有效:

- my-project
  - front
    - {Vue.js files & folders}
  - back
    - {files & folders for my server}
  - package.json (containining dependencies and yarn scripts for both front and back)

But does that also mean that when eg Vite/Vue compiles the.js files for production it will also "accidentally" include irrelevant node_modules that were actually there only for the backend to use?但这是否也意味着当例如 Vite/Vue 编译用于生产的 .js 文件时,它也会“意外地”包含实际上仅供后端使用的不相关的 node_modules?

UPDATE: I tried it and it's pretty clean and straightforward and works fine.更新:我试过了,它非常干净、直接,而且工作正常。 I'm posting this here in case anyone is interested:如果有人感兴趣,我会在这里发布:

- /root
  - /back (contains server files & folders)
  - /front (contains Vue.js files & folders)
  - package.json
  - .eslintrc.cjs
  - .gitignore
  - vite.config.js
  - yarn.lock

// contents of package.json
{
  ......,
  ...,
  "type": "module",
  "scripts": {
    "back:start": "nodemon --experimental-modules --es-module-specifier-resolution=node ./back/server.js",
    "front:start": "vite",
    "front:build": "vite build",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
    "start": "yarn front:build && yarn back:start"
  },
  "dependencies": {
    .....,
    ...
  },
  "nodemonConfig": {
    "ignore": [...]
  },
  "engines": {
    "node": "^16.14.0"
  }
}

I'm deploying the above on Heroku as is and Heroku simply calls yarn start and the app is built and deployed.我按原样在 Heroku 上部署上述内容,Heroku 只需调用yarn start即可构建和部署应用程序。 (You'll notice I have no "devDependencies" and that's because Heroku ignores everything under "devDependencies", including vite) (你会注意到我没有“devDependencies”,那是因为 Heroku 忽略了“devDependencies”下的所有内容,包括 vite)

Yes this is possible and in fact quite common.是的,这是可能的,而且实际上很常见。 This usage pattern is often called the monorepo in the JS community.这种使用模式在 JS 社区中通常被称为monorepo The usual way of doing it is exactly the folder structure you describe.通常的做法就是您描述的文件夹结构。

Most frontend frameworks including Vue are designed to handle this however most of the usual tools assume your frontend is separate from your backend.包括 Vue 在内的大多数前端框架旨在处理此问题,但大多数常用工具都假定您的前端与后端是分开的。 You may need to manually configure your frontend framework to do this.您可能需要手动配置前端框架来执行此操作。

Some things to consider:需要考虑的一些事项:

  • Frontend development server (eg. webpack) may need to be configured to search for files in a subdirectory (eg. "frontend").前端开发服务器(例如 webpack)可能需要配置为在子目录(例如“前端”)中搜索文件。
  • You may want to install all frontend modules as --save--dev so they can be skipped when you deploy your backend.您可能希望将所有前端模块安装为--save--dev以便在部署后端时可以跳过它们。

There are of course additional advantages to this beyond shared package.json file (thus, a common npm run workflow).除了共享 package.json 文件(因此,一个通用的npm run工作流程)之外,当然还有其他优势。 One advantage is that you can have shared libraries between frontend and backend.一个优点是您可以在前端和后端之间共享库。 I usually have this structure:我通常有这样的结构:

├ .git
├ package.json
├ frontend/         - frontend project
│    ├ src/         - source files
│    └ public/
├ backend/          - backend project
│    ├ controllers/ - endpoint modules
│    └ lib/         - backend models/modules
└ lib/              - shared modules

In my package.json I usually have at least something like this:在我的 package.json 中,我通常至少有这样的东西:

{
  "scripts": [
    "frontend": "cd frontend; webpack serve",
    "backend": "cd backend; nodemon main.js"
  ]
}

The npm commands even look natural: npm 命令看起来甚至很自然:

npm run backend
npm run frontend

I normally use a javascript script to run both backend and frontend together to reduce issues for my Windows using colleagues.我通常使用 javascript 脚本同时运行后端和前端,以减少我的 Windows 使用同事的问题。 So my start script is usually just:所以我的开始脚本通常只是:

{
  "scripts": [
    "start": "node ./scripts/start.js"
  ]
}

.. however writing such start scripts will more than double the length of this answer (which is already quite long as is) so I leave it to your creativity. .. 但是编写这样的启动脚本会使这个答案的长度增加一倍以上(已经很长了)所以我把它留给你的创造力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM