Mailchimp and Node.js with typescript noob question: Import vs Require
Asked Answered
L

5

6

I am creating an app that sends certain transactional emails using Mailchimp.

They have great docs here: https://mailchimp.com/developer/api/transactional/messages/send-using-message-template/

But Im using typescript, so the line:

var mailchimp = require("mailchimp_transactional")("API_KEY");

Doesn't work, I get the following error:

Error: Cannot find module 'mailchimp_transactional'

I know this is something small, but I am not sure how to get around it at all. I found an article that describes creating your own types file here: @mailchimp/mailchimp_marketing/types.d.ts' is not a module in nodeJs

But there has to be a quicker simpler solution. It also doesn't make it clear how to set the API key in that case.

I have tried to import the module which is @mailchimp/mailchimp_transactional which did not work. I have ofcourse also run npm install @mailchimp/mailchimp_transactional

Any help would be appreciated, here is a full sample just incase it helps.

var mailchimp = require("mailchimp_transactional")("API_KEY");

export const testSendEmailFromTemplate = async () => {
    let mcbody = {
        template_name: "my-template",
        template_content: [{
            name:"firstname",
            content:"INJECTED.BY.TEMPLATE.CONT.firstname"
        },
        {
            name:"surname",
            content:"INJECTED.BY.TEMPLATE.CONT.surname"
        }],
        message: {
            to:{
                email:"[email protected]",
                name: "Test",
                type: "to"
            }
        },
        async:true
    };
    return await mailchimp.messages.sendTemplate(mcbody);
}
Lam answered 4/12, 2020 at 15:5 Comment(6)
Your question doesn't seem to have much to do with your title. In particular, there's no "import vs. require" aspect to the question.Expectant
The problem is that the module isn't installed where you're trying to use it (on the server, right?) or your config isn't set up to find modules wherever it is installed.Expectant
@T.J.Crowder good pick-up thanx. I stated that because I'm fairly certain I should be using Import instead, but the mailchimp docs don't specify how to do that. I edited the question to include that. I have also installed the package, I'm running this locally stillLam
You certainly can't combine them as you have (using require for import, but using an export declration for export). Which you use is a choice for your project, controlled by your project options.Expectant
@T.J.Crowder Any recommendations on how to properly import it?Lam
The main think you'll want to do is make sure your configuration is correct (I'm surprised it's not complaining either that A) require is not a function [if configured for JavaScript modules ["ESM"], or B) About a syntax error near export [if configured for CommonJS modules [CJS]). But if you switch to ESM, the equivalent would be: import mailchimpFactory from ("mailchimp_transactional"; const mailchimp = mailchimpFactory("API_KEY");Expectant
L
14

If anyone is unfortunate enough to face this issue because Mailchip's docs don't cater to the typescript setup, and you aren't sure how to make it 'just work' here is the answer:

const mailchimpFactory = require("@mailchimp/mailchimp_transactional/src/index.js");
const mailchimp = mailchimpFactory("PUTKEYHERE");

This pulls in the javascript file directly and then the second line initialises the object.

Good luck all!

Lam answered 5/12, 2020 at 16:21 Comment(2)
is that your actual key?Agglomerate
I still get "require is not defined in ES module scope, you can use import instead" with thatFoam
F
4

As of March 2022 the types have been added to DefinitelyTyped and can be accessed by running

npm install --save-dev @types/mailchimp__mailchimp_transactional

Filthy answered 13/10, 2022 at 16:42 Comment(0)
R
3

For those using it with React, install it first

yarn add @mailchimp/mailchimp_transactional
yarn add @types/mailchimp__mailchimp_transactional -D

And then use like so

import mailchimp from '@mailchimp/mailchimp_transactional'

async function run() {
    const mctx = mailchimp('YOUR-KEY')
    const response = await mctx.users.ping()
    console.log(response)
}

run()
Revels answered 16/8, 2023 at 12:5 Comment(0)
I
1

I had the same problem in a node/Typescript project, but this is working for me:


const mailchimp = require('@mailchimp/mailchimp_marketing')

export class MailchimpServices {

constructor() {

    mailchimp.setConfig({
        apiKey: '...',
        server: 'us5',
    });
}


async ping() {
    console.log('Start mailchimp ping!')
    const response = await mailchimp.ping.get();
    console.log(response);
}

}

Integrator answered 15/1, 2021 at 13:59 Comment(1)
Not working for me.. It says "Error: Cannot find module '@mailchimp/mailchimp_marketing'"Officialese
B
0

in 2023 with typescript I've found it can be done like that:

import * as mailchimp from '@mailchimp/mailchimp_marketing';

or just

import mailchimp from '@mailchimp/mailchimp_marketing';

if you've got esModuleInterop: true in tsconfig. and then:

mailchimp.setConfig({ apiKey: 'your_key', server: 'us10' });
const result = await mailchimp.ping.get();
Britska answered 14/5, 2023 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.