digitalmaas / serverless-plugin-browserifier

Reduce the size and speed up your Node.js based lambda's using browserify.
Other
27 stars 3 forks source link

"Unable to import module" error when using bwip-js #5

Closed real050280 closed 7 years ago

real050280 commented 7 years ago

I received an error when using bwip-js for serverless project. Though this setup works when executed locally, when deployed to AWS, it throws some error.

Unable to import module 'test/index': Error at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) ...

Current configuration is below:

serverless.yml
package:
  individually: true

plugins:
  - serverless-plugin-browserifier
package.json
...
"dependencies": {
    "aws-sdk": "2.104.0",
    "bardcode": "1.0.0",
    "bwip-js": "1.5.6",
    "pdfkit": "0.8.3"
  },
"devDependencies": {
    "chai": "4.1.1",
    "mocha": "3.5.0",
    "nock": "9.0.14",
    "serverless": "1.20.2",
    "serverless-plugin-browserifier": "1.0.5"
  }

Code snippet.

'use strict';

const bardcode = require('bardcode');
const fs = require('fs');
const bwipjs = require('bwip-js');
nolde commented 7 years ago

Hi!

You said "this setup works when executed locally", but how did you execute it locally? If you did that by using sls invoke local, you are not invoking the browserified version, but your raw local version.

To be sure if works after browserified, please run sls package, go to your serverless output folder (usually .serverless folder within the working folder), unzip the function package and run the extracted script.

This looks like an incompatibility between browserify and bwip-js.

real050280 commented 7 years ago

Hello!

Executed it locally by running node test/test.js . Yes, you are right, when I run it locally it didn't use the browserified version of the code. It uses the raw version.

I executed your instructions above, but how should I run the extracted script ?

Also, is there a way to resolve this incompatibilities ? bwip-js is a pure Javascript library translated from Postscript.

By the way, I have tried other barcode library (bardcode) which has no issues even deployed to AWS. Only, that library doesn't suport GS1-128 types.

nolde commented 7 years ago

Hi mate!

You could create a small snippet that calls you code, simulating AWS.

// callback function
function done (err, result) {
  if (err) {
    console.log('failure:', err)
    process.exit(1)
  }
  console.log('success:', result)
}

// example event data
const mockedEventData = {
  myInputData: 'anything you want'
}

const contextData = {
  // context data, if you use it
  someFakeFields: 'to match your needs'
}

// request you code and execute you lambda handler function
const lambda = require('your-browserified-code-path')
lambda.handler(mockedEventData, contextData, done)

That should allow you to debug the browserified code. I will try to add a command in the future that allows you to execute the browserified code using serverless cli.

I have noticed that bwip-js contains a browser compatible version within its package; maybe you could give that a shot.

I will close this for now, as it is not related to the plugin itself. If you need anything else, just ask!

Take care!