sidecar icon indicating copy to clipboard operation
sidecar copied to clipboard

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

Open nazrinnoorzan opened this issue 4 years ago • 9 comments

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 avatar Jul 07 '21 10:07 nazrinnoorzan

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.

nazrinnoorzan avatar Jul 07 '21 15:07 nazrinnoorzan

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

aarondfrancis avatar Jul 07 '21 23:07 aarondfrancis

Is this can only be used with Vapor?

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

aarondfrancis avatar Jul 07 '21 23:07 aarondfrancis

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'
    ]);
});

nazrinnoorzan avatar Jul 08 '21 02:07 nazrinnoorzan

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

aarondfrancis avatar Jul 08 '21 13:07 aarondfrancis

@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.

nazrinnoorzan avatar Jul 08 '21 14:07 nazrinnoorzan

@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.

aarondfrancis avatar Jul 08 '21 14:07 aarondfrancis

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.

wilsenhc avatar Feb 18 '22 16:02 wilsenhc

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

prependBasePath() which uses DIRECTORY_SEPARATOR which 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 /

w00key avatar May 06 '22 19:05 w00key