【问题标题】:Serverless-webpack not including `pg` packageServerless-webpack 不包括 `pg` 包
【发布时间】:2021-07-08 06:17:19
【问题描述】:

我正在尝试修改从aws-nodejs-typescript 模板生成的代码。我已经安装了 typeormreflect-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: [],
};

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

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

附: sls offline 有效,因此代码应该是正确的,问题在于这个未包含的压缩包压缩包。

【问题讨论】:

    标签: node.js typescript webpack typeorm serverless


    【解决方案1】:

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

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

    custom:
      webpack:
        includeModules:
           forceInclude:
             - pg
    

    serverless-webpack 中查看forced inclusion

    【讨论】:

    • 我已经有includeModules: true了如何扩展它?
    • 按原样尝试了此代码。似乎正在工作,并且不包括我想要的 Node 包。谢谢!
    • 是的,它工作正常.. 谢谢。 & 对于打字稿项目你需要写custom: {webpack: {webpackConfig: './webpack.config.js',includeModules:{ forceInclude: ["pg"] }},}
    猜你喜欢
    • 2016-09-01
    • 2019-12-26
    • 2019-04-12
    • 1970-01-01
    • 2017-10-31
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    相关资源
    最近更新 更多