image-size-loader icon indicating copy to clipboard operation
image-size-loader copied to clipboard

Error: Module build failed: Error [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string without null bytes. Received type string

Open budarin opened this issue 7 years ago • 3 comments
trafficstars

Hi!

I have a problem with the loader in my project. Webpack 4.6.0 Here is loaders config fo image loader:

            {
                test: /\.(svg|png|jpg|gif)$/,
                include: path.resolve('./src'),
                exclude: path.resolve('node_modules'),
                use: {
                    loader: 'image-size',
                    options: {
                        name: 'img/[name].[hash:7].[ext]',
                    },
                },
            }

Using an image in my code:

import Img from '../../assets/404.png';

trying to run webpack and got an error:

ERROR in ./src/common/assets/404.png
Module build failed: Error [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string without null bytes. Received type string
    at nullCheck (fs.js:177:16)
    at Object.fs.openSync (fs.js:659:3)
    at syncFileToBuffer (...\node_modules\image-size\lib\index.js:58:23)
    at Object.module.exports (...\node_modules\image-size\lib\index.js:100:18)

tryied to guess what is happening - I've put console log into loader

/**
 * @params input - buffer or relative/absolute path of the image file
 * @params callback - optional function for async detection
 */
module.exports = function (input, callback) {

    console.log('typeof input', typeof input, input);

and got in console:

IHDR   �      "�f   sRGB ���   gAMA  ���a   	pHYs  �  ��o�d  ylIDATx^�]`���ݽ�^H	IH��k�E�`��g}>�w}��w��r��C ...

budarin avatar Apr 20 '18 09:04 budarin

I've found that there is another package in my node_modules folder with name 'image-size' so I changed loaders name to

{
                test: /\.(svg|png|jpg|gif)$/,
                include: path.resolve('./src'),
                exclude: path.resolve('node_modules'),
                use: {
                    loader: 'image-size-loader',
                    options: {
                        name: 'img/[name].[hash:7].[ext]',
                    },
                },
            }

and got another error:

ERROR in ./src/common/assets/404.png
Module build failed: TypeError: Cannot read property 'context' of undefined

budarin avatar Apr 20 '18 10:04 budarin

Finally - adding context option makes loader to work properly

            {
                test: /\.(svg|png|jpg|gif)$/,
                include: path.resolve('./src'),
                exclude: path.resolve('node_modules'),
                use: {
                    loader: 'image-size-loader',
                    options: {
                        name: 'img/[name].[hash:7].[ext]',
                        context: path.resolve(__dirname, 'src'),
                    },
                },
            }

something should be documented or fixed

budarin avatar Apr 20 '18 11:04 budarin

making changes to an old repo and getting a similar error:

./src/Assets/media/HeaderImage.jpg Module build failed: TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined

adamaslan avatar Jul 15 '22 18:07 adamaslan