Cannot find dependency in lambda (module 'node-fetch')

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

const fetch = require('node-fetch');

My lambda function URL: https://vigilant-euler-d561b4.netlify.com/.netlify/functions/starwars

{
"errorMessage":"Cannot find module 'node-fetch'",
"errorType":"Error",
"stackTrace":["Function.Module._load (module.js:474:25)",
"Module.require (module.js:596:17)",
"require (internal/module.js:11:18)",
"Object.<anonymous> (/var/task/starwars.js:3:15)",
"Module._compile (module.js:652:30)",
"Object.Module._extensions..js (module.js:663:10)",
"Module.load (module.js:565:32)",
"tryModuleLoad (module.js:505:12)",
"Function.Module._load (module.js:497:3)"]
}

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.

Hi there,

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 https://functions.netlify.com/tutorials.

We do have a library based on webpack that you can use to bundle your function without configuring webpack, it’s netlify-lambda: https://github.com/netlify/netlify-lambda

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 https://functions.netlify.com/tutorials 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.