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.

Parcel loads .json/.js file as .html

See original GitHub issue

🐛 bug report

Parcel loads files with the wrong MIME-type. In this issue I’ll focus on a specific case where .json gets loaded as .html

🎛 Configuration (.babelrc, package.json, cli command)

package.json

{
  "name": "my-website",
  "version": "1.0.0",
  "description": "Website",
  "main": "app.js",
  "scripts": {
    "prod": "parcel build index.html",
    "dev": "parcel index.html",
    "dev-clear": "rm -rf .cache && parcel index.html"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "homepage": "./",
  "dependencies": {
    "@barba/core": "^2.9.7",
    "gsap": "^3.5.1",
    "jquery": "^3.5.1",
    "locomotive-scroll": "^4.0.4"
  },
  "devDependencies": {
    "parcel-bundler": "^1.12.4"
  }
}

🤔 Expected Behavior

It should load the files as the format they actually have. The same issue is happening with .js files that are loaded as .html and thus cause errors

😯 Current Behavior

There should be no error at all Instead this error occurs:

Uncaught SyntaxError: JSON.parse: 
unexpected non-whitespace character after JSON data at line 1 column 3 of the JSON data

Under the network tab one can see the problem clearly json js

Both files, the json and javascript file are getting imported as html files.

💻 Code Sample

This is an already rather larger project so I tried to compress the code sample to the most important part, that I think is crucial for understanding

//THREE.js
 const loader = new THREE.FontLoader();
 loader.load('../fonts/my_font.json', function(font) {
   ...
}

This is what causes the main error, that is discussed in this issue. I don’t really know what I can provide for the second importing/loading issue of the contact.js here, as this just seems to be a loading issue, however if there is something I can add please let me know.

🌍 Your Environment

Software Version(s)
Parcel 1.12.4
Node 12.16.2
npm/Yarn 6.14.9
Operating System Windows 10 - 64

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mischniccommented, Nov 30, 2020

not even docs, so that may not be possible

(Because ~Zeit~ Vercel shut down ~Now 1~ Vercel version 1 and we can’t redeploy the page anymore without changing the backend.)

Parcel 2

Related: https://github.com/parcel-bundler/website/issues/769.

We could probably also mention this here: https://v2.parceljs.org/languages/babel/ (what importing json does). But I’m not sure if this is the place where you would search…

1reaction
101arrowzcommented, Nov 29, 2020

If you’ll always be loading the font, it’s definitely better to inline the JSON because the contents will be sent over GZIPped when you inline, but XHR can’t accept GZIP when you load manually. Plus, one fewer network request. As in, replace the function with:

import fontJSON from '../fonts/my_font.json';

const font = loader.parse(fontJSON);

Switch to Parcel 2 for URL support when you have massive JSON that isn’t always loaded.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML - Parcel
HTML files are often the entry file that you provide to Parcel, and all dependencies including JavaScript, CSS, images, and links to other...
Read more >
How to serve a json file with parcel without bundling it?
In package.json , I include them as parcel *.html tiles/* (for start ) and parcel build index.html tiles/* ...
Read more >
An Introduction to Parcel.js, the No-Config Web Application ...
This is your introduction to Parcel.js, the self-proclaimed super fast, no config web application bundler. Here's what you need to know.
Read more >
Building a basic website with Parcel - DEV Community ‍ ‍
Running this command we are creating a package.json file with default ... Parcel will bundle all our JS, CSS, HTML, file assets, etc...
Read more >
Package management basics - Learn web development | MDN
First of all however, take another look at your package.json file. ... Parcel expects an index.html and an index.js file to work with, ......
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