Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token: export #11

Closed
Yorgg opened this issue Oct 26, 2017 · 3 comments
Closed

Unexpected token: export #11

Yorgg opened this issue Oct 26, 2017 · 3 comments

Comments

@Yorgg
Copy link

Yorgg commented Oct 26, 2017

Getting an error: SyntaxError: Unexpected token export
Works fine when function invoked locally, but not on lambda.
I'm guessing that babel isn't transpiling the code?

Error on CloudWatch logs:

Syntax error in module 'handler': SyntaxError
(function (exports, require, module, __filename, __dirname) { export const fetchQuotes = (event, context, callback) => {
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)

serverless.yml

service: blah
plugins:
  - serverless-webpack
custom:
  webpackIncludeModules: true
provider:
  name: aws
  runtime: nodejs6.10
  memorySize: 128
package:
  individually: true

...

webpack.config.js

var slsw          = require('serverless-webpack');
var nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  devtool: 'source-map',
  externals: [nodeExternals()],
  module: {
    rules: [{
      test: /\.js$/,
      loader: 'babel-loader',
      include: __dirname,
      exclude: /node_modules/,
    }]
  }
};

package.json

{
  "name": "blah",
  "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": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-source-map-support": "^1.0.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-3": "^6.24.1",
    "serverless-webpack": "^3.1.0",
    "webpack": "^3.6.0",
    "webpack-node-externals": "^1.6.0"
  },
  "dependencies": {
    "babel-runtime": "^6.26.0",
    "source-map-support": "^0.4.18"
  }
}

handler.js

export const fetchQuotes = (event, context, callback) => {
  const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin" : "*",
      "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
    },
    body: JSON.stringify(event)
  };

  const fetchData = async () => {

  }

  callback(null, response);

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

And output of console after entering sls deploy:

Serverless: Bundling with Webpack...
Time: 1516ms
         Asset     Size  Chunks             Chunk Names
    handler.js  5.48 kB       0  [emitted]  handler
handler.js.map  5.71 kB       0  [emitted]  handler
   [0] ./handler.js 2.31 kB {0} [built]
   [1] external "babel-runtime/core-js/promise" 42 bytes {0} [not cacheable]
   [2] external "babel-runtime/helpers/objectWithoutProperties" 42 bytes {0} [not cacheable]
   [3] external "babel-runtime/regenerator" 42 bytes {0} [not cacheable]
   [4] external "babel-runtime/helpers/asyncToGenerator" 42 bytes {0} [not cacheable]
   [5] external "babel-runtime/core-js/json/stringify" 42 bytes {0} [not cacheable]
   [6] external "source-map-support/register" 42 bytes {0} [not cacheable]
Serverless: Packing external modules: babel-runtime@^6.26.0, source-map-support@^0.4.18
Serverless: fetchQuotes is packaged by the webpack plugin. Ignore messages from SLS.
Serverless: Packaging service...
@jayair
Copy link
Member

jayair commented Oct 26, 2017

@Yorgg Just a quick check, do you have a .babelrc file in your directory?

@Yorgg
Copy link
Author

Yorgg commented Oct 26, 2017

Yessir,

It is in the root (of service folder) and contains:

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

@jayair
Copy link
Member

jayair commented Oct 27, 2017

@Yorgg This looks good. I'd need to try it out to see whats going on. Do you have it in a repo I can use?

@Yorgg Yorgg closed this as completed Nov 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants