Search code examples
webpackserverlesspg

Webpack not including all dependencies on deploying serveless application


Am getting an error when I am deploying serverless lambda function on AWS

"errorMessage": "Please install pg package manually"

I observe that serverless is not including all dependencies from pakage.json. pg and pg-hoster package is missing.

Serverless: Packing external modules: source-map-support@^0.5.19, dotenv@^10.0.0, ajv@^8.6.1, 
ajv-errors@^3.0.0, @middy/core@^1.5.2, @middy/http-json-body-parser@^1.5.2, [email protected], 
bcryptjs@^2.4.3, jsonwebtoken@^8.5.1

my webpack.config file is

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-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', 'js'],
    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: [
  new ForkTsCheckerWebpackPlugin({
    eslint: {
       files: './src/**/*.{ts,tsx,js,jsx}', // required - same as command `eslint ./src/**/*. 
      {ts,tsx,js,jsx} --ext .ts,.tsx,.js,.jsx`
      },
    }),
  ],
};

and my serverless.ts file is look like

import type { AWS } from '@serverless/typescript';
import lambdas from './src/handlers/index';

const serverlessConfiguration: AWS = {
  service: 'tapit-backend',
  frameworkVersion: '2',
  custom: {
    webpack: {
      webpackConfig: './webpack.config.js',
      includeModules: true,
    },
  },
  plugins: ['serverless-webpack', 'serverless-offline', 'serverless-prune-plugin'],
  provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    apiGateway: {
      minimumCompressionSize: 1024,
      shouldStartNameWithService: true,
     },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      NODE_ENV: 'dev',
    },
    lambdaHashingVersion: '20201221',
  },
  // import the function via paths
  functions: lambdas,
};

module.exports = serverlessConfiguration;

could someone please help me where i am doing wrong. please


Solution

  • Full credit to this blog post

    You are developing a NodeJS + Webpack + Sequelize + pg + pg-hstore application. You compile everything and when you execute your webpack bundle, you have the following error

    throw new Error(`Please install ${moduleName} package manually`);
    Error: Please install pg package manually
    

    You need to remove all nodes_modules librairies from your final bundle with NPM package “webpack-node-externals”.

    To do this, just install it with

    npm install --save-dev webpack-node-externals
    

    In your webpack.config.js file, you need to import the plugin with

    const nodeExternals = require('webpack-node-externals');
    

    and in the “externals” section of this file, you need to declare

    externals: [
        nodeExternals(),
    ],
    

    Your final bundle will be smaller and you won’t have the Error: Please install pg package manually error message anymore.