I migrated recently my node.js (v20) 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 a commonjs library (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, like if webpack somehow is ignoring the "require" statement, and skipping the whole node-modules/image-size for the final bundle.
What should I take into account in order to ensure the image-size library is included in the bundle?
As module import had issues with image-size module
Runtime.ImportModuleError: Error: Cannot find module 'image-size'"
and you imported it with require
const sizeOf = require('image-size');
as this directly going to search for module inside node_modules
so might be failing so can you relative file provide path, this is for resolve local modules const sizeOf = require('./image-size');
so here ./ prefixed it for that,
and if it is if its node_modules
then import directly they should have exported modules it by default, so no need for relative path declaration for that
and even then you dont get your required methods tobe imported then might you want to look for another dependencies or library which could have build on top of image-size
, and which might offer support that logic