aarondfrancis / sidecar

Deploy and execute AWS Lambda functions from your Laravel application.
https://hammerstone.dev/sidecar/docs/main
MIT License
831 stars 58 forks source link

Error: Cannot find module 'image'. Running Laravel on localhost. #21

Open nazrinnoorzan opened 3 years ago

nazrinnoorzan commented 3 years ago

Is this can only be used with Vapor?

I've setup a local Laravel and follow your code implementation example, and I don't have any error when running the command php artisan sidecar:deploy --activate

But on the /ogimage, I got error saying - Lambda Execution Exception for App\Sidecar\OgImage: "Error: Cannot find module 'image' Require stack: - /var/runtime/UserFunction.js - /var/runtime/index.js. [TRACE] Runtime.ImportModuleError: Error: Cannot find module 'image' Require stack: - /var/runtime/UserFunction.js".

nazrinnoorzan commented 3 years ago

image

Owh yeah, and I noticed why the folder and file name here inside the Lambda is difference? Why it becomes __resources/lambda_image.js ? And even I test execute directly from here also fail.

aarondfrancis commented 3 years ago

That's odd, I wouldn't expect to see __resources. Can you share what your PHP class looks like?

aarondfrancis commented 3 years ago

Is this can only be used with Vapor?

Also no, you should be able to run it from localhost just fine!

nazrinnoorzan commented 3 years ago

That's odd, I wouldn't expect to see __resources. Can you share what your PHP class looks like?

app/Sidecar/OgImage.php

<?php

namespace App\Sidecar;

use Hammerstone\Sidecar\LambdaFunction;

class OgImage extends LambdaFunction
{
    public function handler()
    {
        // Define your handler function.
        // (Javascript file + export name.)
        return 'resources/lambda/image.handler';
    }
    public function package()
    {
        // All files and folders needed for the function.
        return [
            'resources/lambda',
        ];
    }
}

resources/lambda/image.js

// const { createCanvas } = require("canvas");
exports.handler = async function (event) {
    console.log(event);
    // const canvas = createCanvas(1200, 630);
    // const context = canvas.getContext("2d");
    // context.font = "bold 70pt Helvetica";
    // context.textAlign = "center";
    // context.fillStyle = "#3574d4";
    // // Read the text out of the event passed in from PHP.
    // context.fillText(event.text, 600, 170);
    // // Return an image.
    // return canvas.toDataURL("image/jpeg");
};

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Sidecar\OgImage;

Route::get('/', function () {
    return view('welcome');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

Route::get('/ogimage', function () {
    return OgImage::execute([
        'title' => 'Executing Functions',
        'url' => 'https://hammerstone.dev/sidecar/docs/main/functions/executing',
        'template' => 'docs/sidecar'
    ]);
});
aarondfrancis commented 3 years ago

image

That's quite strange. I ran it locally and am seeing the expected folder structure.

Are you running this from a Windows machine? I might have to check on that if so

nazrinnoorzan commented 3 years ago

@aarondfrancis

Yup, on Windows 10. For now my work around is edit back the handler function to the correct path created inside the Lambda, in this case return './__resources/lambda/image.handler';

I'll check on my 2nd Windows PC also to see this is happening or not.

aarondfrancis commented 3 years ago

@nazrinnoorzan thanks for the report. I'll try to add a Windows machine to the GitHub actions build so I can test it there. I don't really understand where that would be coming from. It looks like it's also removing the lambda folder and renaming your file lambda_image.js for some reason.

wilsenhc commented 2 years ago

I have this same problem on Windows 11.

Isolated the problem to the Hammerstone\Sidecar\Package class. Paths have an extra \\ at the beginning after removing the base path.

w00key commented 2 years ago

As @wilsenhc said the problem is in Hammerstone\Sidecar\Package class, looks to specifically be

prependBasePath() which uses DIRECTORY_SEPARATORwhich resolves to \ on windows which affects the path written into the zip. Replacing with '/' instead of DIRECTORY_SEPARATOR fixes the issue. Other platforms should already be resolving to /