我正在使用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: getwebpack.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)
);我能做些什么来消除这个错误?
发布于 2017-12-07 16:16:31
我见过这个错误,它是非描述性的.我相信这意味着一个没有被抓住的例外正在被抛出。当出现运行时语法错误时,它也会出现,例如使用未定义的变量。下面是我的工作流程,它与您的工作流程不同,但允许我设置断点并逐步遍历代码。从这个我可以识别出坏的路线。
离线安装无服务器服务器,这样您就可以在本地运行项目。安装Visual代码,以便您可以在其中调试IDE。我已经将我的launch.json发布到了Breakpoints not being hit when debugging Serverless in vscode,设置了一个断点,然后用浏览器或卷曲点击了您的位置。
https://stackoverflow.com/questions/46491769
复制相似问题