简体   繁体   English

webpack5 不从.json 文件导入内容?

[英]webpack5 does not import content from .json file?

I am using webpack5 and could not import.json files.我正在使用 webpack5,无法导入 .json 文件。 As far as I know, I do not need to install loader to import json. N.netheless, I did install "json5-loader" and set this rule:据我所知,我不需要安装 loader 来导入 json。N.netheless,我确实安装了“json5-loader”并设置了这个规则:

rules: [
      {
        test: /\.json5$/i,
        loader: 'json5-loader',
        type: 'javascript/auto',
        // this option did not help neither
        options: {
          esModule: true,
        },
      },
    ],

Building gives me this error either way.无论哪种方式,建筑都会给我这个错误。

SyntaxError: /home/Documents/projects/ethereum/src/contracts/Tether.json: Missing semicolon. (2:16)

  1 | {
> 2 |   "contractName": "Tether",
    |                 ^
  3 |   "abi": [
  4 |     {
  5 |       "inputs": [],

Obviously, it does not recognize JSON files.显然,它不识别JSON个文件。

  import Tether from "./contracts/Tether.json";

this code is working with "create-react-app" but not with webpack5此代码适用于“create-react-app”但不适用于 webpack5

To reproduce the issue I created a simle json:为了重现这个问题,我创建了一个简单的 json:

Name.json:名称.json:

    { "name": "yilmaz" }

And tried to import it in the same directory:并尝试将其导入同一目录:

import Name from "./contracts/Name.json";
// import Tether from "./contracts/Tether.json";

getting this error:收到此错误:

SyntaxError: /home/Documents/projects/ethereum/clarion-ethereum/src/contracts/Name.json: Missing semicolon. (1:8)

> 1 | { "name": "yilmaz" }
    |         ^
  2 |

You may be relying on hot-reload to load your webpack config changes, however this is incorrect and you need to stop and restart the webpack server to see these changes take effect.您可能依赖热重载来加载您的 webpack 配置更改,但这是不正确的,您需要停止并重新启动 webpack 服务器才能看到这些更改生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM