使用serverless-webpack部署express api。 Sequelize导入错误

时间:2018-06-07 10:36:01

标签: express sequelize.js serverless-webpack-plugin

我尝试使用serverless-webpack将项目部署到AWS Lambda。但我在导入模型上出现了错误。 以下是来自AWS的错误日志:

module initialization error: ReferenceError
at t.default (/var/task/app.js:1:8411)
at Sequelize.import(/var/task/node_modules/sequelize/lib/sequelize.js:398:32)

我的serverless.yml文件:

service: backend-aquatru

plugins:
  - serverless-webpack
  - serverless-offline
  - serverless-dotenv-plugin

custom:
  webpackIncludeModules:
    forceInclude:
      - pg
      - pg-hstore
  webpackConfig: 'webpack.config.js'
  includeModules: true   
  packager: 'npm'   
provider:
 name: aws
 runtime: nodejs8.10
 stage: dev
 region: us-west-1

functions:
  app:
    handler: app.handler
    events:
     - http: 'ANY /'
     - http: 'ANY {proxy+}'

webpack.config.js

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports = {
  entry: './app.js',
  target: 'node',
  externals: [nodeExternals()],

  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, '.webpack'),
    filename: 'app.js', // this should match the first part of function handler in serverless.yml
  },
  plugins: [
    new Dotenv(),
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: __dirname,
        loaders: ['babel-loader'],
      },
    ],
  },
}

我的续集导入模型:

  import {sequelize as dbConfig} from '../config/vars'
import Sequelize from 'sequelize';

const db = {
  Sequelize,
  sequelize: new Sequelize(dbConfig.makeUri(), {
    operatorsAliases: Sequelize.Op,
    dialect: dbConfig.dialect
  }),
};

// require each model from every endpoint with sequelize
db.User = db.sequelize.import('User', require('../api/models/user.model'));

// run associations from every model here

Object.keys(db).forEach((modelName) => {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

export const User = db.User;
export default db;

我看到了带有require的导入模型文件的serverless-webpack问题,并以这种方式修改我的代码。但它没有帮助。这是讨论的链接。 [在此输入链接描述] [问题]

0 个答案:

没有答案