node.jsgraphicsmagick

why i cant use gm(GraphicsMagick) in node project by shell module, but in cmd i can do it


shell.exec(`gm convert ${filePath}[0] ${imagePath}`, (code) => {
    if (!code) {
      console.log("success");
    }
  });

i just want to get a .JPG file according to the first page of .pdf file throw error below

Error: Command failed: /bin/sh: gm: command not found

but in cmd gm that's ok like this

[root@VM-8-4-centos ~]# gm
GraphicsMagick 1.3.35 2020-02-23 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2020 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.
Usage: gm command [options ...]

Where commands include:
    animate - animate a sequence of images
      batch - issue multiple commands in interactive or batch mode
  benchmark - benchmark one of the other commands
    compare - compare two images
  composite - composite images together
    conjure - execute a Magick Scripting Language (MSL) XML script
    convert - convert an image or sequence of images
    display - display an image on a workstation running X
       help - obtain usage message for named command
   identify - describe an image or image sequence
     import - capture an application or X server screen
    mogrify - transform an image or sequence of images
    montage - create a composite image (in a grid) from separate images
       time - time one of the other commands
    version - obtain release version

OS:CentOS

how can i work out this error thank you for your help!


Solution

  • This question is not really node.js related and more about linux in general, but let me explain what happens here. When you type a command like gm, what linux does is it looks into all directories listed in the PATH environment variable and searches for a binary with the given name - 'gm' in this example. When running it in your shell the path variable seems to set correctly including the directory in which gm is present. In your node.js program that is not the case.

    To fix this what i would recommend is to run which gm in your terminal. This will give you the full path to the gm binary and then in your node.js program reference the full path instead of just gm.

    Hope that helps.