AWS Lambda - Runtime.ImportModuleError: Error: Cannot find module '../package.json'

Viewed 577

this is my first question that i ask so if there is any more data i can provide please tellme so.

Currently i am learning how to deploy services to AWS lambda using serverless. In order to bundle my lambda i am using webpack. Everything runs nicely when i use sls offline start. Webpack packages everything and uploads it to aws. However... When i try to test my lambda i get an error: "Runtime.ImportModuleError: Error: Cannot find module '../package.json'"

I tryied looking for similar problems and i did find other people with similar error but it was never concerning package.json

here is my 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'

And here is my 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'] 
              ]
            }
          }
        ]
      }
    ]
  }
};
1 Answers

I found the issue, in my case it was coming from one of the npm packages that i was bundling.

Related