流星中的sendgrid导入问题



我正在尝试在流星中使用sendgrid NPM软件包(在服务器上):

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

继续获取此错误:

(STDERR) packagesmodules.js:961
(STDERR) const {
(STDERR)       ^
(STDERR)
(STDERR) SyntaxError: Unexpected token {
(STDERR)     at Object.exports.runInThisContext (vm.js:53:16)
(STDERR)     at D:myProject.meteorlocalbuildprogramsserverboot.js:331:30
(STDERR)     at Array.forEach (native)
(STDERR)     at Function._.each._.forEach (C:Usersuser1AppDataLocal.meteorpackagesmeteor-tool1.5.2mt-os.windows.x86_32dev_bundleserver-libnode_modulesunderscoreunderscore.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需要node.js版本6及更高,但是在流星中捆绑的一个是 4.8.4

$ meteor node --version
v4.8.4

如本期所述,更新node.js将有所帮助,但显然无法使用流星进行。

我建议您使用 sendgrid npm软件包,这是与node.js v4一起工作的。

最新更新