如何删除“意外令牌”导出'无服务器项目的错误?

时间:2017-09-29 15:06:37

标签: babel serverless-framework

我正在使用支持es7的无服务器框架创建项目。但是当我尝试使用以下命令在本地运行我的函数时,我收到意外的令牌导出错误

serverless invoke local --f hello

我想我已经包含了所需的babel依赖项 -

的package.json

{
  "name": "olep-app-api",
  "version": "1.1.0",
  "description": "A starter project for the Serverless Framework with ES7 support",
  "main": "handler.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/AnomalyInnovations/serverless-es7.git"
  },
  "devDependencies": {
    "aws-sdk": "^2.94.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-3": "^6.24.1",
    "js-yaml": "^3.8.2",
    "serverless-webpack": "^2.0.0",
    "webpack": "^3.0.0",
    "webpack-node-externals": "^1.6.0"
  },
  "dependencies": {
    "babel-runtime": "^6.23.0",
    "source-map-support": "^0.4.14"
  }
}

.babelrc

{
  "plugins": ["transform-runtime"],
  "presets": ["es2015", "stage-3"]
}

serverless.yml

service: olep-17-api

plugins:
  - serverless-webpack

custom:
  webpackIncludeModules: true

provider:
  name: aws
  runtime: nodejs6.10
  stage: prod
  region: ap-south-1

  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:ap-southeast-1:*:*"

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

webpack.config.js

var yaml = require('js-yaml');
var fs = require('fs');
var path = require('path');
var nodeExternals = require('webpack-node-externals');

var handlerRegex = /\.[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/;
var include = './_webpack/include.js';
var entries = {};

var doc = yaml.safeLoad(fs.readFileSync('serverless.yml', 'utf8'));

// Find all the handler files in serverless.yml
// and build the entry array with them
for (var key in doc.functions) {
  var handler = doc.functions[key].handler;
  var entryKey = handler.replace(handlerRegex, '');

  // Add error handling and source map support
  entries[entryKey] = [include, './' + entryKey + '.js'];
}

module.exports = {
  entry: entries,
  target: 'node',
  // Generate sourcemaps for proper error messages
  devtool: 'source-map',
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [{
      test: /\.js$/,
      loader: 'babel-loader',
      include: __dirname,
      exclude: /node_modules/,
    }]
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  }
};

handler.js

export const hello = async (event, context, callback) => {
  const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
      "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS 
    },
    body: JSON.stringify({
      message: `Go Serverless v1.0! ${(await message({ time: 1, copy: 'Your function executed successfully!'}))}`,
      input: event,
    }),
  };

  callback(null, response);
};

const message = ({ time, ...rest }) => new Promise((resolve, reject) => 
  setTimeout(() => {
    resolve(`${rest.copy} (with a delay)`);
  }, time * 1000)
);

我该怎么做才能摆脱这个错误?

1 个答案:

答案 0 :(得分:0)

我看到了这个错误,并且它是非描述性的。我相信这意味着抛出的异常并未被捕获。当出现运行时语法错误时,它也会出现,例如使用未定义的变量。这是我的工作流程与您的工作流程不同,但允许我设置断点并逐步执行代码。由此我可以识别坏线。

安装无服务器脱机,以便您可以在本地运行项目。安装Visual Studio代码,以便有一个IDE可以调试。我已经将我的launch.json发布到Breakpoints not being hit when debugging Serverless in vscode设置一个断点然后用浏览器或卷曲点击你的enpoing。