node.jstypescriptwebpackminify

How to minify node.js typescript backend project?


My webpack.config.js:

const path = require('path');
var fs = require('fs')

var nodeModules = {};
fs.readdirSync('node_modules').filter(function (x) {return ['.bin'].indexOf(x) === -1;}).forEach(function (mod){nodeModules[mod]= 'commonjs ' + mod;
  });

module.exports = {
  mode: 'development',
  target: 'node',
  entry: './src/server,ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.node$/,
        loader: 'node-loader',
      },
    ],
  },
  externals: [nodeModules],
  devtool: 'source-map',

};

There's an issue with my webpack.config.js file. When I run npm run build, which is configured to execute webpack, I encounter module-related errors. One such error is:

ERROR in ./node_modules/sequelize/lib/dialects/postgres/hstore.js 2:15-35
Module not found: Error: Can't resolve 'pg-hstore' in 'C:\..\node_modules\sequelize\lib\dialects\postgres'
resolve 'pg-hstore' in 'C:\..\node_modules\sequelize\lib\dialects\postgres'
  Parsed request is a module

Solution

  • I resolved this problem by editing webpack.config.js

    var webpack = require('webpack');
    var path = require('path');
    var fs = require('fs');
    var nodeExternals = require('webpack-node-externals');
    
    module.exports = {
      mode: 'development',
      target: 'node',
      entry: './src/server.ts',
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
      },
      resolve: {
        extensions: ['.ts', '.tsx', '.js', '.json'],
        alias: {
          '@': path.resolve(__dirname, 'src'),
          '@config': path.resolve(__dirname, 'src/config'),
          '@controllers': path.resolve(__dirname, 'src/controllers'),
          '@dtos': path.resolve(__dirname, 'src/dtos'),
          '@exceptions': path.resolve(__dirname, 'src/exceptions'),
          '@interfaces': path.resolve(__dirname, 'src/interfaces'),
          '@middlewares': path.resolve(__dirname, 'src/middlewares'),
          '@models': path.resolve(__dirname, 'src/models'),
          '@routes': path.resolve(__dirname, 'src/routes'),
          '@services': path.resolve(__dirname, 'src/services'),
          '@utils': path.resolve(__dirname, 'src/utils'),
          '@sentry/profiling-node': path.resolve(__dirname, 'node_modules/@sentry/profiling-node'),
        },
      },
      module: {
        rules: [
          {
            test: /\.ts$/,
            use: 'ts-loader',
            exclude: /node_modules/,
          },
          {
            test: /\.node$/,
            loader: 'node-loader',
          },
        ],
      },
      externals: [
        nodeExternals(),
      ],
      plugins: [new webpack.ContextReplacementPlugin(/Sequelize(\\|\/)/, path.resolve(__dirname, '../src'))],
      devtool: 'source-map',
    };