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.

lingui/loader with JSON file parse error

See original GitHub issue

Describe the bug Hi, I actually work with NextJS and I need to implement an i18n. I decided to use lingui over other lib but I encounter and error when I try to use lingui/loader to dynamically load and compile my .json file I got errors: when

warn  - ./src/locale/en/messages.json (./node_modules/@lingui/loader!./src/locale/en/messages.json)
Module parse failed: Unexpected token / in JSON at position 0 while parsing near '/*eslint-disable*/mo...'
File was processed with these loaders:
 * ./node_modules/@lingui/loader/index.js
You may need an additional loader to handle the result of these loaders.
SyntaxError: Unexpected token / in JSON at position 0 while parsing near '/*eslint-disable*/mo...'
    at JSON.parse (<anonymous>)

But if I change my configuration to default ( .po files ) and change my import to import .po file everything works perfectly.

To Reproduce Steps to reproduce the behavior, possibly with minimal code sample, e.g:

_app.js

import { I18nProvider } from "@lingui/react";
import { i18n } from '@lingui/core';
import { useEffect } from 'react';

async function dynamicActivate(locale) {
  const { messages } = await import(`@lingui/loader!./../src/locale/${locale}/messages.json`)
  i18n.load(locale, messages)
  i18n.activate(locale)
}

function MyApp({ Component, pageProps }) {
  const { locale } = pageProps;

  useEffect(async () => {
    if (locale) {
      await dynamicActivate(locale);
    }
  }, [])

  return (
    <I18nProvider i18n={i18n}>
        <Component {...pageProps} />
    </I18nProvider>
  )
}

lingui.config.js

const defaultNextConfig = require('./next.config');

module.exports = {
  "locales": defaultNextConfig.i18n.locales,
  "fallbackLocales": {
    default: defaultNextConfig.i18n.defaultLocale
  },
  "catalogs": [{
    path: "<rootDir>/src/locale/{locale}/messages",
    include: ["<rootDir>"],
    exclude: ["**/node_modules/**", "/.next/", "/build/"]
  }],
  format: "minimal"
}

next.config.js

module.exports = {
  distDir: 'build',
  i18n: {
    locales: ['fr', 'en'],
    defaultLocale: 'fr',
  },
}

Expected behavior Everything works with .json files.

Additional context package.json

{
 "dependencies": {
    "@google-cloud/secret-manager": "^3.6.0",
    "@lingui/react": "^3.8.10",
    "jsonwebtoken": "^8.5.1",
    "next": "10.1.2",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "sass": "^1.32.8"
  },
  "devDependencies": {
    "@babel/core": "^7.13.15",
    "@lingui/cli": "^3.8.10",
    "@lingui/core": "^3.8.10",
    "@lingui/loader": "^3.8.10",
    "@lingui/macro": "^3.8.10",
    "babel-plugin-macros": "^3.0.1"
  }
}

.babelrc

{
  "presets": ["next/babel"],
  "plugins": ["babel-plugin-macros"]
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
semoalcommented, Apr 19, 2021

Meanwhile I fix this issue, I have to speak with some Webpack maintainers to understand a new change on the js parser.

You can use an alternative approach (recommended by the way by the webpack team):

Your webpack configuration:

  webpack: (config, { isServer }) => {
    config.module.rules = [
      ...config.module.rules,
      {
        resourceQuery: /raw-lingui/,
        type: 'javascript/auto',
      }
    ];
  const { messages } = await import(`@lingui/loader!../locale/${locale}/messages.json?raw-lingui`)

This will say to Webpack that .json files with the @lingui/loader import must be loaded (yes or yes) with javascript parser / generator, if not, webpack tries to import it as a json file.

In previous days a workaround was working but right now is not working properly.

In a few days I hope to get a solution for this =)

1reaction
semoalcommented, Apr 19, 2021

It’s the same Loader (was just saying that just today I’ve working on that module), the problem comes with the .json handler, by default Webpack when using .json files tries to use a internal loader of Webpack instead of ours.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack 5 Support · Issue #713 · lingui/js-lingui - GitHub
Running a build returns with the following error. ****/src/locale/fr-CA/messages.json Module parse failed: Abstract method Parser.parse. Must ...
Read more >
JSON.parse error while writing json data into json file
When I try to open the file, I receive an error that says Multiple JSON root elements . import urllib.parse import urllib.request, json...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
SyntaxError: JSON.parse: bad parsing - LambdaTest
SyntaxError is an inherited object of the main error object The main reason behind the error is usually a mistake in the JSON...
Read more >
[SOLVED] Problems with JSON parsing errors - Ubidots API
I want to test setting a variable to a value. My device is called mqttdata and the variable is called number. My curl...
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