Hi, I read in the docs that if I have a node.js function ‘npm install’ will be run if there is no yarn.lock file in my directory. I’m a newbie at npm and Netlify.
I know I can’t be the first to encounter this, but I found this link which is 1) unclear and 2) conflicts with the docs that say that npm install will be run when building my project. However, it chokes on
Hello @dmorsebu, the docs you linked to as specifically for site building. Dependencies for lambda functions are handled different. If your function has its own package.json, then our buildbot won’t automatically install those dependencies. That said, can you tell me what you found was unclear in the community post that you share?
The sentence “Bring the node_modules dependencies with you from the function directory” is unclear. What does “bring” mean in a programming/Netlify sense? I’ve never heard “bring” used in a coding context. It would be more helpful to give an example of a lambda function directory structure with the ‘node-fetch’ folder location and contents so I can make sure mine matches.
The other suggestion, “Pre-build your modules with a bundler (like webpack) into your script and target a node build” assume I know what a bundler is, and what webpack is and how to use it. I don’t. It would be more helpful to give me the command to install it and pack my lambda function.
I also don’t understand the phrase “into your script”. What script? My single js file? How would I build something “into my script”? My script is JavaScript, so it would be more helpful to tell me what code I need to add to my .js file.
Lastly, I don’t understand the phrase “target a node build”. I think of builds as compiled programs such as an EXE or a DLL, and that JavaScript files are interpreted, not compiled. So, a “node build” doesn’t make sense to me for an interpreted language. I’m five chapters into a “Teach yourself Node.js” book and it has not mentioned a build.
Sorry about the lack of beginning resources. Many of our instructions assume you are already familiar with the node ecosystem and don’t target those that are closer to the beginning of their learning journey. We typically leave that to community and collect a lot of great posts from them on Tutorials - Netlify Functions.
However you don’t have to bundle the function, you just need to make sure your dependencies are installed since we automatically zip your dependencies with your functions during the build process if you specify a functions directory in your netlify.toml, but that zipping process does not install them for you. This means you’ll have to install them yourself if you have a package.json that’s in your functions directory. If you have the dependencies in the root package.json then we will install those.
If you want to have a separate package.json in your sub-directory then you can install those dependencies using netlify-lambda install, but you’ll have to have netlify-lambda as a dependency in your root package.json so that the command will be available to run during your build. Running netlify-lambda install will look in your functions directory and run npm install for your package.json in there for each of your functions so that the dependencies are available. Typically you run this as part of your build process using npm scripts in your package.json, or just entering it as part of your netlify build command.
I recommend going through the resources on Tutorials - Netlify Functions where you can find tutorials and examples using functions. some of it will be netlify specific, and some will be general info that can help you understand serverless lambda functions better as well as the node ecosystem.