Error load json in web3.js

Hi, webpack is so great for build dapp. But I have problem when use webpack to load package web3.

ERROR in ./node_modules/web3/lib/contracts/ICAPRegistrar.json
Module parse failed: Unexpected token m in JSON at position 0
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token m in JSON at position 0
at JSON.parse ()
at JsonParser.parse (/home/vanthanhbk/Documents/FinalProjectBK/MLM/node_modules/webpack/lib/JsonParser.js:15:21)
at doBuild.err (/home/vanthanhbk/Documents/FinalProjectBK/MLM/node_modules/webpack/lib/NormalModule.js:375:32)
at runLoaders (/home/vanthanhbk/Documents/FinalProjectBK/MLM/node_modules/webpack/lib/NormalModule.js:272:12)
at /home/vanthanhbk/Documents/FinalProjectBK/MLM/node_modules/loader-runner/lib/LoaderRunner.js:370:3

File ICAPRegistrar.json:
[{“constant”: true,”inputs”: [{“name”: “_name”,”type”: “bytes32″}],”name”: “owner”,”outputs”: [{“name”: “”,”type”: “address”}],”type”: “function”},{“constant”: false,”inputs”: [{“name”: “_name”,”type”: “bytes32”},{“name”: “_refund”,”type”: “address”}],”name”: “disown”,”outputs”: [],”type”: “function”},{“constant”: true,”inputs”: [{“name”: “_name”,”type”: “bytes32″}],”name”: “addr”,”outputs”: [{“name”: “”,”type”: “address”}],”type”: “function”},{“constant”: false,”inputs”: [{“name”: “_name”,”type”: “bytes32″}],”name”: “reserve”,”outputs”: [],”type”: “function”},{“constant”: false,”inputs”: [{“name”: “_name”,”type”: “bytes32”},{“name”: “_newOwner”,”type”: “address”}],”name”: “transfer”,”outputs”: [],”type”: “function”},{“constant”: false,”inputs”: [{“name”: “_name”,”type”: “bytes32”},{“name”: “_a”,”type”: “address”}],”name”: “setAddr”,”outputs”: [],”type”: “function”},{“anonymous”: false,”inputs”: [{“indexed”: true,”name”: “name”,”type”: “bytes32″}],”name”: “Changed”,”type”: “event”}]

In module, I using json-loader.

module: {
rules: [{
test: /.css$/, // To load the css in react
use: [‘style-loader’, ‘css-loader’],
include: /src/
}, {
test: /.jsx?$/, // To load the js and jsx files
loader: ‘babel-loader’,
include: /src/,
exclude: /node_modules/,
query: {
presets: [‘es2015’, ‘react’, ‘stage-2’]
}, {
test: /.json$/, // To load the json files
loader: ‘json-loader’,
include: [/node_modules/,path.resolve(__dirname, ‘..’)]
test: /.svg$/,
loader: ‘svg-loader?{png:{scale:2}}’
test: /.sol$/,
loaders: [‘web3’, ‘solc’]}

What my problem, anyone can help me, thank so much.

Author: Fantashit

1 thought on “Error load json in web3.js

  1. Okei, Im sorry. But I think I found something.
    I dont understand why use JSON.parse() for object, param input in parse function is json file [{}] .

    class JsonParser {
    constructor(options) {
    this.options = options;

    parse(source, state) {
    const data = JSON.parse(source);
    state.module.buildInfo.jsonData = data;
    state.module.buildMeta.exportsType = “named”;
    if (typeof data === “object” && data)
    state.module.addDependency(new JsonExportsDependency(Object.keys(data)));
    state.module.addDependency(new JsonExportsDependency([“default”]));
    return state;

    I just change: data = source. It solved my problem.

Comments are closed.