serverless-webpack icon indicating copy to clipboard operation
serverless-webpack copied to clipboard

Unable to import module when using sqlite3

Open RALifeCoach opened this issue 5 years ago • 4 comments

This is a Bug Report

  • What went wrong? Running locally works fine, but running from AWS I get the message,
Unable to import module 'src/middleware': Error
at Function.Module._resolveFilename (module.js:547:15)
  • What did you expect should have happened? I expected it to work on AWS as it does locally.
  • What was the config you used?
service: ffhb-middleware

provider:
  name: aws
  runtime: nodejs8.10

iamRoleStatements:
  - Effect: "Allow"
    Action:
      - "s3:ListBucket"
    Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ]  }

functions:
  middleware:
    handler: src/middleware.middleware
    memorySize: 3008
    events:
      - http:
         path: middleware
         method: get

plugins:
  - serverless-webpack
  - serverless-offline
custom:
  webpack:
      webpackConfig: 'webpack.config.js'
      includeModules: true
      packager: 'npm'

package.json

  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "serverless": "^1.27.3",
    "serverless-offline": "^3.25.4",
    "serverless-webpack": "^5.1.5",
    "webpack": "^4.12.2",
    "webpack-node-externals": "^1.7.2"
  },
  "dependencies": {
    "aws-sdk": "^2.264.1",
    "babel-polyfill": "^6.26.0",
    "babel-runtime": "^6.26.0",
    "sequelize": "^4.38.0",
    "sqlite3": "^4.0.1"
  }
  • What stacktrace or error message from your provider did you see? See above.

  • Serverless-Webpack Version you're using: 5.1.5

  • Webpack version you're using: 4.12.2

  • Serverless Framework Version you're using: 1.27.3

  • Operating System: MAC

  • Stack Trace (if available):

RALifeCoach avatar Jun 27 '18 14:06 RALifeCoach

@RALifeCoach I think that sqlite3 include some native node module, and I had problems with bundling with webpack itself, because it didn't understood that native modules are part of the output.

Can you check if your serverless package zip file include those native modules, and try to manually include them? I think that's possible using serverless include directives, but you should find the correct lib and dependencies in order to run sqlite3 on AWS Lambda.

Disclaimer: It works locally because your function, even the webpack'ed version is able to find those native modules.

geovanisouza92 avatar Jun 28 '18 14:06 geovanisouza92

Hi @RALifeCoach and @geovanisouza92 . This is a duplicate of #383

The reason for the issue is, that sequelize requires all its database dependencies dynamically. So webpack cannot detect them automatically. The solution is, to add sqlite3 as forceInclude as follows:

custom:
  webpack:
    includeModules:
      forceInclude:
        - sqlite3

Additionally you must move the sqlite3 package to your dependencies section in package.json. Currently you have it in devDependencies.

HyperBrain avatar Jun 28 '18 18:06 HyperBrain

Thank-you @HyperBrain. I will give that a try. (Check again, sqlite3 is in dependencies.)

RALifeCoach avatar Jun 29 '18 09:06 RALifeCoach

@RALifeCoach Did you succeed with the forceInclude ?

HyperBrain avatar Jul 04 '18 17:07 HyperBrain