简体   繁体   中英

I have installed Sharp globally using npm but I can't use it

Whenever I use sharp --version, it shows sharp command not found

sharp --version
bash: sharp: command not found

I can't use

const sharp= require('sharp');

It gives error of - "Module not found"

If I use it without requiring it, I get error like- "sharp is not defined"

you need to install it locally npm install sharp to be able to require it, to be able to install an npm package for a node.js project you need to have package.json file inside that project folder, use npm init to create that package.json file,

here an exapmle of using sharp to get information of an image

//app.js
const sharp = require("sharp");

async function getMetadata() {
  try {
    const metadata = await sharp("image.png").metadata();
    console.log(metadata);
  } catch (error) {
    console.log(`An error occurred during processing: ${error}`);
  }
}

getMetadata();

run the app

node app.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM