API call error: Cannot set properties of undefined (setting 'setConfig')

Hi everyone,

My first time working with setting up my own backend API service, so please be nice :upside_down_face:

I have the backend lambdas in a functions/ folder. One of them (mailchimp.js) is set up like this:


const mc = require("@mailchimp/mailchimp_marketing");

const apiKey = process.env.MAILCHIMP_API_KEY;

  apiKey: apiKey,
  server: "us8",

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.
  from = "/*"
  to = "/index.html"
  status = 200

# Define which paths this specific [[headers]] block will cover.
for = "/*"
  Access-Control-Allow-Origin = "*"
  functions = "functions/" # netlify dev uses this directory to scaffold and serve your functions

  command = "npm run start"
  port = 8888
  targetPort = 3000
  publish = "dist" 
  autoLaunch = true
  framework = "create-react-app"

  directory = "functions"
  included_files = ["./src/data/payments.json"]

Can somebody help me understand what this error is saying and why it’s happening?

Thanks very much.

fwiw, here’s what my file structure looks like:

  • functions-serve
  • mailchimp
    • src
      • functions
        • mailchimp.js
        • mailchimp.js.map
      • mailchimp.js
      • package.json


  • mailchimp.js

^does this look right?

At first glance @NicoleJaneway, no it does not.

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.

Repo: https://github.com/NicoleJaneway/netlify-build

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).

What do you think?

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’)”

Something, such as an environment variable, is undefined it seems. Have you tried adding in a few console.log() to check the values in the function?

This is not the case - there are no undefined environment variables. Other suggestions?

Without the ability to replicate the error myself, I believe I’m out of suggestions.

Hey @NicoleJaneway,

The error appears to go away if you remove type: module from your package.json.

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.

Actually, they’re served on localhost:8888.

It’s not that line. It’s the line inside that library that’s throwing the error:

That’s where exports.prototype returns undefined if package.json has the "type": "module" specified.

The error appears to go away if you remove type: module from your package.json .

Resolved - thank you so much @hrishikesh!! I was really stumped on this, and you cracked it :blush:

1 Like

Hi thanks for the reply, I cant access the function on port 8888. I noticed this has been resolved, thanks anywaYS

1 Like

@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 ".

Okay, works in development, but now not working in production. Thoughts from the brain trust? @hrishikesh?

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module

Seeing this error with every module. Yes, they’re all in my package.json. Yes, package.json is at the root.

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.