无服务器(Serverless)Webpack不包含“pg”软件包

5

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

它们在package.json中的dependenciesenter image description here

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: [],
};
问题:在运行sls deploy后,zip压缩包中不包含pg包,因此请求失败,提示Postgres package has not been found installed. Try to install it: npm install pg --save。我该如何解决这个问题?
我怀疑这可能是由于动态依赖关系或其他原因导致的,因为我的代码中没有直接依赖项,并且typeorm根据连接配置中的字符串决定使用哪个驱动程序。
附注:sls offline可以工作,因此代码应该是正确的,问题在于未包含此软件包到zip压缩包中。
1个回答

4

是的,你说得对,可能没有直接依赖,并且你使用了动态requires,也就是说,你需要在运行时require模块。

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

custom:
  webpack:
    includeModules:
       forceInclude:
         - pg

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


1
我已经有了 includeModules: true,如何进行扩展? - Sergey
尝试了这段代码,似乎正常工作,并且没有包含 Node 的软件包,这正是我想要的。谢谢! - Sergey
是的,它工作得很好。谢谢。对于Typescript项目,您需要编写:custom:{webpack:{webpackConfig:'./ webpack.config.js',includeModules:{forceInclude:["pg"]}},} - Vishal Bareja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接