Postinstall script fails using netlify-lambda to install function dependencies
babycourageous opened this issue ยท 11 comments
Hi there -
Been trying to figure this out. Not sure if my solution is a fluke or preferred method. I have a site that is connected to Netlify with GitHub Continuous Deployment.
**My Original Settings: **
# netlify.toml
[build]
command = "npm run build"
publish = "_site"
functions = "src/_functions"
netlify-lambda
was a regular dependency and postinstall
script was postinstall: netlify-lambda install
.
Inside src/_functions
was a simple fetch function with node-fetch
as dependency.
Pushing to github resulted in the deploy failing due to missing function dependencies. If I deployed manually with "Clear Cache and Deploy" via the dashboard it worked.
I updated the postinstall
script to the following: netlify-lambda install src/functions
. Still no dice.
Finally I uninstalled netlify-lambda
and re-installed as a devDependency
. Now it works as expected. Pushing to github results in proper function install and site is live.
Hopefully this all makes some sense. Thanks!
UPDATE: This must have been random cause a push this morning resulted in a failed deploy.
CLEAR CACHE AND DEPLOY worked tho.
I think this is the relevant error (at end of log after the other npm scripts run):
6:13:30 PM: Function Dir: /opt/build/repo/src/_functions
6:13:30 PM: TempDir: /tmp/zisi-5e7e7a6af62abc00081d1149
6:13:30 PM: Prepping functions with zip-it-and-ship-it 0.3.1
6:13:31 PM: Error: Could not find "node-fetch" module in file: /dad-joke/dad-joke.js.
6:13:31 PM: Please ensure "node-fetch" is installed in the project.
6:13:31 PM: Error prepping functions
6:13:31 PM: Error running command: Build script returned non-zero exit code: 1
6:13:31 PM: Failing build: Failed to build site
6:13:31 PM: failed during stage 'building site': Build script returned non-zero exit code: 1
6:13:31 PM: Finished processing build request in 31.08675145s
thanks
I am also getting this issue - any push to Netlify will fail, but if I click the "Clear cache and deploy" button, it work properly. I've ping'd @jlengstorf about this - hopefully he can poke the right people!
I think this happens because there is no npm install
when using cached dependencies, so therefore the post install hook never runs. Seems like Netlify doesn't cache the function dependencies though.
Seems weird that we need a post install hook at all - I would expect them to just detect the package.json for us and have it install the deps for us, just like how the rest of Netlify "just works".
Thanks @wesbos!
The postinstall
script is so folks can organize function dependencies alongside the functions rather than in the gumming up the main package.json
- another option I've seen is adding something like && cd [into function dir] &&
npm install` but I don't necessarily care for that if you have more than one function folder (Sorry if ya knew all that already).
Anyway, hope you aren't coding in the shed anymore (unless it's starting to get warm up there)!
swapping postinstall
to prebuild
will circumvent this to unblock you while we dig into how we can improve this
I've raised this internally as well
thanks for all the context here!
thanks a ton Jason!
Thanks @jlengstorf ! I'll give that a whirl.
@jlengstorf & @wesbos - switching from postinstall
per the OG docs to Jason's recommended prebuild
seems to do the trick!
I'll leave this open in case you want to update the issue if you get more info about the postinstall
glitch but feel free to close if ya like.
Thanks!
This prebuild
workaround didn't work for me, possibly because I have a Ruby project, not a JS project. (So my package.json
was purely about invoking netlify-lambda install
.)
I ended up just getting rid of netlify-lambda
and essentially doing cd functions/my-function/ && npm install
as recommended above. (I only have one function so this is fine.)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I think I am running in to this issue as well with using postinstall
as per the docs: https://github.com/netlify/netlify-lambda#netlify-lambda-install. I'll test with prebuild
.