-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support running an app from a subdirectory #385
Comments
Exactly what I need. Thanks @jmorrell! |
@jmorrell here's a workaround. It ain't perfect but works...
{
"name": "foobar-app",
"version": "0.1.0",
"scripts": {
"postinstall": "npm install --prefix server && npm install --prefix frontend-app"
}
} Notice the --prefix argument which tells npm which subfolder to work on.
Hope that helps, |
OK, I just realized you are working for Heroku, so this is probably not a real question, but rather a feature request. Sorry for the misunderstanding. In any case, I will leave my answer above in case someone finds it useful. |
@jmike thanks for weighing in. Is this something you've tried yourself and has worked? Because in the absence of an official solution I'll give that a try. |
@Arrow7000 yeap, works like a charm. The only problem is caching - see issue #387 even though it's somewhat unrelated to the application structure. See, I am using local files as npm dependencies. In any case if you face a similar issue you can always disable caching according to heroku instructions https://devcenter.heroku.com/articles/nodejs-support#cache-behavior. |
Thanks @jmike that worked like a treat! It's not the cleanest solution, because it requires an extra package.json with important options duplicated - eg |
We had been using git subtree push for a while but started facing the exact issue described above, so we implemented the following custom buildpack https://github.com/Pagedraw/heroku-buildpack-select-subdir which allows us to deploy multiple apps from the same Heroku repo. Then we just make each of the frontend-app and server require the shared-code as an npm local dependency. One caveat is that we have to explicitly add the node_modules folder installed to the NODE_PATH so npm knows where to look for requires within shared-code. It works for us. Let me know if it also works for you! |
@jmike I tried your approach and it sadly does not work for me.
It fails though when trying to run gulp:
Any idea on what I am doing wrong here would be highly appreciated :) |
It appears that if you run the buildpack from within a subdirectory, the commands it's suppose to install are not available after release. My buildpack runs the heroku/php buildpack in a sub directory, but when I |
@atomkirk The buildpacks install their dependencies globally, and I'm not sure what you mean by running the "php buildpack in a sub directory". Could you open a support ticket at https://help.heroku.com/ so we can sort out what's going wrong? |
So it looks like they don't install them globally. When I deploy a root directory php app with heroku/php buildpack, then So this is actually an easy fix, just need to fix PATH so it points to Would be nice if the buildpack system provided an option so we didn't have to make our own buildpacks to work around it |
@Jan0707 have you tried running gulp on Another idea might be to install gulp with root package.json and update your configuration as such: {
"name": "App",
"engines": {
"node": "8.1.x"
},
"dependencies": {
"gulp": "^3.9.1"
},
"scripts": {
"postinstall": "npm install --prefix app/Resources && gulp --gulpfile app/Resources/gulpfile.js"
}
} Please note that you don't really need to provide the qualified file path for gulp - npm knows what to do. |
Why a subdirectory, @atomkirk? Or, at least, why does the PHP runtime have to be in a subdirectory, and not just your application? A lot of moving parts are set up to look at |
Well, thanks for the response, but @jmorrell is right, I'm trying to do something unsupported. I kind of took us off topic. Sounds like if you want to support the original issue, the buildpack will probably have to run in the subdirectory and then PATH point at where it put the .heroku folder (or if the buildpack puts the .heroku folder in the repo root, then change nothing) |
This feature is what I was looking for. I have a client and a server which have two independent package files, and I want them to be sibling directories and not nested. Therefore I need a way to instruct heroku to start from a specific package file which is not located at the root of my repository. |
Hi,
The entire project is inside server folder , including the client ... any help |
@IIvanov8888 could you open a support ticket at help.heroku.com? |
@gablg1 for me your solution doesn't work. After valid deploy I see in console app[client.1]: bash: npm: command not found. The same situation with node. |
I have a similar issue. I want to run an Ember Fastboot app without having to make a separate repo for my API backend (which is Rails in my case). Ideally I'd be able to specify multiple web processes for a single app. Since I can't do that, I have to make two separate apps for the frontend and backend. But I don't want to create multiple repos for this. I just want to be able to tell one app to use one subdirectory and tell the other app to use the other subdirectory from the git project root. |
As mentioned in the ticket, one can use |
@WeishiZeng can you please elaborate? :) |
@Bnaya The git command is used to push a subfolder to remote. If the subfolder happens to be a self-contained app recognized by heroku, then it'll be deployed. |
One note to add to the conversation is that the I encountered this issue when trying to use a monorepo with a node.js app as I describe here: https://stackoverflow.com/questions/51449750/how-do-i-get-heroku-ci-to-run-tests-when-using-a-monorepo |
has there not been an official solution for this yet? is there a ticket somewhere that we can follow? |
@kris-campos I haven't had a chance to loop back on this yet, so there is no official solution. In the meantime I would look at Yarn workspaces: https://yarnpkg.com/blog/2017/08/02/introducing-workspaces/ which I believe npm is also in the process of implementing. This will be the first thing I explore as a potential solution. If you try them please report back with how it worked for you. |
@jmorrell I just starting switching to yarn workspaces & lerna. So far using https://github.com/timanovsky/subdir-heroku-buildpack I can get the package to run properly but I'm unable to access a shared module because it bascially wipes everything outside the sub directory so
|
UPDATE: Well I tried to avoid it but looks like I need to use https://www.npmjs.com/package/yalc instead of This is my current working deployment setup using a local lerna / yarn workspaces monorepo:
First, I'm using these 3 buildpacks: https://github.com/weibeld/heroku-buildpack-run
https://github.com/timanovsky/subdir-heroku-buildpack
https://github.com/danielmahon/create-react-app-buildpack I use #!/bin/bash
echo " Copying shared modules"
mkdir -p packages/app/lib/@myscope
cp -R packages/shared packages/app/lib/@myscope
cp yarn.lock packages/app
# repeat for other simultaneous deployments I use I use a forked Maybe we can set In my
I also needed to set the shared package as an optional dependency since it is unpublished.
As of now, running This works but obviously requires much more setup than I would like, let me know if anyone sees anything that can be simplified (I suppose a dedicated buildpack would help), until there is a better option. |
Why do you have the server and frontend-app in the postinstall but only the server in the procfile? |
@chrissyast The The Procfile specifies the "start" command for the server. There's only one server per Heroku dyno, and |
removing the yarn.lock did the trick for me :) |
I don't really like the suggested solution. I have a Go app and now I have to add useless |
BTW I'm also think that many people here are actually trying to deploy their build artifacts generated by |
Heroku losing more ground. 😢 |
Why not let the official buildpacks accept an environment variable specifying the root directory? What's wrong with the blunt approach? An official reply from Heroku would be nice as to why this issue is difficult or taking so long to fix. |
The reason we are waiting is because npm 7 will support workspaces (https://github.com/npm/rfcs/blob/latest/accepted/0026-workspaces.md), which is a solution for subdirectories in Node projects. Yarn already has subdirectory support, and npm will in the near future. I helped review the npm workspaces RFC, and it seemed that this would be a good solution for Heroku developers. If we find that npm workspaces is not a fit for Heroku users, we can reexamine. We need to make this decision knowing how npm workspaces work with Heroku users so that the buildpack doesn't step on what npm is offering. |
@danielleadams thanks for your reply and attention. But for a quick vent, neither Netlify, Vercel, nor AWS Amplify needed NPM 7 to do an incredible job supporting monorepos. I’ve been reworking my servers to work with the server less architecture so I can, with a heavy heart, migrate away from Heroku. |
@LawJolla I hear you and appreciate your support of Heroku. I'm fully aware of what the other cloud providers have done, but unfortunately, that is not the direction we opted to go. |
Huh? What about the other Buildpacks then? Cause this is really an issue that ends up affecting all of them. Perhaps Heroku needs to admit that buildpacks are not suited for this problem and a new approach needs to be found. Maybe some sort of switchboard that lets us map the directories to their runtimes. |
@StephenCarboni I hear you. You're welcome to open a support ticket to give that feedback, but the topic being discussed on this Issue is only for subdirectories within Node projects, hence the multiple comments of waiting for NPM's version of workspaces to be released. |
I've spent the entire work day trying to get a simple monorepo deployed. I'm solely trying to deploy the frontend at this point. I've tried dozens of solutions, so far to no avail. The 2 apparently simplest from this thread seemed to be: #385 (comment) However in both cases, although it purports to have pushed successfully, accessing the app in a browser yields 404 not found. My project layout is simply: I tried accessing the heroku url directly, as well as accessing /frontend. Both cases, 404. After reading this entire thread and probably 20 other blog posts about this issue, I'm honestly a bit baffled that such a seemingly common setup is so difficult. I'm not using yarn workspaces nor Lerna, just a simple react app that includes code from a shared folder. If anyone could give me some pointers as to specifically why the 2 comments mentioned above are yielding 404s, I'd be appreciated. I figured this final deployment step would be the simplest/quickest, but seems to be the biggest hangup of this whole project so far :( |
Second full work day on this. I gave up on Heroku for the frontend, & deployed it to Netlify, which took about 2 seconds & worked immediately. Now I'm stuck with the backend. I tried @alexpetralia 's idea of putting release: cd backend && npm install && npm run build. When I push, I see it running the commands, so it appears to work. However, it does not. If I login interactively (heroku run bash), none of the packages have been installed in "backend." If I manually run the exact same commands: cd backend && npm install && npm run build, then it works fine. It just doesn't work when they're in the procfile. I still cannot comprehend that 3 years later this is so hacky & difficult. |
@metal450 Would Netlify not work for the backend too? |
@metal450 Can you try to use this build pack? Make sure to add the monorepo buildback, then add the node js buildpack below it. Also note that you do not use a Procfile with this buildpack. I have it working with this monorepo setup like yours. Check the server directory. Notice the |
@chrissyast: Netlify is for static sites (frontends) only. I think you can maybe get backends to work with some modification ("serverless functions"), which I haven't started looking into. I guess I may just have to abandon Heroku entirely. It just still feels...ridiculous that it could possibly be this inflexible. |
@joserocha3: Tried it - build failed, it couldn't find source files in /shared. I think this only works if your subprojects are well and truly completely separate, but if you give it a subdirectory that accesses any shared resources in another, it fails. i.e.:
|
Finally got my own hacky solution that works. It's similar to some of the others above, but or some reason using the Procfile didn't work at all (I tried using its
Make sure the backend's package.json's script tag has This works for the backend. I gave up on Heroku for the frontend, which is on Netlify. So between using Netlify for frontend & this for backend, I have a hacked-together working stack, if or until Heroku finally gets around to supporting something as basic as an official way to give it a subdirectory to run. |
Thanks @jmike for the suggestion, it helped me a lot finding a solution for my case! DISCLAIMER: It's been difficult!I have a root folder with a Instead of using The other trick was to specify Finally, I managed to deploy to Heroku adding this root package.json (verbose because I originated it with
Side note: to make the angular compile in the correct server dist folder, change client's
Note that if your server is not using typescript, you might want to use this instead (omit
|
I wanted to replace an outdated custom buildpack that just installs node & npm for asset compilation in a subfolder ( This is annoying :/ I don't know what workspaces are/will be but I'm not even sure if that would fix my problem... |
For future reference and for anyone that is running a monorepo using NPM workspaces. You can workaround this issue by using the If anyone is deploying via Github Actions and using - name: ⬇️ checkout repo
uses: actions/checkout@v4
- name: 🥁 update .npmrc with workspace option
run: |
printf "\nworkspace=\"your-app\"" >> .npmrc
cat .npmrc
git config --global user.email "${{secrets.HEROKU_DEPLOYMENT_EMAIL}}"
git config --global user.name "github-actions[bot]"
git add -A && git commit -m "updated .npmrc with workspace option"
- name: ♊️ deploy to heroku
uses: akhileshns/heroku-deploy@v3.12.14
... |
There is currently no supported way to run a node app from a directory within your git repo. You can work around it most of the time by using
git subtree push
but that wouldn't work if you have shared code outside of your directory.A super common situation is to have the following repo structure:
Being able to direct the buildpack to look in
/server
instead of/
would be helpful for cases like this.The text was updated successfully, but these errors were encountered: