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.

Importing json files result in SyntaxError: Unexpected token m in JSON at position 0

See original GitHub issue

💥 Regression Report

After upgrade of jest-runtime to 24.8.0 the json file import stopped working because the json file is not imported but transformed into following string

module.exports = "permanentComponentConfigurations.json";

As a workaround I provided a json transformation like so

package.json

    "transform": {
      ".+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      ".+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      ".+\\.json": "<rootDir>/config/jest/jsonTransform.js",
      "(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },

(The configuration from package.json is based on Create-React-App)

jsonTransform.js

module.exports = {
  process(src) {
    return src;
  }
};

Last working version

Worked up to version: 24.7.1

Stopped working in version: 24.8.0

To Reproduce

Import a json file in js like

import PERMANENT_CONFIGURATIONS from 'core/service/config/permanentComponentConfigurations.json';

With 24.8.0 json files are transformed as well. But the resulting json file import results to following content

module.exports = "permanentComponentConfigurations.json";

Expected behavior

The imported json file should be JSON.parsed like before with 24.7.1 per default. If someone needs custom transformation, providing a transformation file is fine, but it should not be a requirement.

Link to repl or repo (highly encouraged)

Please provide either a repl.it demo or a minimal repository on GitHub.

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

  System:
    OS: Windows 10
    CPU: (4) x64 Intel(R) Xeon(R) CPU E3-1226 v3 @ 3.30GHz
  Binaries:
    Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

69reactions
sakulstracommented, May 7, 2019

just a small addition for people accidentally stepping over this:

if you’ve been using

transform: {
    ...,
    '*.': 'babel-jest'
}

28.8 was a breaking change as it will break your json imports. You might want to e.g. restrict the transform to only transform js files.

transform: {
   ...,
    '^.+\\.(js)$': 'babel-jest'
}
6reactions
FlaviooLimacommented, May 15, 2019

@sakulstra After 3 hours of debugging, I must say… You saved my day! I’m almost in tears !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Unexpected token < in JSON at position 0
I experienced this error "SyntaxError: Unexpected token m in JSON at position", where the token 'm' can be any other characters.
Read more >
What Is JSON and How to Handle an “Unexpected Token” Error
Learn what JSON is and how you can deal with errors occurring when parsing JSON data, such as "Unexpected Token < in JSON...
Read more >
Unexpected token < in JSON at position 0 - Dave Ceddia
The root cause is that the server returned HTML or some other non-JSON string. Why would it do that? “Unexpected token o in...
Read more >
How to Fix SyntaxError: Unexpected token < in JSON at ...
In this case the error is thrown when response.json() tries to run and fails to parse the data from the server as JSON....
Read more >
Troubleshoot 'Uncaught SyntaxError: Unexpected token u in ...
The 'Uncaught SyntaxError: Unexpected token u in JSON at position 0' error is caused when the client has been asked to execute JSON.parse() ......
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