【问题标题】:Import path is different when service is deployed to AWS lambda using serverless framework使用无服务器框架将服务部署到 AWS lambda 时,导入路径不同
【发布时间】:2018-12-20 23:19:42
【问题描述】:

当我将无服务器功能部署到 AWS 时,我得到“找不到模块 '../knexfile'”。当我使用无服务器离线工作时,该导入路径有效。但是,当我部署到 AWS 时,所有包都包含在根级别,因此导入路径不正确。当我将其更改为“knexfile”而不是“../knexfile”时,它在部署时有效,但在本地运行时无效。我该怎么做才能使路径自动成为它需要的路径?

我希望在部署到 AWS 或本地测试时自动解析路径。

【问题讨论】:

  • 需要验证模块是否包含在部署包中...
  • 我在问题中指定了“当我部署到 AWS 时,所有包都包含在根级别”。我能够使用 webpack 解决我的问题。

标签: node.js aws-lambda serverless-framework


【解决方案1】:

我使用 serverless-webpack npm 包解决了这个问题。这最终成为我的 webpack.config.js 文件:

const path = require('path')
const slsw = require('serverless-webpack')
const nodeExternals = require('webpack-node-externals')

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  mode: slsw.lib.webpack.isLocal ? 'development': 'production',
  optimization: {
    // We no not want to minimize our code.
    minimize: false
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false
  },
  devtool: 'nosources-source-map',
  externals: [nodeExternals(),
    {
      'sqlite3': 'sqlite3',
      'mariasql': 'mariasql',
      'mssql': 'mssql',
      'mysql': 'mysql',
      'mysql2': 'mysql2',
      'mssql/package.json': 'mssql/package.json',
      'mssql/lib/base': 'mssql/lib/base',
      'oracle': 'oracle',
      'strong-oracle': 'strong-oracle',
      'oracledb': 'oracledb',
      'pg-native': 'pg-native',
      'pg-query-stream': 'pg-query-stream',
      'tedious': 'tedious'
    }],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader'
          }
        ],
      }
    ]
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
    sourceMapFilename: '[file].map'
  }
}

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 1970-01-01
    • 2021-02-27
    • 2019-08-04
    • 1970-01-01
    • 2021-10-07
    • 2018-05-17
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多