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

放肆的年华 提交于 2020-12-13 03:05:46

问题


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.


回答1:


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



回答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' {
...
}



回答3:


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.



来源:https://stackoverflow.com/questions/64685959/mailchimp-mailchimp-marketing-types-d-ts-is-not-a-module-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!