我尝试使用 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 问题,并以这种方式修改了我的代码。但这无济于事。这是讨论的链接。[在此处输入链接描述][问题]