AWS lambda SyntaxError:意外的令牌导入

时间:2019-01-22 19:10:13

标签: node.js aws-lambda aws-api-gateway serverless-framework aws-serverless

我遵循了serverless-stack.com上的指南,将我的api在一个单存储库中分为多种服务。 该代码可以通过seed.run很好地部署,但是当我尝试从reactjs Webclient到达某些端点时,如果从我的api中获取错误。

我尝试console.log错误,但是它没有透露更多细节。我从stackowerflow上的其他问题中猜测,它可能与ES6或babel有关,但我不确定。

我的.babelrc看起来像这样:

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

我在AWS(cloudwatch)上收到以下错误:

Syntax error in module 'list': SyntaxError
(function (exports, require, module, __filename, __dirname) { import * as dynamoDbLib from "./libs/dynamodb-lib";
^^^^^^

SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18) . 

有什么想法我可能会错过吗?

已更新:

我的babelrc:

{
  "presets": [
      [
          "env",
          {
              "targets": {
                  "node": "8.1"
              }
          }
      ]
  ]
}

我的webpack.config.js

const nodeExternals = require("webpack-node-externals");

module.exports = {
  entry: slsw.lib.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()],
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  optimization: {
    // We no not want to minimize our code.
    minimize: false
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false
  },
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        include: __dirname,
        exclude: /node_modules/
      }
    ]
  }
};

Package.json

{
  "name": "api",
  "version": "1.1.4",
  "description": "Backend API for APP.",
  "main": "handler.js",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@types/node": "^10.12.18",
    "body-parser": "^1.18.3",
    "nodemon": "^1.18.9",
    "aws-sdk": "^2.224.1",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-plugin-source-map-support": "^1.0.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-3": "^6.24.1",
    "jest": "^21.2.1",
    "serverless-offline": "^3.18.0",
    "serverless-webpack": "^5.1.0",
    "webpack": "^4.2.0",
    "webpack-node-externals": "^1.6.0"
  },
  "dependencies": {
    "babel-runtime": "^6.26.0",
    "source-map-support": "^0.4.18",
    "stripe": "^5.8.0",
    "uuid": "^3.2.1"
  }
}

2 个答案:

答案 0 :(得分:0)

我成功地使用Babel版本6将代码部署到运行Node 8.1的lambda函数。我遇到了与您上面提到的完全相同的问题。我的解决方案:

.babelrc文件

{
    "presets": [
        [
            "env",
            {
                "targets": {
                    "node": "8.1"
                }
            }
        ]
    ]
}

来自package.json的Dev依赖项

  "devDependencies": {
    "@types/node": "^10.12.18",
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "nodemon": "^1.18.9",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1"
  },

package.json中的脚本

    "clean": "rm -rf dist",
    "build": "npm run clean && babel ./lambda/custom/ -d dist && cp lambda/custom/package.json dist/",
    "start": "nodemon",
    "serveo": "ssh -R 80:localhost:3000 serveo.net",
    "deploy": "npm run build && ask deploy",

答案 1 :(得分:0)

导入语法在Node v8.x.x中不可用。改为使用 require ,或像bagdan Alexandru Militaru一样使用babel对其进行编译。