Reputation: 9072
I have the following code I am running that is failing to work because it can not find the models folder when I upload to amazon.
exports.setModels = function(connection,modelPath){
//Import all the known models for the project.
//Proof of Stage being set.
console.log("stage for models="+stage);
const fs = require('fs');
const dir = modelPath;
var models = {};
//@JA - Wait until this function finishes ~ hence readdirSync vs regular readdir which is async
fs.readdirSync(dir).forEach(file => {
console.log("file="+file);
//Split the .js part of the filename
var arr = file.split(".");
var name = arr[0].toLowerCase();
//Create a modle object using the filename as the reference without the .js pointing to a created sequelize instance of the file.
var modelPath = "../models/"+file; //default assumes same directory that was used to scan.
if(process.env.DOMAIN_NAME){ //If this enviroment variable is detected then change the modelPath.
modelPath = "../../../../models/"+file;
}
models[name] = connection.import(modelPath);
})
return models;
}
After investigating the issue, I found out its because the models folder is not being packaged by the serverless webpack plugin.
I found out recently how to force certain packages to upload using this code in my serverless file.
webpackIncludeModules:
forceInclude:
- mysql
- mysql2
This will only seem to include packages however, and when I tried to reference the models folder to automatically include all my sequelize models I got an error saying it was not a package, which of course makes sense.
This does leave me with the question of how I can get it to package the models directory without manually doing a require for each model. I wrote a dynamic function to get them at runtime and import it to sequelize.
Information on the plugin is here (https://github.com/serverless-heaven/serverless-webpack), I looked through it all but can't seem to find the answer.
The output of packaging with serverless looks like this, missing the models folder with all my sequelize models.
My main directory before doing webpack looks like this.
Here is my webpack.config.js as well.
const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
module.exports = {
entry: slsw.lib.entries,
target: "node",
// Since 'aws-sdk' is not compatible with webpack,
// we exclude all node dependencies
externals: [nodeExternals()],
// Run babel on all .js files and skip those in node_modules
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
include: __dirname,
exclude: /node_modules/
}
]
}
};
Upvotes: 1
Views: 4275
Reputation: 2939
you could use the copy-webpack-plugin to include the models instead of referencing.
const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: slsw.lib.entries,
target: "node",
// Since 'aws-sdk' is not compatible with webpack,
// we exclude all node dependencies
externals: [nodeExternals()],
// Run babel on all .js files and skip those in node_modules
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
include: __dirname,
exclude: /node_modules/
}
]
},
plugin: [
new CopyWebpackPlugin(filesToCopy, {})
]
};
make sure you import the models from the right folder since you are importing from relative path. This might change if you're bundle is in a different folder.
Upvotes: 0