繁体   English   中英

使用无服务器框架将服务部署到AWS Lambda时,导入路径不同

[英]Import path is different when service is deployed to AWS lambda using serverless framework

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

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

我使用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'
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM