问题描述
我有,它包含一个后端(Node / Express)和一个前端客户端,因此:
I have a repository which contains a backend (Node/Express) and a Frontend client, as such:
├── build
├── config
├── coverage
│ └── lcov-report
├── dist
│ └── static
├── server (node/express server)
│ ├── coverage
│ ├── docs
| ├── src
│ ├── etc
│ └── package.json
|
├── src (Vue.js : client code)
│ ├── api
│ ├── assets
│ ├── components
│ ├── router
│ └── store
└── static
└── package.json
我有两个package.json文件,一个用于客户端,另一个用于服务器。
I have two package.json files, one for the client and one for the server.
- 我面对在Heroku等服务上部署问题,因为他们不希望在一个存储库中有两个不同的npm包(我认为)。如何使用此设置部署到Heroku(或其他)?
- 为应用程序的两个部分都有1个package.json文件会更明智吗?
在同一个package.json中同时拥有前端和后端部分的优点和缺点是什么?
Which would be the advantage and disadvantages of having both frontend and backend parts in the same package.json?
推荐答案
您可以使用,并在您推送到Heroku的单个git仓库中为您的客户端和服务器维护单独的package.json文件。
You can use heroku-postbuild and maintain separate package.json files for your client and server in a single git repo that you push to Heroku.
例如,在我的一个项目中,目录结构如下所示:
For example, in one of my projects, the directory structure looks like this:
|-- package.json (for node/express server)
|-- Procfile
|-- www
|--client
|-- package.json (for Ionic/Angular client app)
|-- ...
|--server
|--- ...
|-- server.js (top level node.js/express script for server)
在我的顶层package.json中,我有:
In my top-level package.json, I have:
"scripts": {
"start": "node www/server.js",
"heroku-postbuild": "cd www/client && npm install && npm run build"
},
在我的客户端package.json中, p>
In my client package.json I have:
"scripts": {
"build": "ionic-app-scripts build",
...
},
最后在我的Procfile中有:
And finally in my Procfile I have:
web: npm start
使用此解决方案,Heroku运行我的服务器并在每个Heroku构建版本上构建我的客户端代码。
With this solution, Heroku runs my server and builds my client code on every Heroku build.
我认为客户端和服务器package.jsons应该分开保存,原因有几个。首先,你真的不希望所有的服务器端代码捆绑到你的客户端。
I think client and server package.jsons should be kept separate for several reasons. For one thing, you really don't want all your server-side code bundled into your client.
这篇关于部署完整堆栈节点应用程序npm / package.json体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!