I'm trying to modify a code generated from aws-nodejs-typescript template. I've installed typeorm, reflect-metadata and pg to work with PostgreSQL.
They are in dependencies in package.json

The webpack config is the default one
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
/*
This line is only required if you are specifying `TS_NODE_PROJECT` for whatever reason.
*/
// delete process.env.TS_NODE_PROJECT;
module.exports = {
context: __dirname,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',
resolve: {
extensions: ['.mjs', '.json', '.ts'],
symlinks: false,
cacheWithContext: false,
plugins: [
new TsconfigPathsPlugin({
configFile: './tsconfig.paths.json',
}),
],
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
optimization: {
concatenateModules: false,
},
target: 'node',
externals: [nodeExternals()],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.(tsx?)$/,
loader: 'ts-loader',
exclude: [
[
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, '.serverless'),
path.resolve(__dirname, '.webpack'),
],
],
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
plugins: [],
};
The problem: after sls deploy the zip archive does not contain the pg package and hence requests fail as Postgres package has not been found installed. Try to install it: npm install pg --save. How do I fix this?
I suspect this might be happening because of a dynamic dependency resolution or whatever as there is no direct dependency in my code and typeorm decides which driver to use based on string in connection configuration.
P.S. sls offline works so the code should be correct and the problem is around this not included package to zip archive.
Yes you're right, there's probably no direct dependency and you use a dynamic requires, i.e. you require modules that are only known at runtime.
So you need to force adding it using the following:
custom:
webpack:
includeModules:
forceInclude:
- pg
See forced inclusion in serverless-webpack.