2

When i am doing upload web3 modules on AWS lambda function then getting this error.Please tell me what is reason

{
  "errorMessage": "/var/task/node_modules/scrypt/build/Release/scrypt.node: invalid ELF header",
  "errorType": "Error",
  "stackTrace": [
    "Object.Module._extensions..node (module.js:597:18)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "Object.<anonymous> (/var/task/node_modules/scrypt/index.js:3:20)",
    "Module._compile (module.js:570:32)",
    "Object.Module._extensions..js (module.js:579:10)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "Object.<anonymous> (/var/task/node_modules/scrypt.js/node.js:1:76)",
    "Module._compile (module.js:570:32)",
    "Object.Module._extensions..js (module.js:579:10)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)"
  ]
}
  • You may want to add more information about your issue if you expect help. If the issue is purely related with web3js I would expect to see more people asking about it. What version of web3 do you use? What version of Node in the lambda? – Raul Guiu Jan 28 '18 at 11:51
  • Possible duplicate of [bcrypt invalid elf header when running node app](https://stackoverflow.com/questions/15809611/bcrypt-invalid-elf-header-when-running-node-app) – Raul Guiu Jan 28 '18 at 13:24

2 Answers2

0

The problem is that your scrypt module is compiled for OSX and is not compatible with the OS running the lambdas. In serverless' git the issue is discussed.

To bring the solutions here, a couple are given, first one is by Kennu, he suggest to add a custom "install" script in the package.json:

"install": "[ -e node_modules/sharp/build/Release/sharp.node ] || docker run --rm -v $PWD:/data -w /data node:4 npm install sharp"

The solution I used was given by jokeyrhyme, he suggest to use the following script to be able to run npm install within a docker instance. I copy the code here:

'use strict'

// ideal for use with AWS Lambda and native Node.js modules

// requires Docker: https://docs.docker.com/engine/installation/

/*
Usage:
  node docker-npm.js install
  node docker-npm.js rebuild
*/

const childProcess = require('child_process')

const nodejsImage = 'node:4.3'
const innerWorkingDir = '/src'
const dockerArgs = [
  'run', '-i',
  '-v', `${process.cwd()}:${innerWorkingDir}`,
  '-w', innerWorkingDir,
  nodejsImage, 'npm'
]
const npmArgs = process.argv.slice(2)

const cp = childProcess.execFile(
  'docker',
  dockerArgs.concat(npmArgs),
  {},
  (err, stdout, stderr) => {}
)

cp.stderr.on('data', (data) => console.error(data))
cp.stdout.on('data', (data) => console.log(data))

cp.on('close', (code) => process.exit(code))
Raul Guiu
  • 2,374
  • 22
  • 37
0

If anyone is still having issues with running web3 > 1.0.0 and you're using SAM local to test it, consider running sam build --use-container prior to executing the lambda with sam invoke local .

This will build and install any dependencies inside of a amazon linux container instead of building it locally.

scmilee
  • 21
  • 2