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

Include linux native module bindings in package #308

Closed
joshhunt opened this issue Jan 13, 2018 · 3 comments
Closed

Include linux native module bindings in package #308

joshhunt opened this issue Jan 13, 2018 · 3 comments

Comments

@joshhunt
Copy link

joshhunt commented Jan 13, 2018

I'm using node-sqlite3 in a serverless.js function, and I'm having trouble getting serverless (or serverless-webpack - I'm not 100% sure where the problem lies) to package the correct native bindings.

Thankfully node-sqlite3 uses node-pre-gyp, so I'm able to download and install the required bindings locally on my mac. In my package.json, I have this postinstall script which handles this for me.

"postinstall": "cd node_modules/sqlite3 && ./node_modules/.bin/node-pre-gyp install --target_platform=linux --target=6.10.3"

And I can prove that the bindings are present on my machine, which is exactly what I want

$ ls node_modules/sqlite3/lib/binding/
node-v48-linux-x64  node-v57-darwin-x64

However once I run serverless package (and prevent the .webpack folder from being removed),
I notice that the node-v48-linux-x64 bindings folder doesn't get copied

$ ls ./.webpack/dependencies/node_modules/sqlite3/lib/binding/
node-v57-darwin-x64

It's also not in the zip archive in .serverless:

$ unzip -l ./.serverless/serverless-graphql.zip | grep binding/
  1722644  01-01-1980 00:00   node_modules/sqlite3/lib/binding/node-v57-darwin-x64/node_sqlite3.node

In these above cases, I need the node-v48-linux-x64/node_sqlite3.node file to also be packaged across.

How can I ensure that node_modules/sqlite3/lib/binding/node-v48-linux-x64/** is included in the final package? I've tried using the package.include[] option in serverless.yml, however that doesn't work. I've also tried adding sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node as an entrypoint in the webpack config, but it's still not copied across.

How can I fix this?

@joshhunt joshhunt changed the title Forcefully include file in final package Include linux native module bindings in package Jan 13, 2018
@pavelvlasov
Copy link

@joshhunt you have to use special loader for native modules, e.g. https://github.com/tec27/node-native-loader

@HyperBrain
Copy link
Member

@joshhunt Thanks for asking. @pavelvlasov is right. You should use a proper webpack plugin or loader to get this done. Another one is the copy files plugin - see #205 (comment)

Additionally, the options to include and exclude modules with the webpack plugin is:

custom:
  webpackIncludeModules:
    forceInclude:
      - sqlite3
      - ...
    forceExclude:
      - ...

See also the README.

@joshhunt
Copy link
Author

Thanks for the responses. It looks like copy-webpack-plugin (the original one, not the forked one) the trick to get it working.

For reference, this is how I used it:

plugins: [
    new CopyWebpackPlugin([
      {
        from: './node_modules/sqlite3/lib/binding/**',
        to: './',
      },
    ]),
  ],

And it included the required files:

$ unzip -l ./.serverless/serverless-graphql.zip | grep binding/
  1734829  01-01-1980 00:00   node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node
  1722644  01-01-1980 00:00   node_modules/sqlite3/lib/binding/node-v57-darwin-x64/node_sqlite3.node

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

No branches or pull requests

3 participants