gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:13:01 31 4
gpt4 key购买 nike

enter image description here
我正在尝试使用 netlify 及其 lambda 函数功能来运行具有依赖关系的 Node 函数。基于 https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget/ ,我在我的函数/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}`)
....
当我查看我的 netlify 网站功能选项卡(上面的截图)时,我看到:
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"]}
我的 package.json (运行 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"
}
}
包结构如下:
enter image description here
这是 repo :
https://github.com/kc1/test2
我尝试将 Node 模块文件夹和 package.json 放在多个位置并重新推送存储库,但我仍然收到上述错误。我究竟做错了什么?
编辑:
请看 Netlify: No build command found, continuing to publishing有关构建过程的新信息

最佳答案

你应该只有一个 package.jsonbase directory .如果这不起作用,请尝试删除 package-lock.json然后再试一次。不要推node_modules文件夹自 Netlify运行 npm install .

If your build requires any JavaScript dependencies, you must listthese in a package.json file saved in the site's base directory inyour repository.

...

By default, if your site's base directory does not include a yarn.lockfile (more information below), we will run npm install to install thedependencies 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
如果它仍然不起作用并且是可接受的解决方案,您还可以下载脚本并将其导入本地。
require('./fetch.min.js')
https://cdn.jsdelivr.net/npm/node-fetch@2.6.1/browser.min.js
如果这不起作用,我会尝试 Run a local development environment为了更好地调试并查看您在生产环境中实际获得的软件包。
https://docs.netlify.com/cli/get-started/#run-a-local-development-environment
我能想到的最后一件事是检查 Node.jsRequire()可以找到 node_modules但这是一个真正的远景:
https://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm

关于javascript - ImportModuleError","errorMessage":"Error: Cannot find module while using Netlify lambda functions with dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65134613/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com