sindresorhus / gulp-imagemin

Minify PNG, JPEG, GIF and SVG images
MIT License
1.9k stars 157 forks source link

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null #360

Closed ghost closed 2 years ago

ghost commented 3 years ago

Nameless is Here!

So I'm trying to make a gulp task which optimises images (obviously). Here are my files:

gulpfile.esm.js

import gulp from 'gulp';
import images   from './gulp/tasks/images.js';
import config   from './gulp/config.js';

const build = () => gulp.series(
    gulp.parallel(
        images.build
    )
);

const watch = () => gulp.series(
    build,
    gulp.parallel(
        images.watch
    )
);

let defaultTask;
if (config.env() === 'prod') {
    defaultTask = build;
} else {
    defaultTask = watch;
};

export default defaultTask;

./gulp/tasks/images.js

import gulp     from 'gulp';
import plumber      from 'gulp-plumber';
import changed      from 'gulp-changed';
import gulpIf       from 'gulp-if';
import imagemin     from 'gulp-imagemin';
import pngquant     from 'imagemin-pngquant';
import config       from '../config.js';

const build = () => (
    gulp.src(`${config.source.images}/**/*`)
        .pipe(plumber())
        .pipe(changed(config.build.images))
        .pipe(gulpIf(config.env() === 'prod', imagemin([
            pngquant({ quality: [ 0.8, 0.9 ] }),
            imagemin.mozjpeg({ quality: 80 }),
            imagemin.svgo()
        ])))
        .pipe(gulp.dest(config.build.images))
);

const watch = () => gulp.watch(`${config.source.images}/**/*`, build);

export default { watch, build };

The error I get:

[21:14:04] Requiring external module esm
node_modules/gulp-imagemin/index.js:1
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:119:11)
    at Object.basename (node:path:1309:5)
    at Error.<anonymous> (node:internal/errors:1450:55)
    at getMessage (node:internal/errors:421:12)
    at new NodeError (node:internal/errors:348:21)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1128:19) {
  code: 'ERR_INVALID_ARG_TYPE'


Any help is appreciated! Sorry for my terrible english.

chriswthomson commented 3 years ago

I also get this. Reverting to 7.1.0 solves it. Looks to me like an issue with the conversion to ESM.

jmartsch commented 3 years ago

Same problem here

ghost commented 2 years ago

You can solve this problem with renaming gulpfile.esm.js back to gulpfile.js and adding "type": "module" to package.json. However, there will be some restrictions:

  1. There must be a gulpfile.js file directly in the root directory. So you can't have an index.js file inside gulpfile.js directory instead.
  2. Any import of a JavaScript file (except modules installed with npm) must contain an extension.

For now this is the only solution I've found for 8.0.0.

chriswthomson commented 2 years ago

My setup is as described (recently converted the package so it no longer uses esm). I get the error, but only on SVG files.

// Copy images
function images() {
    return gulp.src(`${config.src}/img/**/*`)
        .pipe(imagemin([
            mozjpeg({
                quality: 80,
            }),
            svgo({
                plugins: [
                    {removeViewBox: false},
                ]
            }),
            imageminPngquant({
                quality: [0.6, 0.8],
                dithering: 1,
            }),
        ]))
        .pipe(gulp.dest(`${config.dist}/img`));
}

If I remove the SVG files from src/img the error doesn't happen.

sindresorhus commented 2 years ago

The SVGO config format changed: https://github.com/sindresorhus/gulp-imagemin/commit/a1579c0a88232f58ce2b8b5e95e24570240fb1a4

chriswthomson commented 2 years ago

Thanks @sindresorhus, changing the config format fixed the issue for me.

DaveyJake commented 2 years ago

The SVGO config format changed: a1579c0

Please add this to the documentation as it would've save a lot more time.

ghost commented 2 years ago

Hi there. Sorry for noticing so late. Currently, I don't have resources to try it out, so I'll close the issue and reopen it if anything goes wrong. Thanks!