question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find module '../build/Release/sharp-{platformAndArch}.node on typescript project

See original GitHub issue

Possible install-time or require-time problem

  • I have read the documentation relating to installation.
  • I have ensured that the architecture and platform of Node.js used for npm install is the same as the architecture and platform of Node.js used at runtime.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

Is this a problem with filesystem permissions? NO

If you are using npm v6 or earlier and installing as a root or sudo user, have you tried with the npm install --unsafe-perm flag?

If you are using npm v7 or later, does the user running npm install own the directory it is run in?

If you are using the ignore-scripts feature of npm, have you tried with the npm install --ignore-scripts=false flag?

What is the complete output of running npm install --verbose sharp?

Too Long, but no visible error during this step.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

System: OS: macOS 12.2 CPU: (8) x64 Apple M1 Memory: 35.98 MB / 8.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node Yarn: 1.22.17 - ~/.nvm/versions/node/v14.19.0/bin/yarn npm: 6.14.16 - ~/.nvm/versions/node/v14.19.0/bin/npm npmPackages: sharp: ^0.30.2 => 0.30.2

Suspicions and additional context

I do understand the platform and arch are different but sharp is clever enough to detect the platform and install the appropriate pre-built binaries. In addition, the installation process is restarted on the lambda. Lastly, this error is only occuring at runtime. Project is setup to use typescript. I have tried using different kinds of imports like require(‘sharp’), import {sharp} from ‘sharp’, import * as sharp from ‘sharp’ and import sharp from ‘sharp’ as in other issues but with no luck.

Error on lambda function Screenshot Capture - 2022-03-09 - 11-17-51

@lovell does this library currently support typescript? I dove deep into the documentation but could not find how to use it with typescript. This image shows the error I was encountering both on my local on Mac and on AWS lambda. Please assist.

Error on M1 showing the file actually exists image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
SayefReyadhcommented, May 24, 2022

For AWS lambda deployment with Sharp module from win32-x64 OS, the following worked for me when using serverless, esbuild and serverless-esbuild. Changed the serverless.yml file with the below configuration. It is basically telling esbuild to download sharp again with the following --arch=x64 --platform=linux considering your lambda uses x64 arch. Check serverless-esbuild packager and packagerOptions options for more understanding.

esbuild:
    # keep existing configurations
    external:
      - sharp
    packagerOptions:
      scripts:
        - npm install --arch=x64 --platform=linux sharp

Note: If you are using AWS code build for automatic deployment no other additional script was needed for my end. Also, you need to comment out the script npm install --arch=x64 --platform=linux sharp while testing with serverless-offline from win32-x64 OS

2reactions
floskycommented, Nov 11, 2022

@SayefReyadh I found a good solution that should be added to the sharp docs, I think.

You can set this up quite easily by using the NodejsFunction from aws-cdk-lib/aws-lambda-nodejs. It has esbuild as bundler built in and allows for some useful bundling configurations, which help in this case:

  1. You can tell the bundler to keep certain packages in their node_modules file and not bundle them into the final .js file
  2. You can force the bundler to always run inside a docker container which aligns with the target architecture. So no need to additionally call SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch= arm64 --platform=linux --libc=glibc sharp. I tested it with setting the Lambda architecture to Architecture.ARM_64 and it worked

Just as a reminder, this is for cdk Lambda projects using sharp

Here is a code snippet:

new NodejsFunction(this, 'Lambda', {
            role: someRole,
            handler: 'handler',
            entry: 'path/to/handlerFile.ts',
            functionName: 'sharpImageProcessingLambda',
            runtime: Runtime.NODEJS_14_X,
            architecture: Architecture.ARM_64,
            bundling: {
                externalModules: ['aws-sdk', 'sharp'],
                sourceMap: true,
                nodeModules: ['sharp'],
                forceDockerBundling: true
            }
        });
Read more comments on GitHub >

github_iconTop Results From Across the Web

`Cannot find module` for my own TypeScript module
It was a problem with the tsconfig.json of the project I was using the module in: The module property must be set to...
Read more >
Cannot find module 'typescript' · Issue #707 - GitHub
when I execute: $ npx ts-node, it throwing Cannot find module ... in node without having to init a project, install typescript again...
Read more >
A Complete Guide to Using TypeScript in Node.js - Better Stack
Executing TypeScript source files directly without compilation. Fixing errors caused by missing types. Setting up linting and formatting for TypeScript files.
Read more >
Documentation - Migrating from JavaScript - TypeScript
Importing from Modules. You might start out getting a bunch of errors like Cannot find name 'require'. , and Cannot find name 'define' ......
Read more >
Fixing The "Cannot Find Module" Error In TypeScript
I specialize in React, Node.js & TypeScript application development. If you need help on a project, please reach out, and let's work together....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found