【问题标题】:AWS Lambda - Runtime.ImportModuleError: Error: Cannot find module '../package.json'AWS Lambda - Runtime.ImportModuleError:错误:找不到模块“../package.json”
【发布时间】:2020-10-14 20:08:38
【问题描述】:

这是我提出的第一个问题,如果我可以提供更多数据,请告诉我。

目前我正在学习如何使用无服务器将服务部署到 AWS lambda。为了捆绑我的 lambda,我正在使用 webpack。 当我使用 sls 离线启动时,一切运行良好。 Webpack 打包所有内容并将其上传到 aws。 然而... 当我尝试测试我的 lambda 时,我收到一个错误: “Runtime.ImportModuleError:错误:找不到模块'../package.json'”

我试着寻找类似的问题,我确实找到了其他人有类似的错误,但它从来没有涉及 package.json

这是我的 serverless.yaml

service: products-service

plugins:
  - serverless-webpack
  - serverless-offline
  - serverless-dotenv-plugin
package:
  individualy: true
provider:
  name: aws
  runtime: nodejs12.x
  stage: ${self:custom.stageName.${env:STAGE}, self:custom.stageName.default}
  region: ${env:REGION}
  tracing:
    lambda: true
  
environment:
  db: ${env:MONGODB_URL}
  API_URL:
    {
      'Fn::Join':
        [
          '',
          [
            ' https://',
            { 'Ref': 'ApiGatewayRestApi' },
            '.execute-api.${self:provider.region}.amazonaws.com/${self:provider.stage}',
          ],
        ],
    }
  api: ${self:provider.environment.API_URL}
  rabbit: ${env:RABBIT_URL}

functions:
  products:
    handler: products.handler
    events: ${file(products.js):events}

custom:
  dotenv:
    basePath: ../../
  stageName:
    default: 'local'
    local: 'local'
    dev: 'dev'
    staging: 'staging'
    prod: v${file(../../getVersion.js):major}
  webpack:
    webpackConfig: 'webpack.config.js'   # Name of webpack configuration file
    includeModules: false   # Node modules configuration for packaging
    packager: 'npm'   # Packager that will be used to package your external modules
  webpackIncludeModules:
    packagePath: '../../package.json'

这是我的 webpack.config

const slsw = require('serverless-webpack');
module.exports = {
  target: 'node',
  entry: slsw.lib.entries,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  optimization: {
    minimize: false,
  },
  devtool: 'inline-cheap-module-source-map',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  { targets: { node: '12' }, useBuiltIns: 'usage', corejs: 3 }
                ]
              ],
              plugins: [
                  ['@babel/plugin-proposal-class-properties'] 
              ]
            }
          }
        ]
      }
    ]
  }
};

【问题讨论】:

    标签: webpack aws-lambda runtime-error serverless


    【解决方案1】:

    我发现了问题,就我而言,它来自我捆绑的一个 npm 包。

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2021-11-20
      • 2021-12-21
      • 2017-06-04
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多