Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

javascript - ImportModuleError","errorMessage":"Error: Cannot find module while using Netlify lambda functions with dependencies

enter image description here

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'
Require stack:
- /var/task/submission-created.js
- /var/runtime/UserFunction.js
- /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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/node-fetch@2.6.1/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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...