Meteor 中的 Sendgrid 导入问题

Sendgrid import issue in Meteor

我正在尝试在 Meteor 中(在服务器上)使用 sendgrid npm 包:

const sendgridMail = require('@sendgrid/mail');

不断出现此错误:

(STDERR) packages\modules.js:961
(STDERR) const {
(STDERR)       ^
(STDERR)
(STDERR) SyntaxError: Unexpected token {
(STDERR)     at Object.exports.runInThisContext (vm.js:53:16)
(STDERR)     at D:\myProject\.meteor\local\build\programs\server\boot.js:331:30
(STDERR)     at Array.forEach (native)
(STDERR)     at Function._.each._.forEach (C:\Users\user1\AppData\Local\.meteor\packages\meteor-tool.5.2\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)

有什么解决办法吗?

更新: package.json 包括以下依赖项:

"dependencies": {
    "@sendgrid/client": "^6.1.4",
    "@sendgrid/mail": "^6.1.4",
    "babel-runtime": "^6.20.0",
    "bcrypt": "^1.0.2",
    "body-parser": "^1.17.2",
    "card": "^2.3.0",
    "google-auth-library": "^0.10.0",
    "googleapis": "^21.3.0",
    "meteor-node-stubs": "~0.2.4",
    "moment": "^2.18.1",
    "pnotify": "^3.2.0",
    "shortid": "^2.2.8",
    "simpl-schema": "^0.3.1",
    "stripe": "^4.24.0"
}

我是这样用的,效果不错。

import sendgridModule from 'sendgrid';

let SEND_GRID_API_KEY = '';
try {
  SEND_GRID_API_KEY = Meteor.settings.env.SEND_GRID_API_KEY;
} catch (e) {
  // no-op
}
const sendgrid = sendgridModule(SEND_GRID_API_KEY);

我认为使用 import 而不是 require 是首选,它现在可以用于条件导入

问题是 SendGrid SDK v6 requires Node.js version 6 and higher,但 Meteor 中捆绑的是 4.8.4:

$ meteor node --version
v4.8.4

this issue 所述,更新 Node.js 会有所帮助,但显然不能用 Meteor 完成。

我建议你使用sendgrid npm package, this one works fine with Node.js v4