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

Module parse failed: Unexpected character '' (1:0)

Open arrayoutofbounds opened this issue 6 years ago • 12 comments

ERROR in /tmp/seed/source/node_modules/canvas/build/Release/canvas.node 1:0
Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
 @ /tmp/seed/source/node_modules/canvas/lib/bindings.js 3:17-56
 @ /tmp/seed/source/node_modules/canvas/index.js
 @ /tmp/seed/source/services/create.js

Added the canvas library ( also used by merge-images-v2 library which I use as well) to do some qr code manipulation with lambdas and insert into aws storage. Running into an issue, as seen above, when trying to deploy. Looked at the issue as shown in https://github.com/AnomalyInnovations/serverless-bundle/issues/12 and even installed the 1.3.0-beta.2 version with the scripts specific to excluding canvas, but had no success.

My serverless.yml has

custom:
  bundle:
    packagerOptions:
      scripts:
        - rm -rf node_modules/canvas && yarn install --arch=x64 --platform=linux --target=10.16.3 canvas

Would love some help regarding this issue. Thank you for your time!

arrayoutofbounds avatar Dec 09 '19 12:12 arrayoutofbounds

@arrayoutofbounds I think we'll need to deal with this package specifically. Do you have some documentation on how to get this to work with Webpack?

jayair avatar Jan 02 '20 19:01 jayair

I was able to get it to work with the serverless-webpack plugin by using layers for canvas (see https://github.com/jwerre/node-canvas-lambda), and setting the webpack config in serverless.yml like so:

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules:
      forceExclude:
        - canvas

However, now I would like to use serverless-bundle and can't figure out how to get it to work. I get the same error as @arrayoutofbounds Please advise. Thanks!

Susan123456789 avatar Feb 14 '20 19:02 Susan123456789

@Susan123456789 So are you looking for a way to tell serverless-bundle to not package the canvas then?

jayair avatar Feb 23 '20 03:02 jayair

@jayair yes, that would work for me. I ended up forking the serverless-bundle repo and adding canvas to the forceExclude. Can you perhaps expose the forceExclude in serverless-bundle?

photobook-generator avatar Feb 23 '20 13:02 photobook-generator

@jayair sorry, i accidentally responded from the wrong account. That last was from me.

Susan123456789 avatar Feb 23 '20 13:02 Susan123456789

@Susan123456789 Got it. Yeah we'll expose it. But if Canvas cannot be used with Webpack then we should automatically exclude it.

@arrayoutofbounds are you doing something similar with the Canvas library?

jayair avatar Mar 01 '20 22:03 jayair

@jayair

I was able to get it to work by using layers as well. I did the exact same steps as @Susan123456789 .

The issue was that the layers have to be done manually for each lambda function and that is not realistic.

I ended up not using canvas due some issues. But would love to use it in the future along with serverless bundle.

arrayoutofbounds avatar Mar 02 '20 02:03 arrayoutofbounds

@jayair exposing forceExclude would be great. This is the second time I have run into this situation, the first time being with the sharp library. According to this thread it looks like canvas cannot be bundled with webpack: https://github.com/Automattic/node-canvas/issues/867

Susan123456789 avatar Mar 02 '20 18:03 Susan123456789

Got it. Let me start by excluding Canvas then.

Btw, @Susan123456789 for the sharp library, does our documented solution work for you? https://github.com/AnomalyInnovations/serverless-bundle#sharp

jayair avatar Mar 08 '20 18:03 jayair

@jayair Yes, that solution worked for the sharp library

@arrayoutofbounds I'm not sure what you mean by the layers having to be done manually for each lambda. I downloaded the layers from https://github.com/jwerre/node-canvas-lambda and created a serverless.yml file to create the layers like so:

layers:
  canvas:
    name: ${self:custom.stage}-canvas-layer
    package:
      artifact: node10_canvas_layer.zip
  canvaslib64:
    name: ${self:custom.stage}-canvas-lib64-layer
    package:
      artifact: canvas-lib64-layer.zip

resources:
  Outputs:
    CanvasLayerArn:
        Value:
          Ref: CanvasLambdaLayer
        Export:
          Name: ${self:custom.stage}-CanvasLambdaLayerArn
    Canvaslib64LambdaLayerArn:
        Value:
          Ref: Canvaslib64LambdaLayer
        Export:
          Name: ${self:custom.stage}-Canvaslib64LambdaLayerArn

Then I can use them in individual lambdas like so:

  my-lambda:
    handler: my-lambda.main
    layers:
      - 'Fn::ImportValue': ${self:custom.stage}-CanvasLambdaLayerArn
      - 'Fn::ImportValue': ${self:custom.stage}-Canvaslib64LambdaLayerArn

Susan123456789 avatar Mar 09 '20 14:03 Susan123456789

@arrayoutofbounds Hello!Do you have a solution for this problem?Is it saved?I'm having the same problem now.Thank you very much!

1057189519 avatar Dec 03 '21 07:12 1057189519

Unfortunately, I dont have a solution. I ended up doing something else from what I last remember.

arrayoutofbounds avatar Feb 02 '22 12:02 arrayoutofbounds