Sendgrid-nodejs: 运行时示例 sendgrid node.js 代码中的语法错误。

创建于 2017-08-29  ·  4评论  ·  资料来源: sendgrid/sendgrid-nodejs

问题摘要

当我复制示例 sendgrid 代码并尝试在我的服务器上运行它时,mail.js 中出现语法错误

繁殖步骤

  1. npm 安装 @sendgrid/mail
  2. 在我的 bash 环境中导出环境变量 SENDGRID_API_KEY
  3. 复制示例代码
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

最后一步:
运行代码

# node sendgrid.js
/root/scripts/node_modules/@sendgrid/mail/src/mail.js:6
const {
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/root/scripts/sendgrid.js:1:78)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

技术细节:

# npm list |grep sendgrid
├─┬ @sendgrid/[email protected]
│ ├─┬ @sendgrid/[email protected]
│ │ ├── @sendgrid/[email protected] deduped
│ └─┬ @sendgrid/[email protected]
├─┬ [email protected]
│ └── [email protected]
npm ERR! extraneous: [email protected] /root/scripts/node_modules/jsprim/node_modules/assert-plus
npm ERR! extraneous: [email protected] /root/scripts/node_modules/verror/node_modules/assert-plus
npm ERR! extraneous: [email protected] /root/scripts/node_modules/dashdash/node_modules/assert-plus
npm ERR! extraneous: [email protected] /root/scripts/node_modules/getpass/node_modules/assert-plus
npm ERR! extraneous: [email protected] /root/scripts/node_modules/sshpk/node_modules/assert-plus
# npm -v 
5.3.0
# node -v
v4.8.4
# echo $SENDGRID_API_KEY
SG.jyr8dVjFTBO4MayuTAS_zA.7a9Qg_XXXXXX
help wanted question

最有用的评论

你好@kiranshashiny

我相信这对您不起作用,因为此 SDK 的 v6 不支持 Node v4。

您是否可以访问使用 Node v6 或更高版本的机器?

最好的问候,

埃尔默

所有4条评论

你好@kiranshashiny

我相信这对您不起作用,因为此 SDK 的 v6 不支持 Node v4。

您是否可以访问使用 Node v6 或更高版本的机器?

最好的问候,

埃尔默

这是正确的,错误发生在 Node 4 不支持的解构语句上。

我升级到节点 8.x,它可以工作。

关闭问题,因为修复是升级。

此页面是否有帮助?
0 / 5 - 0 等级