我有一个使用nx
的项目,其中包含许多包。我的问题是,我只有一个package.json
文件,所以如果我只想构建一个包,我仍然必须使用npm install
构建根项目。
这是一个问题,因为在CI/CD步骤中,我必须构建整个项目,这需要花费太长时间,而且它还生成一个node_modules
文件夹,该文件夹变得巨大(3GB),这也使我的包大小过大。
如何构建单个包,使node_modules
文件夹只包含包所需的依赖项,而不是拥有所有包的所有依赖项?
如果这是不可能的,那么如何编译一个捆绑所有依赖项的可执行main.js
文件呢?
编辑:我尝试为所有包拆分package.json
文件,但是每当我构建一个单独的包时,我仍然会将所有的依赖项加载到根目录中的node_modules
文件夹中。是否有可能为每个单独的包有一个node_modules
文件夹?
发布于 2022-10-14 20:18:51
我找到了一个不理想的解决方案,但至少起作用了。generatePackageJson
所做的是在build文件夹中创建一个package.json
文件,该文件可用于再次运行build 来生成只包含特定目标的依赖项的node_modules
文件夹。因此,在我的例子中,我所做的就是将"generatePackageJson": true
添加到后端包中的build
目标中:
{
"sourceRoot": "apps/backend/src",
"projectType": "application",
"targets": {
"build": {
// ...
"options": {
"outputPath": "dist/apps/backend",
"main": "apps/backend/src/main.ts",
"tsConfig": "apps/backend/tsconfig.app.json",
"generatePackageJson": true, //
"assets": [
"apps/backend/src/assets"
]
}
// ...
}
}
}
现在,如果我运行nx build backend
,我将在dist/apps/backend
中有一些可以用作独立项目的东西。不幸的是,npm
检查父文件夹中的package.json
文件,并破坏所有内容,因此我最终编写了一个构建脚本,该脚本可以清理项目并允许轻松部署。(这适用于Heroku,但很容易适应其他PaaS / IaaS解决方案。我要把它逐字粘贴在这里:
注意,这个脚本是从根文件夹运行的,所以我们的后端dist文件夹相对于这个脚本的运行位置位于
dist/apps/backend
。(我将脚本保存在script
文件夹中。您还不应该运行这个本地,因为它会删除项目中的内容。只在CI/CD环境中运行此操作。
#!/bin/bash
print_usage() {
echo " $1"
echo "Help below "
echo ""
echo "Builds the specified project for Heroku."
echo ""
echo "Usage: ./heroku-build <nx-project-name>"
echo ""
echo "__Note that__ this script is intended to be used from CI/CD, you probably won't need it during development."
exit 1
}
if [ -z "$1" ]; then
print_usage "Project name is missing!"
fi
# Heroku build is a little bit different because they have a slug size (deployment size) limit of 500MB.
# If you build the project (not just the package) you'll end up with a `node_modules` folder that's ridiculously big (3GB)
# but it can't be pruned properly. If you think you can prune it without this hacky solution go ahead, but it is unlikely
# that you'll be able to figure it out. **If** you try it please increment the counter below:
#
# total_hours_wasted_trying_to_prune_node_modules=13
#
# So how this works is that Heroku will run `npm install --prod` that will delete devDependencies too, so
# DON'T MOVE nx and @nrwl packages to devDependencies!
# After the project is built you'll have the horrendous `node_modules` folder, but it's not a big deal as we'll delete it.
# Build the project with nx
nx build $1 --prod
# This step is necessary because npm will look for `package.json` files in the parent folder and it will use the `node_modules`
# folder from the parent folder. We don't want that, we want to have only the necessary packages (backend) in the `node_modules` folder.
mv package.json package.json.bak
mv package-lock.json package-lock.json.bak
# We get rid of all the unnecessary packages.
rm -Rf node_modules
# We install the necessary packages.
# In the `nx build` step nx generates a `package.json` that only contains the dependencies of the backend project
# so this will *only* () download 500MB from npm.
npm install dist/apps/backend
# More reading on this problem:
#
# - https://stackoverflow.com/questions/73373307/how-to-build-and-package-only-the-relevant-dependencies-using-nx?noredirect=1#comment129738870_73373307
# - https://github.com/nrwl/nx/issues/1518
# - https://github.com/nestjs/nest/issues/1706#issuecomment-579248915
https://stackoverflow.com/questions/73373307
复制相似问题