question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack 5: "Module parse failed: Unexpected token" when importing JSON file

See original GitHub issue

Bug report

I think I have found a bug in webpack 5. I’m getting the error Module parse failed: Unexpected token (1:13) from webpack. It points to a node_modules/emoji-mart/data/all.json. emoji-mart is a react library that we are using https://github.com/missive/emoji-mart.

What is the current behavior? When I try to compile my application webpack throws an error saying that it can’t parse the json file. image

 @ ../../../node_modules/emoji-mart/dist-es/utils/emoji-index/emoji-index.js 1:0-42 3:38-42
 @ ../../../node_modules/emoji-mart/dist-es/index.js 1:0-72 1:0-72
 @ ../jsx/MyApp/screens/screen-inner/sections/Tab/Chat/components/Emojis.tsx
 @ ../jsx/MyApp/screens/screen-inner/sections/Tab/Chat/components/TextFieldForm.tsx
 @ ../jsx/MyApp/screens/screen-inner/sections/Tab/Chat/Chat.tsx 34:0-55 199:18-31
 @ ../jsx/MyApp/screens/screen-inner/sections/Tab/Chat/index.tsx 1:0-33 1:0-33
 @ ../jsx/MyApp/screens/screen-inner/sections/Tab/LiveTabs.tsx 36:0-26 250:80-84
 @ ../jsx/MyApp/screens/screen-inner/sections/Tab/index.tsx 1:0-37 1:0-37
 @ ../jsx/MyApp/screens/screen-inner/Detail.tsx 50:0-43 289:18-26 336:18-26
 @ ../jsx/MyApp/screens/screen-inner/index.tsx 32:0-38 131:43-49
 @ ../jsx/MyApp/App.tsx 40:0-44 66:16-25
 @ ../jsx/MyApp/index.tsx 14:0-24 24:67-70

It points to the first line of node_modules/emoji-mart/dist-es/emoji-index/emoji-index.js:

import data from '../../../data/all.json'; // <= THIS ONEEEE
import NimbleEmojiIndex from './nimble-emoji-index';
var emojiIndex = new NimbleEmojiIndex(data);
var emojis = emojiIndex.emojis,
    emoticons = emojiIndex.emoticons;

function search() {
  return emojiIndex.search.apply(emojiIndex, arguments);
}

export default {
  search: search,
  emojis: emojis,
  emoticons: emoticons
};

Maybe it’s related to this? (image from emoji-mart github docs) image

If the current behavior is a bug, please provide the steps to reproduce.

  • Install "emoji-mart": "3.0.0" package
  • Configure babel loader with react
  • Build files with webpack in production mode

Webpack config:

{
 ....
 module: {
   rules: {
     {
  test: /\.(js|tsx|ts)$/,
  include: pathFolder,
  use: [
    {
      loader: 'babel-loader',
      options: {
        cacheDirectory: true,
      },
    },
  ],
},
 {
  test: /\.(graphql|gql)$/,
  include: [
    pathFolder,
    path.join(__dirname, '../../../../node_modules/@xxx'),
  ],
  loader: 'graphql-tag/loader',
},
{
  test: TEST,
  use: [
    'style-loader',
    'css-loader',
   'postcss-loader',
    'sass-loader',
  ],
},
{
  test: /\.svg$/,
  include: pathFolder,
  loader: 'svg-react-loader',
},
   }
 }
 ...
}

What is the expected behavior? It should compile without errors since webpack5 supports json files.

Other relevant information: My solution was to use the json-loader. With json-loader it builds without any errors 🤔 webpack version: 5.36.2 Node.js version: v12.18.2 Operating System: Window 10 Additional tools:

  • “webpack-cli”: “4.6.0”
  • “terser-webpack-plugin”: “5.1.1”
  • “css-minimizer-webpack-plugin”: “^2.0.0”
  • “circular-dependency-plugin”: “5.2.2”
  • “html-webpack-harddisk-plugin”: “2.0.0”
  • “html-webpack-plugin”: “5.3.1”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sokracommented, Jun 2, 2021

Change test: /\.m?js/ to test: /\.m?js$/ in your webpack config.

0reactions
GiancarlosIOcommented, Jun 2, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module parse failed: Unexpected token when using webpack 5
Webpack works in a way that you have to tell it what to do for each specific filetype extension (or however you define...
Read more >
module parse failed: unexpected token (1:0) you may need an ...
Answer. The root cause is that your Typescript rule isn't matching (“currently no loaders are configured to process this file”), so Webpack is...
Read more >
Module parse failed: Unexpected token m in JSON at position ...
[Solved]-Module parse failed: Unexpected token m in JSON at position 0-Reactjs ... Since webpack >= v2.0.0, importing of JSON files will work by...
Read more >
Odyssey Lift-off I: "Module parse failed: Unexpected token" Error
You may need an appropriate loader to handle this file type. Looks like webpack is unhappy for some reason. Could be a few...
Read more >
Build error with @arcgis/core and Angular 11: "Module parse ...
Build error using @arcgis/core with Angular 11: Error: . ... 5:101 Module parse failed: Unexpected token (5:101) File was.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found