At one point, I was able to make calls to this endpoint, but now for whatever reason, Iām getting the following error:
Request from ::1: PUT /.netlify/functions/mailchimp
{ālevelā:āerrorā,āmessageā:āEnd - Error:ā}
{āerrorMessageā:āCannot set properties of undefined (setting āsetConfigā)ā,āerrorTypeā:āTypeErrorā,ālevelā:āerrorā,āstackTraceā:[ānode_modules/@mailchimp/mailchimp_marketing/src/ApiClient.js (/Users/nicolejaneway/ReactProjects/data-strategy-pros/node_modules/@mailchimp/mailchimp_marketing/src/ApiClient.js:36:13)ā,ā__require (/Users/nicolejaneway/ReactProjects/data-strategy-pros/.netlify/functions-serve/mailchimp/src/functions/mailchimp.js:2:44)ā,ānode_modules/@mailchimp/mailchimp_marketing/src/index.js (/Users/nicolejaneway/ReactProjects/data-strategy-pros/node_modules/@mailchimp/mailchimp_marketing/src/ApiClient.js:570:49)ā,ā__require (/Users/nicolejaneway/ReactProjects/data-strategy-pros/.netlify/functions-serve/mailchimp/src/functions/mailchimp.js:2:44)ā,āObject. (/Users/nicolejaneway/ReactProjects/data-strategy-pros/.netlify/functions-serve/mailchimp/src/functions/mailchimp.js:20846:10)ā,āModule._compile (node:internal/modules/cjs/loader:1103:14)ā,āObject.Module._extensionsā¦js (node:internal/modules/cjs/loader:1157:10)ā,āModule.load (node:internal/modules/cjs/loader:981:32)ā,āFunction.Module._load (node:internal/modules/cjs/loader:822:12)ā,āModule.require (node:internal/modules/cjs/loader:1005:19)ā]}
Hereās my netlify.toml
# The following redirect is intended for use with most SPAs that handle
# routing internally.
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[[headers]]
# Define which paths this specific [[headers]] block will cover.
for = "/*"
[headers.values]
Access-Control-Allow-Origin = "*"
[build]
functions = "functions/" # netlify dev uses this directory to scaffold and serve your functions
[dev]
command = "npm run start"
port = 8888
targetPort = 3000
publish = "dist"
autoLaunch = true
framework = "create-react-app"
[functions]
directory = "functions"
included_files = ["./src/data/payments.json"]
Can somebody help me understand what this error is saying and why itās happening?
The .netlify directory is created by Netlify CLI when building/serving functions.
If the project you are working on has a package.json, then any dependencies required for functions will go into it too. If the project does not have a package.json, you would add it to the directory where the function code resides (before building).
In the first instance, dependencies are installed as part of the build and they are used when building the functions.
In the second instance they arenāt, so you would need a build command like
cd functions && npm i && cd ..
which instructs Netlify build to move into the functions, install dependencies, then move back out before starting the build. Functions are built after any site building.
If you have a repository you can share, this could help ensure everything is hunky dory.
The calls to mailchimp iin APItest.js file are both failing. Itās like the netlify function doesnāt have access to the required package (@mailchimp/mailchimp_marketing).
If the build is running as a production build devDependencies arenāt loaded. As @mailchimp/mailchimp_marketing is a under devDependencies and not dependencies this could explain why the package is missing.
Okay, @coelmay, thanks for this input. Iāve moved the mailchimp package to dependencies in package.json, deleted package-lock.json and node-modules/, and npm installed.
Running netlify dev, and I get the same error:
āTypeError: Cannot set properties of undefined (setting āsetConfigā)ā
The development environment is wrongly set. I have tried to replicate your code locally without success.
I cloned your repo and ran netlify dev and tried to do some debugging. What node version are you using?
Netlify dev functions are not served in the standard 8888 port with path /functions, they are served from a random port, they are just served as localhost:(randomport)/mailchimp. This should be fixed, adhere to default Netlify settings standards, is best.
I was able to get back the same error, this is the line throwing the error:
const mc = require("@mailchimp/mailchimp_marketing");
That line is throwing the errror. Have you tried using this module ("@mailchimp/mailchimp_marketing") with Express.js or similar?
I am not a React expert, however I have dealt with netlify functions in the past and have learned to implement them.
@NicoleJaneway you should always change this when using CommonJS modules and ārequireā. Otherwise you need to use the default of āmoduleā which is ESM (ES6) based "import ".
Update: I had to add the packages as external_node_modules within [functions] section of netlify.toml to get this to work. Iām still having problems, but Iāll start a new thread.
Iād have assumed for it to work even in production. Happy to follow-along your next thread. I can see that itās here: API call fails - inconsistent error. Iāll get to it in order of other threads.