nylas / nylas-nodejs

A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.
MIT License
169 stars 118 forks source link

m is not a constructor #460

Closed denfield-daveyonmayne closed 1 year ago

denfield-daveyonmayne commented 1 year ago

Using v6.10.0 I got an error for new EmailParticipant({..}). A basic usage gives the error:

import EmailParticipant from 'nylas/lib/models/email-participant.js';

// Gives an error m is not a constructor
new EmailParticipant({email: 'foo', name: 'bar'})

Edited: I'm importing in a TypeScript file with compilerOptions:

{
  "compilerOptions": {
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "disableSizeLimit": true,
    "allowJs": true,
    "checkJs": false
  }
}
relaxedtomato commented 1 year ago

Hi @denfield-daveyonmayne - thanks for the message, is the purpose of the code shared to send an email?

relaxedtomato commented 1 year ago

Ah okay, I see, the code sample is creating email participants to send emails to.

denfield-daveyonmayne commented 1 year ago

Hi yes, to send a message per docs.

denfield-daveyonmayne commented 1 year ago

Think I see a potential issue. I'm importing that .js file in a TypScript file. This may be the issue.

relaxedtomato commented 1 year ago

Let me try this out locally - I am suspecting it is the import (we only support CJS, ESM is in the pipeline)

relaxedtomato commented 1 year ago

So swapping import for require works:

const { default: EmailParticipant } = require('nylas/lib/models/email-participant');

and

const sendTo = new EmailParticipant({ email: to });

denfield-daveyonmayne commented 1 year ago

Hi @relaxedtomato in this application, we cannot use require. Is there another workaround using import?

relaxedtomato commented 1 year ago

Okay, will spin up a TypeScript project with your config and take a look soon 👍

relaxedtomato commented 1 year ago

Sharing a related issue for context. I don't see a straightforward path until we support ESM (we are looking into this).

cc: @mrashed-dev

relaxedtomato commented 1 year ago

@denfield-daveyonmayne - let me know if the following works, going to test with your TS config next:


import EmailParticipant from "nylas/lib/models/email-participant.js";

const sendTo = new EmailParticipant.default({ email: 'email@email.com' });
relaxedtomato commented 1 year ago

I had to modify the TS config (first two items)

{
  "compilerOptions": {
    "target": "es2017",
    "module": "es2022",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "disableSizeLimit": true,
    "allowJs": true,
    "checkJs": false
  }
}
denfield-daveyonmayne commented 1 year ago

Hi @relaxedtomato did you got it working on your end? I'm getting

 error TS2339: Property 'default' does not exist on type 'typeof EmailParticipant'

I've also modified my compilerOptions as what you have.

denfield-daveyonmayne commented 1 year ago

If you guys are working on an ES import then I'd use axios where needed until then. I'll close this thread and thanks for your time.

relaxedtomato commented 1 year ago

Sounds good @denfield-daveyonmayne, just an update: @mrashed-dev from our SDK team has shared that ESM support is going into active development in a sprint soon. We'll keep you posted!