Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get sequelize.js to work with serverless-bundle. #45

Open
ggrantrowberry opened this issue Dec 20, 2019 · 5 comments
Open

Can't get sequelize.js to work with serverless-bundle. #45

ggrantrowberry opened this issue Dec 20, 2019 · 5 comments
Labels
question Further information is requested

Comments

@ggrantrowberry
Copy link

I am using a lambda function to parse a file uploaded to s3 then take the data and put it in a remote mysql database. I am using sequelize.js with the mysql2 package. I've deployed my code but I get the this error, {"errorType":"Error","errorMessage":"Please install mysql2 package manually"}. I googled around and found that using serverless-webpack I just need to use the following.

custom:
  webpack:
    includeModules:
      forceInclude:
        - mysql
        - mysql2

https://stackoverflow.com/questions/48554917/getting-sequelize-js-library-to-work-on-amazon-lambda
I tried that but it obviously didn't work with serverless-bundle. I saw a previous issue about using knex.js that looked like a similar problem. I tried installing the serverless-bundle@beta as was suggested but it didn't install properly.

I'm brand new to serverless and I'm not sure what else to do.

@jayair
Copy link
Member

jayair commented Jan 2, 2020

@ggrantrowberry What was the issue you ran into with the new beta? That should address this issue.

You can checkout the Knex.js instructions in the README - https://github.com/AnomalyInnovations/serverless-bundle/tree/beta#advanced-options

@jayair jayair added the question Further information is requested label Jan 2, 2020
@blai
Copy link

blai commented Mar 2, 2020

I ran into the same problem. After some debugging, I found that the dynamic importing of knex.js and sequelize.js isn't the same. While both are dynamic import depending on the configuration, knex does it with a static import at dialect implementation level, while sequelize configure it at dialect level, and invokes require with a variable in a base class. This subtle difference has made a huge difference when webpack transpiles the code, in the sequelize case, webpack would replace the require() into the webpack's empty importer, which does nothing but throw exception.

@blai
Copy link

blai commented Mar 4, 2020

BTW, I found the work around in here. Basically Sequelize config accepts a dialectModule option, which would allow webpack to work around the dynamic import issue. Here's some code fragments I extract from an working example:

// hello.js
import Sequelize from 'sequelize'
import pg from 'pg'

export const hello = async function (event, context, ...rest) {
  const sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USERNAME,
    process.env.DB_PASSWORD,
    {
      host: process.env.DB_HOST,
      dialect: process.env.DB_DIALECT,
      dialectModule: pg
    }
  )
  const result = await sequelize.query('select 1+1 as result')

  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: `Hello ${result}`,
        input: event
      },
      null,
      2
    )
  }
# serverless.yml
service: sls2

provider:
  name: aws
  runtime: nodejs12.x
  environment:
    DB_DIALECT: "postgres"
    DB_NAME: "sls2_name"
    DB_USERNAME: "sls2_username"
    DB_PASSWORD: "sls2_password"
    ...
custom:
  bundle:
    # forceInclude: # NOTE: you don't need this anymore
    #   - pg
    ignorePackages:
      - pg-native

plugins:
  - serverless-bundle

functions:
  hello:
    handler: src/hello.hello

I suggest we create an examples folder, and add these solutions for special cases into there as working (but minimal) examples.

@jayair
Copy link
Member

jayair commented Mar 8, 2020

@blai Thanks for investigating and posting this. Agreed, that we need specific examples. Do you mind contributing one for this case?

Btw, we have this note in the README and I noticed that it has the right option ignorePackages but does not mention the dialectModule. https://github.com/AnomalyInnovations/serverless-bundle#pg

@jayair
Copy link
Member

jayair commented Mar 8, 2020

For now, let me add a note to the README with your instructions. Later we can link to an example.

Edit: Added https://github.com/AnomalyInnovations/serverless-bundle/blob/master/README.md#sequelize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants