sendgrid-nodejs: Cannot import package with TypeScript 2.5.x and 2.6.x
Trying to import the package in a Typescript file:
import * as SendGrid from '@sendgrid/mail';
gives the following error on tsc build:
src/libs/communications.ts(1,27): error TS2497: Module '"<long path>/node_modules/@sendgrid/mail/index"' resolves to a non-module entity and cannot be imported using this construct.
Any known resolution for this issue except using the following workaround?
import SendGrid = require('@sendgrid/mail'); // tslint:disable-line
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 15 (11 by maintainers)
Hi @thinkingserious,
It looks like this SO post addresses this issue: https://stackoverflow.com/questions/39415661/what-does-resolves-to-a-non-module-entity-and-cannot-be-imported-using-this
From what I understand…
In
packages/mail/src/mail.d.ts(https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/src/mail.d.ts), it looks like the export is using CommonJS export, and not ES6export default:Thus, you can’t use ES6 imports as the OP desired:
Instead, he needed to use CommonJS require. It’s not a ‘hack’. It’s just following the same convention:
@ShaharHD thanks for bringing that to our attention, I’ll get a PR put together to address it quickly.
@thinkingserious Referencing the same SO article:
The long-term fix and action item here, is to rewrite any CommonJS
export =to use the ES6export defaultin the sendgrid-nodejs libIf you agree / approve that this is the correct solution, I’d like to take a stab at the fix.
Please advise. Thanks!
Hello @killtheliterate,
Would you mind creating a new issue? Thanks!
With Best Regards,
Elmer
@adamreisnz Oh, right =p Thanks for the feedback and links on ES6 export/import syntax in Node.
@thinkingserious ES6 export/import syntax isn’t supported yet in Node 8 last I checked, so we can’t switch from
requiresto imports etc.https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c
And:
https://tech.io/playgrounds/5262/native-es-modules-in-node-js