nodejs
nodejs copied to clipboard
Using BP_NODE_PROJECT_PATH should keep only the file specified in the path, not the root directory
Describe the Enhancement
Currently when I use the BP_NODE_PROJECT_PATH environment variable, all the files in my monorepo are copied into the final image. What I want is for the command to recognise the yarn.lock or package-lock in the root workspace and use that to build the project in the specified path, but the source code files in the final image should only be the files in the specified path
Possible Solution
No idea because I don't know how this buildpack is implemented
Motivation
I use paketo in my monorepo but I want the final image to be rid of unnecessary files.
In your monorepo, are the sub-projects separate, or are there shared files? I ask because it is possible to run a build with a subset of the repository structure, eg. pack build my-app --path ./some-sub-directory
.
Yes, I do that already. I was suggesting this because the docs mentioned using BP_NODE_PROJECT_PATH
for monorepo cases.
I think that in the case where you'd use the BP_NODE_PROJECT_PATH
environment variable, there is an expectation that the application shares some files with other parts of the monorepo and therefore we cannot safely remove those files from the image. We could update the docs to mention the --path
flag on pack build
as the preferred solution when your application sub-directories are independent. Does that help?
I think mentioning that when application sub-directories are independent, --path
is likely the better option. But for my use case, it's not what I want to have. I'd prefer a way to build a monorepo but only copy the files for the workspace/project I want to build.
I have a Typescript monorepo where workspaces/projects share a root tsconfig.json file. Let's say I have this file structure:

The tsconfig.json for the product
project is:
{
"extends": "../../tsconfig.json",
"include": ["src"],
"exclude": ["scripts", "node_modules"],
"compilerOptions": {
"outDir": "dist"
}
}
If I use that, then there's an error when building the project because it can't find the root tsconfig.json. So, I would like to be able to specify that env variable and build projects in monorepos. Currently, if I use BP_NODE_PROJECT_PATH I can't get it to build successfully, maybe I need to tweak something. The build command runs successfully if I compile normally, but using the buildpacks I get weird compile errors. If I build local and just run pack build
it copies all the files including the locally built /dist
folder and runs fines (but copying all the files isn't my desired goal)
Also, is there a buildpack that can detect TypeScript and automatically use the build script in the project, rather than me specifying BP_NODE_RUN_SCRIPTS=build
? I think the buildpack should detect the build
script just like it does with the start
script.
@ryanmoran any thoughts on this?
There are a couple of requests here. Let me enumerate each and give some guidance on what can be done.
-
the built image should exclude some extra files that belong to other apps
For this, I'd suggest to try out the
source-removal
buildpack. We have discussed adding this to a few of the existing language families, but don't have enough data yet to make the best choices for users. Ideally, you could try using this buildpack and giving us some feedback about the settings you used to achieve your desired outcome. -
the buildpack should automatically detect and run the build script
This feature was recently added in https://github.com/paketo-buildpacks/node-run-script/pull/154 and released in https://github.com/paketo-buildpacks/node-run-script/releases/tag/v0.5.0 and https://github.com/paketo-buildpacks/nodejs/releases/tag/v0.25.0. Please try those releases out and let us know if it satisfies your expectations.
If I've missed anything else in your request, please let me know.
@ryanmoran yes I just noticed yesterday that the buildpacks automatically runs the build script. That's what I want, and I'm glad it works.
I didn't find that buildpack in the docs. Could you give me some pointers as to how I can use this with the pack cli and combine it with the nodejs buildpack
Sorry, I should have included a link to the source-removal
buildpack: https://github.com/paketo-buildpacks/source-removal. The README includes some more details, but to summarize, you could do something like the following:
pack build my-app \
--buildpack paketo-buildpacks/nodejs \
--buildpack paketo-buildpacks/source-removal \
--env BP_INCLUDE_FILES=dist/*
This will remove any files from the /workspace
directory that are not in the dist
subfolder.
I'm going to close this because it looks like the issue was resolved and its been a few years since the last comment/update. Please let us know if that was not the right thing to do.