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

标签 javascript node.js netlify

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 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
如果它仍然不起作用并且是可接受的解决方案,您还可以下载脚本并将其导入本地。
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/

相关文章:

ruby - Jekyll 无法在 Manjaro Linux 上部署 - Sass-Listen-4.0.0 无法在部署时安装(GitLab、Netlify)

git - 尝试使用备用 hexo 主题时无法在 Netlify 上部署网站

javascript - 如何在 EJS 中获取 MySQL 值作为颜色?

javascript - jmeter代码从提取的Url中删除crlf

javascript - 如何从 Angular 应用程序范围之外更改 Angular 中的路由?

node.js - Node 脚本所需的所有文件的列表

webpack - 使用 PostCSS 8 的 Gatsby - 尝试导入错误 : 'component.module.css' does not contain a default export (imported as 'styles' )

javascript - 使用嵌套类获取 Angular 元素

node.js - 当前范围内不存在名称 'Buffer'

javascript - 进一步/递归对象解构