you can also stick it inside the netlify.toml command field, but thats really up to personal preference. bottom line is, you’re responsible for writing the bash commands to install any dependencies that aren’t at the root.
i’m well aware this isn’t ideal. this may change in future iterations of the build bot that we are currently prototyping. but its what we have for now.
Also - I tried adding netlify-lambda to my project. In my root package.json, I then tried netlify-lambda build functions '. This is documented here: Functions overview | Netlify Docs. Doing so gives me a new error:
this is almost a different topic altogether. to use netlify-lambda, be sure to read the docs: “At a high level, netlify-lambda takes a source folder (e.g. src/lambda , specified in your command) and outputs it to a built folder, (e.g. built-lambda , specified in your netlify.toml file).”
given this it would evidently be a bad idea to have your source and dist folders be the same folder. this is what the error is warning you about. you’re coming from a netlify dev workflow, which has no build step, so you don’t have a source and dist folder. For an easy fix, change your functions key inside netlify.toml to something else.
This will be updated with a future version of our build system.
In the meantime you can use this script that will automatically loop through your functions directories and ensure that the dependancies are installed.
and run this in the prebuild scrip of your package.json like so:
Thank you both. I was in a meeting the last 40 minutes or so and was able to quickly test.
So first - yeah, netlify-lamda was a mistake on my part. I was just throwing crap at the wall to see what would stick. I’m going to read your link, but it seems that for how I use Netlify Functions, I can probably avoid it.
Running npm i on the subdirectory worked great, and David, your script to do this dynamically is cool too.
I’m going to write this up in a blog post to specifically cover the use case of:
using netlify dev locally to test
using the CLI to scaffold an app
using code that requires node modules (which will be most functions)
and using Git to deploy where you are not committing node_modules
It sounds like yall may have a way to make this easier in the future too, which is good. Honestly this feels like something I’d have trouble documenting, but until you do get the fix working, maybe a quick reminder at the end of Build functions | Netlify Docs? Before you get into “Tools for Building JavaScript Functions”. And actually, isn’t that section a bit out of date now with the way the netlify function can scaffold an application and netlify dev can run it for you?
Hi @cfjedimaster, as long as you require the dependencies in your lambda functions, what you describe should be handled correctly. Specifically, a file structure like:
/functions/foo.js
/package.json
/node_modules/
Our buildbot uses zip-it-and-ship-it to handle your dependencies, which is flexible enough to handle dependencies in either your root package.json and/or a package.json in your functions folder.
Well, this part: “and/or a package.json in your functions folder” to me means you made it work as easy as possible. So given functions/foo/node_modules and functions/goo/node_modules, both functions requiring what they need and such, Netlify will run npm i for both folders correctly?
The buildbot won’t actually do the npm i except for the root package.json, based on the code as far as I can tell. You will need to do the install in each of those function subfolders individually (as part of your build command). I think maybe it makes more sense to have one package.json in the function folder that will have the dependencies collectively so you only need to run the npm i once.
So… how does that work then? If I need node-fetch in /functions/goo, do I installed it in /functions/ and in goo’s code require(’…/node-fetch’) or some such? Sorry if I’m missing the obvious here.
Yup, you’ll make sure the node-fetch package is in the package.json inside your functions/ folder. During your build command, you’ll need to make sure to run npm i in that folder as well (e.g. cd functions/ && npm i && <rest of your build command>).
Alternatively, you can have node-fetch in your main package.json file so you don’t need to add those commands in your build command.
Lastly, you’re correct that you’ll need to have the require statement as well.
Hopefully that filled in what you were missing. Let me know if it doesn’t.
Yea, that sounds about right. You might not even need to use the absolute path as our buildbot should traverse your repo for package.json files to resolve that module.
I do not have the installer.js nor package.json in functions folder. Also I do not run the prescript.
My goal is to make it work like @cfjedimaster using require(’…/…/SOMETHING’)
I want to use the googleapis. googleapis is in dependencies of package.json in root folder
"dependencies": {
"googleapis": "^45.0.0",
},
I am failing to require the googleapis from root out of my functions/submission-created.js. The function gets called but fails. I get one of those following errors depending on what I am trying.
Unable to import module 'submission-created': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/submission-created.js:1:78)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
var {google} = require('.../googleapis');
var {google} = require('../googleapis');
var {google} = require('/googleapis');
var {google} = require('googleapis');
hi @jakobrosenberg - thank you for your patience. We are still investigating and talking internally about this. We’ll respond here as soon as we have a definitive response!
I have an example in one of my personal repositories:
You’ll see that I have the @google/maps required by my function and it is installed as a dependency in my main package.json. Those are the only two requirements for zip it and ship it to do its thing.
Ok, I’m back. I worked on a site locally with netlify dev. I’ve got a root package.json. Everything worked until I deployed to production and my functions don’t have their dependencies. I thought Netlify would run npm i automatically? Or is the issue that I’m not doing npm i in my function subdirectories?
Also - I really, really, really wish netlify dev would match what netlify does. Having it not act the same way is very frustrating in what is - outside of that - a wonderful tool. I don’t expect things to break when going from my machine to production.