
I'm trying to use netlify and its lambda function feature to run a node function with dependencies. Based on https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget/ , I have in my functions/submission-created.js:
const fetch = require('node-fetch');
exports.handler = async event => {
const email = JSON.parse(event.body).payload.EMAIL
const asking = JSON.parse(event.body).payload.ASKING
console.log(`Recieved a submission: ${email}`)
....
When I look under my netlify websites functions tab (secreenshot above) , I see:
11:40:35 PM: 2020-12-02T04:40:35.092Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task/submission-created.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'node-fetch'","Require stack:","- /var/task/submission-created.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1015:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)"," at Module.load (internal/modules/cjs/loader.js:879:32)"," at Function.Module._load (internal/modules/cjs/loader.js:724:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
my package.json (after running npm init):
{
"name": "site",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"node-fetch": "^2.6.1"
}
}
The package structure looks like:

here is the repo :
https://github.com/kc1/test2
I've tried putting the node modules folder and package.json in multiple places and repushing the repo, but I'm still getting the error above. What am I doing wrong?
EDIT:
please see Netlify: No build command found, continuing to publishing for new info on build process
You should only have one package.json in the base directory. If that does not work then try to delete package-lock.json and try again. Do not push the node_modules folder since Netlify runs npm install.
If your build requires any JavaScript dependencies, you must list these in a package.json file saved in the site's base directory in your repository.
...
By default, if your site's base directory does not include a yarn.lock file (more information below), we will run npm install to install the dependencies listed in your package.json file.
https://docs.netlify.com/configure-builds/manage-dependencies/#javascript-dependencies
https://docs.netlify.com/configure-builds/get-started/#definitions
If it still does not work and is an acceptable solution you could also download the script and import it locally.
require('./fetch.min.js')
https://cdn.jsdelivr.net/npm/[email protected]/browser.min.js
If this does not work I would try to Run a local development environment for better debugging and see what packages you actually get in the production environment.
https://docs.netlify.com/cli/get-started/#run-a-local-development-environment
The last thing that I can think of is to check if Node.js And Require() can find node_modules but it is a real long shot:
https://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm
I tried most of these options without success. Eventually I ended up deleting the site and rebuilding it from my github repo, this time without the node modules. It built and now I can use npm modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With