ImportModuleError","errorMessage":“错误:在将Netlify lambda函数与依赖项一起使用时找不到模块 [英] ImportModuleError","errorMessage":"Error: Cannot find module while using Netlify lambda functions with dependencies
问题描述
我正在尝试使用netlify及其lambda函数功能来运行具有依赖项的节点函数.基于
这是仓库:
https://github.com/kc1/test2
我尝试将节点模块文件夹和package.json放在多个位置并重新推送存储库,但是我仍然遇到上述错误.我在做什么错了?
请参见 Netlify:未找到构建命令,继续发布获取有关构建过程的新信息
我认为此错误有很多原因,今天我明白了,并尝试了; Netlify函数找不到模块;和无法在lambda函数中使用节点获取
- 在项目根目录中安装依赖项
- 在特定于功能的文件夹中安装deps,例如具有
functions/testfunc/package.json
并将netlify.toml
中的命令更改为cd functions/testfunc&&npm安装&&cd ../../&&npm build
- 将const
fetch = require('node-fetch')
更改为:-
const fetch = require('node-fetch').default
-
const fetch = request('node-fetch').default
-
- 添加
node-fetch
的对等依赖项encoding
但是,这些都不起作用.然后,我尝试查看是否可以从Netlify函数游乐场复制获取示例是否可以正常工作.问题是我的函数中有一个名为 static
const static = ['/blog','/about','/'];
由于某种原因,有一个名为 static 的变量会导致
错误:找不到模块'node-fetch'
我不知道为什么,但是将其更改为其他内容可以解决此问题.
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
I think there's lots of reason for this error, I got it today and tried some solutions from Error: Cannot find module ‘node-fetch’" when deploying function;Netlify Functions Can't find module; and Cannot use node-fetch in lambda function
- Installing dependencies in project root
- Installing deps in functions specific folder e.g. having
functions/testfunc/package.json
and changing command innetlify.toml
tocd functions/testfunc && npm install && cd ../../ && npm build
- Changing const
fetch = require('node-fetch')
to:const fetch = require('node-fetch').default
const fetch = request('node-fetch').default
- Adding
node-fetch
's peer dependencyencoding
However, none of these things worked. I then tried seeing if copying the Fetch example from Netlify functions playground would work and it did. The issue was that I had a variable in my function called static
const static = ['/blog', '/about', '/'];
For some reason having a variable called static causes
Error: Cannot find module 'node-fetch'
I don't know why but changing this to something else fixed the issue.
这篇关于ImportModuleError","errorMessage":“错误:在将Netlify lambda函数与依赖项一起使用时找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!