Serverless-webpack 不包含 `pg` 包

Ser*_*gey 5 node.js typescript webpack typeorm serverless

我正在尝试修改从模板生成的代码aws-nodejs-typescript。我已经安装了typeorm,reflect-metadatapg使用 PostgreSQL 。

他们dependenciespackage.json 在此输入图像描述

webpack 配置是默认配置

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: [],
};
Run Code Online (Sandbox Code Playgroud)

问题:sls deploy归档后zip不包含pg包,因此请求失败Postgres package has not been found installed. Try to install it: npm install pg --save。我该如何解决?

我怀疑这可能是由于动态依赖解析或其他原因而发生的,因为我的代码中没有直接依赖,并typeorm根据连接配置中的字符串决定使用哪个驱动程序。

PSsls offline有效,因此代码应该是正确的,问题出在这个未包含的 zip 存档包上。

yve*_*ine 4

是的,你是对的,可能没有直接的依赖关系,并且你使用动态需求,即你需要仅在运行时已知的模块。

因此,您需要使用以下命令强制添加它:

custom:
  webpack:
    includeModules:
       forceInclude:
         - pg
Run Code Online (Sandbox Code Playgroud)

请参阅serverless-webpack中的强制包含