Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proper way to include the library for usage in aws lambda with ESM modules? #402

Open
cesarpachon opened this issue Feb 20, 2024 · 7 comments

Comments

@cesarpachon
Copy link

Hello,
I migrated recently my node.js project to ESM modules (type="module" in the package.json, using only ".mjs" files with import syntax) and bundling with webpack for usage in AWS lambda, but I have problems with image-size, it is in node-modules and package json but I get the following runtime error when trying to execute the lambda:

Runtime.ImportModuleError: Error: Cannot find module 'image-size'"

Like if the library is not being bundled. I am using the following statement to include:

const sizeOf = require('image-size');

I suspect I am missing some important steps in order to use image-size in this context.

@stefansundin
Copy link

I think this should do the trick:

import sizeOfImage from 'image-size';

// ...

const size = sizeOfImage.default(buffer);

This should improve when #370 is merged, but who knows when that will happen.

@netroy
Copy link
Member

netroy commented Mar 11, 2024

When you import with import sizeOfImage from 'image-size', you don't need to add a .default. You can just use const size = sizeOfImage(buffer).

who knows when that will happen

Unfortunately, I don't.
Since I haven't started using ESM, and I'm the only maintainer, I'm depending on the folks that need ESM support to implement it, and let me know.

For anyone really wanting to help test this, please try using 2.0.0-beta.1.

If you see the issue pointed out by the last comment on that PR, please feel free to debug and send a PR fixing this. Once enough people asking for ESM support confirm that the beta releases work for them, I'll gladly merge that PR, and release a 2.0.0.

@cesarpachon
Copy link
Author

cesarpachon commented Mar 11, 2024

this is the workaround I finally used:

import * as imageSize from 'image-size';

const sizeOf = imageSize.imageSize;

const dimensions = sizeOf(filepath);

@netroy
Copy link
Member

netroy commented Mar 11, 2024

@cesarpachon Does this also work for you?

import { imageSize } from 'image-size';
const dimensions = imageSize(filepath);

@cesarpachon
Copy link
Author

yeah it worked :)

@netroy
Copy link
Member

netroy commented Mar 11, 2024

and that's with the 2.0.0-beta.1 version, right?

@cesarpachon
Copy link
Author

nope:
"image-size": "^1.1.1",

to clarify, the reason why I opened the ticket is because webpack ignores "require" calls when using with pure mjs modules, so I was forced to try-and-guess what is the proper way to use the library with "import", because the readme only presents examples with require... I looks just like it needs to add the "import" example to the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants