Search code examples
webpackproj4js

Bundling proj4js with webpack leads to "Unexpected token m in JSON at position 0"


I try to include proj4js (2.5.0) as an ES6 module in a webpack (4.27.1) project.

import proj4 from 'proj4';

That leads to this error:

ERROR in ./node_modules/proj4/package.json
Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports = __w...'
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token m in JSON at position 0 while parsing near 'module.exports = __w...'

What can I do to bypass this error?


Looking at issue reports on the proj4js github page it looks like the json-loader is executed twice since proj4js explicitly requires the package.json:

proj4js/lib/version.js:

export {version as default} from '../package.json';

Solution

  • The problem was caused by this loader in our webpack-config:

    {
         test: /\.(png|gif|jpg|jpeg|svg|xml|json)$/,
         use: ["url-loader"]
    }
    

    I excluded proj4 from this loader and it works.