You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React.js基础教程第13章:webpack加载JSON模块解析失败求助

Fixing "Unexpected token m in JSON at position 0" with Webpack & React

Hey there, let's work through this error you're hitting when trying to load your titles.json file with webpack. That "Unexpected token m" at position 0 is a dead giveaway that something's off right at the start of your JSON file, or how webpack is handling it. Here are the most likely fixes:

1. Check your titles.json file for syntax errors

This is the #1 cause of this error. JSON has strict rules, and even a tiny mistake can break parsing:

  • Double-check the first character: It should be either { (for an object) or [ (for an array). The error says "token m" at position 0—did you accidentally type something like module (from a JS module) at the start? Or maybe a stray comment, space, or hidden character?
  • No allowed comments: JSON doesn't support // or /* */ comments. If you added notes to your JSON file, remove them.
  • Use double quotes only: All keys and string values must be wrapped in double quotes ("key": "value"), not single quotes.
  • No trailing commas: Make sure you don't have a comma after the last key-value pair in an object or last item in an array.

2. Verify your webpack configuration (for older versions)

Webpack 4+ supports loading JSON files out of the box, but if you're using an older version (3 or below), you'll need to set up the json-loader:

  • First, install the loader:
    npm install json-loader --save-dev
    
  • Then add this rule to your webpack.config.js:
    module.exports = {
      // ... other config
      module: {
        rules: [
          // ... other loaders (like babel for JS)
          {
            test: /\.json$/,
            use: 'json-loader'
          }
        ]
      }
    };
    

3. Check for hidden file encoding issues

Sometimes files saved with UTF-8 BOM (byte order mark) add a hidden character at the start, which can cause this error. To fix this:

  • Open your titles.json in a code editor like VS Code
  • Click the encoding label in the bottom-right corner (usually says "UTF-8")
  • Select "Save with encoding" > "UTF-8 without BOM"
  • Rebuild your project

Start with checking the JSON file itself—9 times out of 10, that's where the problem is. Let me know if you still run into issues after trying these steps!

内容的提问来源于stack exchange,提问作者penone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:27:50