Error: spawn /Users/XXXX/node_modules/optipng-bin/vendor/optipng ENOENT

590 views Asked by At

I'm trying to compress the picture before $npm run build:test in my project(Vue3 + vite)

I wrote a script

I need to overwrite the original image. There are multiple folders under assets / images, so you need to solve the path problem of dist and use imagemin Buffer to rewrite

"prebuild:test": "node imagemin.mjs",
"build:test": "vue-tsc --noEmit && vite build --mode test",

When I execute Npm run build:test

I found him not compressed

Here's my code and my error log

async function processFile(filePath) {
  // filePath = 'src/**/*.{jpg,png,svg,gif}'
  let buffer = await fs.readFile(filePath);
  let content;
  try {
    content = await imagemin.buffer(buffer, {
      plugins
    });

    const size = content.byteLength,
      oldSize = buffer.byteLength;

    if (tinyMap.get(filePath)) {
      tinyMap.set(filePath, {
        ...tinyMap.get(filePath),
        size: size / 1024,
        oldSize: oldSize / 1024,
        ratio: size / oldSize - 1
      });
    } else {
      tinyMap.set(filePath, {
        size: size / 1024,
        oldSize: oldSize / 1024,
        ratio: size / oldSize - 1
      });
    }
    return content;

  } catch (error) {
    console.log(error)
    console.error('imagemin error:' + filePath);
  }
}

Error Msg

Error: spawn /Users/XXXXX/node_modules/optipng-bin/vendor/optipng ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
    at onErrorNT (internal/child_process.js:467:16)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn /Users/XXXXX/node_modules/optipng-bin/vendor/optipng',
  path: '/Users/XXXX/node_modules/optipng-bin/vendor/optipng',
  spawnargs: [
    '-strip',
    'all',
    '-clobber',
    '-o',
    7,
    '-out',
    '/private/var/folders/kf/nr04_xcd22q5v98q2mfftdmc0000gn/T/b8a43d55-094f-4cab-971d-8d2372562498',
    '-fix',
    '-i',
    '0',
    '/private/var/folders/kf/nr04_xcd22q5v98q2mfftdmc0000gn/T/fb9379a3-32f2-4499-b460-2c5bf6a95763'
  ],
  killed: false,
  stdout: '',
  stderr: '',
  failed: true,
  signal: null,
  cmd: '/Users/tuzhixiang/XXXXX/node_modules/optipng-bin/vendor/optipng -strip all -clobber -o 7 -out /private/var/folders/kf/nr04_xcd22q5v98q2mfftdmc0000gn/T/b8a43d55-094f-4cab-971d-8d2372562498 -fix -i 0 /private/var/folders/kf/nr04_xcd22q5v98q2mfftdmc0000gn/T/fb9379a3-32f2-4499-b460-2c5bf6a95763',
  timedOut: false
}
imagemin error:src/view/radar/img/radar-bg.png

0

There are 0 answers