2017-09-29 36 views
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) 
); 

我能做些什麼來擺脫這個錯誤?

+0

你的'babel'設置可能不正確。當你執行'sls webpack'時檢查編譯的文件。您編譯的文件不應該使用ES6導入,因爲它們與Node v6.10不兼容。 – dashmug

回答

0

我看到這個錯誤,它是非描述性的。我相信這意味着正在拋出一個沒有被捕獲的例外。當出現運行時語法錯誤時也會出現,如使用未定義的變量。這是我的工作流程,與您的工作流程不同,但允許我設置斷點並逐步執行代碼。從這我可以確定壞的路線。

安裝serverless-offline,以便您可以在本地運行項目。安裝Visual Studio代碼,以便您有一個IDE進行調試。我已將我的launch.json發佈到Breakpoints not being hit when debugging Serverless in vscode設置一個斷點,然後用瀏覽器或捲曲命中您的打開點。