当我将无服务器功能部署到AWS时,出现“找不到模块'../knexfile'”。当我使用无服务器离线工作时,该导入路径有效。但是,当我部署到AWS时,所有软件包都包含在根级别,因此导入路径不正确。当我将其更改为“ knexfile”而不是“ ../knexfile”时,它在部署时有效,但在本地运行时不起作用。我要怎么做才能使其成为自动需要的路径?
我希望该路径在部署到AWS或在本地测试时能够自动解决。
答案 0 :(得分:0)
我使用了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'
}
}