You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
file-loader fails with JSON files in Webpack 4 #259
Using import 'file-loader!./manifest.json'; fails with;
Module parse failed: Unexpected token m in JSON at position 0
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token m in JSON at position 0
at JSON.parse (<anonymous>)
at JsonParser.parse (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/webpack/lib/JsonParser.js:15:21)
at doBuild.err (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/webpack/lib/NormalModule.js:367:32)
at runLoaders (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/webpack/lib/NormalModule.js:264:12)
at /Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:370:3
at iterateNormalLoaders (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:211:10)
at iterateNormalLoaders (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:218:10)
at /Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:233:3
at runSyncOrAsync (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:130:11)
at iterateNormalLoaders (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:229:2)
at Array.<anonymous> (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/loader-runner/lib/LoaderRunner.js:202:4)
at Storage.finished (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:43:16)
at provider (/Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:79:9)
at /Users/jeremy/Documents/Development/webpack-file-loader-test/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:505:3)
Reacted by Minh Phan (Paul), Koike Michitaro, Nikolai Orekhov, Asad Sahi, Marc McIntyre, Emir Marques, Arnaud, Michael Zalla, Grzegorz Lachowski, Quentin Rousseau and 38 more
@seewebpack/webpack#6586. Why is the file-loader even needed here ? Either explicitly add the type to the rule in webpack.config.js or use a different extname for inline usage (e.g x.manifest). The inline loader syntax is discouraged for a while now and it would even work in case x-loader supports module.type = 'json'.
For anyone who's encountering the error Module parse failed: Unexpected token m in JSON at position 0 like I was, and you find this thread, you don't need to specify that json files be loaded in any special way.
No special tests, configuration, or loaders are needed for .json to be required in your JavaScript.
Reacted by Jason Minnick, rohin123, johnjerrico, pilapila, Hichem ben chaabene, Ricardo Matias, Yury Romanov, Chad Ridings, Ilya Pronin, mekbib-tekle and 17 more
Reacted by Brendon, Aron Jones, Mihail ZI, TemaKozyrev, mibcadet, Wu Haotian, Reet Chowdhary, James Reetzke, Egor Komarov and Smackymander
Reacted by crloshro and Nick Brady
Reacted by Hichem ben chaabene, Nick Brady, Paul Zmuda, Nathan Leiby, Nikhil Das Nomula and Vladimir Podolyan
Reacted by Mario Jerkovic, Hichem ben chaabene, Chad Ridings, crloshro, Nick Brady and ikrainiev
@BobbyBabes: I fully agree; it'd be best to fetch json with an async request most of the time.
In this case we were loading a mandatory language file, so the require isn't so bad. If you try to do that with an import tree shaking gets in the way and the file isn't loaded.
My point was mostly that no webpack configuration is required for it to understand the loading of a json file (unlike the additional configuration you'd have to do for say, sass, or jsx loading).
In my package.json I have: "scripts":{ ... "build-mydir01": "webpack --env.ext mydir01 --mode production ./code/mydir01/index.js --output ./public/mydir01/main.js", ... }
In my webpack.config.js:
`
const HtmlWebPackPlugin = require("html-webpack-plugin");
Is there any official documentation on how to output JSON files to my /dist folder with Webpack 4?
Simply adding const langFile = require('./locale/en/lang.json'); to my webpack.config.js file does not tell me much or do much for me with Webpack 4. I still encounter the same error.
Reacted by Alex Matei, Derek Surguy, Cody Swartz, Martin Löper, Arvin Sevilla, Kaelan Cooter, Jérôme Lanteri, Egor Komarov and Johannes Fahrenkrug
Reacted by kensley, Churbanov Valery, yatohiyori, Michal Trnka, Chamod Pathirana, Kevin Kim, Sandeep Sirodia, Materix, Ahmed Kamal, Yves M. and 10 more
Reacted by Kevin Kim, Kaelan Cooter and Cosmin Ababei
Reacted by Sandeep Sirodia, Kevin Kim, Reza Z. Ramadan, Michael Sorensen and Cosmin Ababei
Reacted by Arvin Sevilla, Kevin Kim, Raúl Díaz Poblete, Cosmin Ababei and Olfa Melles
Reacted by Kevin Kim, WWQ1993, Cosmin Ababei and Kevin Farrugia
So I do this, the project build and loads, but it seem the JSON import is undefined like it isn't ready the JSON. is anyone else getting this? Does your application still recognize the JSON values after doing tihs.
This is working for me, my bundle now builds successfully and I have a separate JSON file, but now my app stop working. The js in the bundle that imports the JSON file is only getting a string with the file name instead of the javascript object I was receiving before extracting the JSON file from the bundle.
importlanguagesFileUrlfrom'./languages.json';console.log(languagesFileUrl);// This just returns the URL of your JSON file. For example: '/languages.json'. You should then fetch the file via AJAX.fetch(languagesFileUrl)...
Activity
enjikaka commentedon Feb 27, 2018
I assume this is related to webpack 4 now handling JSON loading natively
alexander-akait commentedon Feb 27, 2018
@enjikaka seems bug in
webpack@4, he should ignore.jsonfiles which loaded other loadersalexander-akait commentedon Feb 27, 2018
@webpack-bot move to webpack/webpack
michael-ciniawsky commentedon Feb 27, 2018
@see webpack/webpack#6586. Why is the
file-loadereven needed here ? Either explicitly add thetypeto theruleinwebpack.config.jsor use a differentextnamefor inline usage (e.gx.manifest). The inline loader syntax is discouraged for a while now and it would even work in casex-loadersupportsmodule.type = 'json'.joelcollyer commentedon May 24, 2018
For anyone who's encountering the error
Module parse failed: Unexpected token m in JSON at position 0like I was, and you find this thread, you don't need to specify that json files be loaded in any special way.Simply require your files like this:
Webpack 4.x will do the rest.
No special tests, configuration, or loaders are needed for
.jsonto be required in your JavaScript.rlefever68 commentedon Jun 7, 2018
Please check the encoding of your .json file.
It is probably 'UTF-8 with BOM'.
Resave the file with pure 'UTF-8' and it should be solved.
1 remaining item
BobbyBabes commentedon Jul 6, 2018
@joelcollyer The only problem with using require() to read JSON files, is that the file will be cached. Unless it's deleted from require()'s cache.
joelcollyer commentedon Jul 6, 2018
@BobbyBabes: I fully agree; it'd be best to fetch json with an async request most of the time.
In this case we were loading a mandatory language file, so the
requireisn't so bad. If you try to do that with animporttree shaking gets in the way and the file isn't loaded.My point was mostly that no webpack configuration is required for it to understand the loading of a json file (unlike the additional configuration you'd have to do for say, sass, or jsx loading).
crloshro commentedon Oct 27, 2018
@joelcollyer thank you so much, that tip helped me to fix the bug!
naretini commentedon Feb 14, 2019
In my package.json I have:
"scripts":{ ... "build-mydir01": "webpack --env.ext mydir01 --mode production ./code/mydir01/index.js --output ./public/mydir01/main.js", ... }In my webpack.config.js:
`
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = function (env) {
console.log(
env, env);const ext = env.ext || 'src';
};
`
After executing:
npm run build-mydir01In my ./public/mydir01/ i see index.html and main.js but extension.json is not moved there....
What do I miss?
blachawk commentedon Mar 3, 2019
Is there any official documentation on how to output JSON files to my
/distfolder with Webpack 4?Simply adding
const langFile = require('./locale/en/lang.json');to my webpack.config.js file does not tell me much or do much for me with Webpack 4. I still encounter the same error.garkin commentedon Oct 14, 2019
Correct way to use
file-loaderwith JSON in Webpack 4+:zaitebaki commentedon Oct 16, 2019
best wishes, you awesome :)))
With saving path:
Garwin4j commentedon Oct 27, 2019
So I do this, the project build and loads, but it seem the JSON import is undefined like it isn't ready the JSON. is anyone else getting this? Does your application still recognize the JSON values after doing tihs.
kevinlabx commentedon Jan 6, 2020
@garkin
Thank you this actually works.
stories2 commentedon Mar 5, 2020
With exclude
node_modulesit makes best for me.RezaZR commentedon Apr 24, 2020
Adding esModule works best for me. Thank you for the solution
Richifg commentedon May 2, 2020
This is working for me, my bundle now builds successfully and I have a separate JSON file, but now my app stop working. The js in the bundle that imports the JSON file is only getting a string with the file name instead of the javascript object I was receiving before extracting the JSON file from the bundle.
Any idea what can he causing this?
Garwin4j commentedon May 3, 2020
I had the same problem. For this you have to read the file as a normal text file and transform it at runtime:
The thing to consider thou is to ensure this is done before the file is used in code.
Note that I spit out the file at the root of the app:
Richifg commentedon May 3, 2020
@Garwin4j thanks for the quick reply, yes I ended loading the failed asyncronously as well. I did it with fetch api and it worked well.
In this example the file is also at the root.
Eli-Black-Work commentedon May 7, 2020
So, to sum this up:
If you want Webpack to load the contents of your JSON, so that you can immediately use it in your code, don't use
file-loaderat all. (#259 (comment))Example:
Your code
This method has a downside: The entire contents of the JSON file gets bundled into your app's .js file, which of course makes your app larger.
If you want to load your JSON file via AJAX, use
file-loader. (#259 (comment))Example:
Webpack config file
Your code