@mailchimp/mailchimp_marketing/types.d.ts' is not a module in nodeJs
Asked Answered
L

5

11

I imported import @mailchimp/mailchim_marketing in my NodeJS app:

import mailchimp from "@mailchimp/mailchimp_marketing";

However, it gives following error:

type.d.ts is not a module

I have searched to see if there is a @types/@mailchimp/mailchimp_marketing but I couldn't see it.

Legged answered 4/11, 2020 at 18:45 Comment(0)
S
11

The type.d.ts file provided by @mailchimp/mailchimp_marketing doesn't have the types of the library and the package doesn't have a @types package too. So it's necessary to create your own to override the provided by him.

To do this, creates the folders @types/@mailchimp/mailchimp_marketing (one inside another) and creates the file index.d.ts inside mailchimp_marketing.

This file has to contain the declaration of the module, and inside than, the functions and types what you gonna use from library. In my case:

declare module '@mailchimp/mailchimp_marketing' {
  type Config = {
    apiKey?: string,
    accessToken?: string,
    server?: string
  }

  type SetListMemberOptions = {
    skipMergeValidation: boolean
  }

  export type SetListMemberBody = {
    email_address: string,
    status_if_new: 'subscribed' | 'unsubscribed' | 'cleaned' | 'pending' | 'transactional'
    merge_fields?: {[key: string]: any}
  }

  export default {
    setConfig: (config: Config) => {},
    lists: {
      setListMember: (listId: string, subscriberHash: string, body: SetListMemberBody, opts?: SetListMemberOptions): Promise<void> => {}
    }
  }
}

SetListMemberBody has much more fields and setListMember is not void, but i added just what I gonna use. To discover this fields and functions I looked in the source code (https://github.com/mailchimp/mailchimp-marketing-node) and api documentation (https://mailchimp.com/developer/api/marketing/list-members/add-or-update-list-member/).

In my case (Typescript 3.7.3) was not necessary to change tsconfig.json, but if you use older version maybe is necessary to add "typeRoots" for your compilerOptions in tsconfig.json:

"compilerOptions": {
  "typeRoots": ["@types", "node_modules/@types"]`
  // other options
}

After all this, I used the library normally:

import mailchimp, { SetListMemberBody } from '@mailchimp/mailchimp_marketing'
import crypto from 'crypto'

mailchimp.setConfig({
  apiKey: process.env.MAILCHIMP_KEY,
  server: 'serverHere',
});

const listId = 'listIdHere'

export const addSubscriber = async (member: SetListMemberBody): Promise<void> => {
  const hash = crypto.createHash('md5').update(member.email_address).digest('hex')
  await mailchimp.lists.setListMember(listId, hash, member)
}
Sheepherder answered 21/11, 2020 at 23:2 Comment(4)
Why don't you submit this to @types?Electrochemistry
When adding index.d.ts as above I get an error "The expression of an export assignment must be an identifier or qualified name in an ambient context". Reasonably new to TS so just following verbatim and now stuck! Using Typescript 3.8.0 if that helpsFraley
@DavidRitchie - I submitted an edit but here is what I found that fixes the typescript error. ` declare module '@mailchimp/mailchimp_marketing' { ...... type DefaultExport = { setConfig: (config: Config) => void, lists: { setListMember: (listId: string, subscriberHash: string, body: SetListMemberBody, opts?: SetListMemberOptions) => Promise<void> } } const _default : DefaultExport; export default _default; } `Dev
@Electrochemistry - As the types depend a lot on Mailchimp's own API, I think the best way is to be able to generate the types through the Swagger documentation or something similar. In the Github repository that generates the package for node there are some isues and PRs about TypeScript support: github.com/mailchimp/mailchimp-client-lib-codegen/issues/134Sheepherder
S
8

replace your code to this:

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

Right, you wont have type safe but at least your code will work.

Salientian answered 28/12, 2020 at 22:48 Comment(1)
Thanks exactly what I need as noob to TypeScript!Gowk
F
6

Types for @mailchimp/mailchimp_marketing are available meanwhile.

Use
yarn add -D @types/mailchimp__mailchimp_marketing
or
npm install --save-dev @types/mailchimp__mailchimp_marketing
to install the types package.

EDIT: the types do not seem to be complete.

Farci answered 24/2, 2022 at 20:41 Comment(0)
H
2

With a similar issue with @mailchimp/mailchimp_transactional I had to create my own mailchimp__mailchimp_transactional.d.ts with declare module (this package also has just types.d.ts and it is almost empty unlike the types.d.ts in mailchimp_marketing package).

So you can type to create your own type description file using their types.d.ts, place it in @types folder of your project and add @types to tsconfig.json like this:

  "compilerOptions": {
    // other options here
    "typeRoots": [
      "@types",
      "node_modules/@types"
    ]

mailchimp__mailchimp_transactional.d.ts

/* eslint-disable camelcase */
declare module '@mailchimp/mailchimp_transactional' {
...
}
Hyperventilation answered 4/11, 2020 at 19:11 Comment(6)
I created a types folder in the root of my app and copied the types.d.ts file from Mailchimp. I then added @types in the typeroot options in tsconfig but the error still persists...Legged
That's why types.d.ts should wrapped in declare moduleHyperventilation
The error is gone now but when I try to set mailchimp.setConfig it gives an error "roperty 'setConfig' does not exist on type 'typeof import("@mailchimp/mailchimp_marketing")'"Legged
And no setConfig in their types.d.ts at all?Hyperventilation
None which is weiredLegged
try to declare it yourself according to their docs and sourcesHyperventilation
C
0

A quick & dirty solution is to delete the types.d.ts file, which prevents the error, though you will no longer get any type information for the API.

Chadd answered 6/11, 2020 at 19:45 Comment(2)
I want more quick and dirty. How do you set your API key if you do this import route?Feeney
@Feeney same as you would otherwise: mailchimp.setConfig().Chadd

© 2022 - 2024 — McMap. All rights reserved.