0
我正在创建一个使用es7支持的无服务器框架的项目。但我得到一个意外的标记出口错误,当我尝试在本地运行我的功能使用以下命令如何从无服务器项目中删除'意外令牌导出'错误?
serverless invoke local --f hello
我觉得我已经包含了所需的巴别塔的依赖 -
的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)
);
我能做些什么来摆脱这个错误?
你的'babel'设置可能不正确。当你执行'sls webpack'时检查编译的文件。您编译的文件不应该使用ES6导入,因为它们与Node v6.10不兼容。 – dashmug