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

**URL:** <https://answers.netlify.com/t/cannot-find-dependency-in-lambda-module-node-fetch/4752>\
**Category:** Support\
**Tags:** lambda-functions, netlify-newbie\
**Created:** [November 5, 2019, 5:15am UTC](https://answers.netlify.com/t/cannot-find-dependency-in-lambda-module-node-fetch/4752 "2019-11-05T05:15:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorsebu](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/dmorsebu/32/3901_2.png) [@dmorsebu](https://answers.netlify.com/u/dmorsebu)\
**Post date:** [November 5, 2019, 5:15am UTC](https://answers.netlify.com/t/cannot-find-dependency-in-lambda-module-node-fetch/4752/1 "2019-11-05T05:15:30Z")

</div>

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](https://answers.netlify.com/t/cannot-find-module-error-when-site-is-live-and-function-is-invoked/1449) which is 1) unclear and 2) conflicts with the [docs that say](https://docs.netlify.com/configure-builds/manage-dependencies/#javascript-dependencies) 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](https://vigilant-euler-d561b4.netlify.com/.netlify/functions/starwars)

```auto
{
"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)"]
}

```

---

<div class="post-metadata">

**Author:** ![Dennis](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/dennis/32/34_2.png) [@Dennis](https://answers.netlify.com/u/Dennis)\
**Post date:** [November 7, 2019, 6:23pm UTC](https://answers.netlify.com/t/cannot-find-dependency-in-lambda-module-node-fetch/4752/2 "2019-11-07T18:23:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dmorsebu](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/dmorsebu/32/3901_2.png) [@dmorsebu](https://answers.netlify.com/u/dmorsebu)\
**Post date:** [November 8, 2019, 8:43am UTC](https://answers.netlify.com/t/cannot-find-dependency-in-lambda-module-node-fetch/4752/3 "2019-11-08T08:43:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![futuregerald](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/futuregerald/32/33_2.png) [@futuregerald](https://answers.netlify.com/u/futuregerald)\
**Post date:** [November 12, 2019, 5:51pm UTC](https://answers.netlify.com/t/cannot-find-dependency-in-lambda-module-node-fetch/4752/4 "2019-11-12T17:51:32Z")

</div>

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 [Tutorials - Netlify Functions](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`: [GitHub - netlify/netlify-lambda: Helps building and serving lambda functions locally and in CI environments](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 [Tutorials - Netlify Functions](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.
