无服务节点8.10 babel-runtime

时间:2018-05-12 21:16:21

标签: webpack babel serverless

一切都很完美,但是当我尝试从cli调用lambda时,我得到了错误。

{
    "errorMessage": "Cannot find module 'babel-runtime/helpers/typeof'",
    "errorType": "Error",
    "stackTrace": [
        "Function.Module._load (module.js:474:25)",
        "Module.require (module.js:596:17)",
        "require (internal/module.js:11:18)",
        "Object.<anonymous> (/var/task/src/functions/mobile/register.js:341:18)",
        "__webpack_require__ (/var/task/src/functions/mobile/register.js:20:30)",
        "Object.<anonymous> (/var/task/src/functions/mobile/register.js:265:16)",
        "__webpack_require__ (/var/task/src/functions/mobile/register.js:20:30)",
        "Object.<anonymous> (/var/task/src/functions/mobile/register.js:77:24)",
        "__webpack_require__ (/var/task/src/functions/mobile/register.js:20:30)"
    ]
}

- 更新我不再收到此错误。但是我确实得到“在部署到aws时找不到模块X错误”

我的无服务器减号功能:

service: my-api
provider:
  name: aws
  runtime: nodejs8.10
  stage: ${opt:stage, 'local'}
  region: us-west-2
  role: 
  timeout: 30
  environment:
    STAGE: ${self:provider.stage}
  apiKeys: 
    - apiKey-${opt:stage, 'local'}
  vpc:
 # removed for security
custom:
  webpackIncludeModules:
    packagePath: './package.json'

functions:
  # Mobile Login
package: 
  exclude:
    - .gitignore
    - .git/**
    - .vscode/**
plugins:
  - serverless-offline
  - serverless-webpack

最后是我的webpack代码

const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');
module.exports = {
    entry: slsw.lib.entries,
    target: 'node',
    externals: [nodeExternals()],
    node: {
        __dirname: false,
        __filename: false
    },
    devtool: 'inline-source-map',
    module: {
        loaders: [{
            test: /\.js$/,
            loaders: ['babel-loader'],
            include: __dirname,
        }]
    }
};

更新:我有顶级模块,但他们的子模块没有。

更新

{
    "errorMessage": "Cannot find module 'underscore'",
    "errorType": "Error",
    "stackTrace": [
        "Function.Module._load (module.js:474:25)",
        "Module.require (module.js:596:17)",
        "require (internal/module.js:11:18)",
        "Object.<anonymous> (/var/task/node_modules/promisified-request/promisified-request.js:4:9)",
        "Module._compile (module.js:652:30)",
        "Object.Module._extensions..js (module.js:663:10)",
        "Module.load (module.js:565:32)",
        "tryModuleLoad (module.js:505:12)",
        "Function.Module._load (module.js:497:3)"
    ]
}

1 个答案:

答案 0 :(得分:0)

我最终将此功能添加到我的无服务器中。

custom:
  webpackIncludeModules:
    forceInclude:
      - request
      - underscore

可能有一种更清洁的方式,但这解决了我。