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
What is the expected behavior?
Loading json files and no errors.
If this is a feature request, what is motivation or use case for changing the behavior?
Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
node v8.9.4, webpack 4.0.1, Ubuntu 16.04
Reacted by Mitch Ryan, Aleksandar, Dennis Brown, Simon Seyock, Eugene Serafimovich , David Nunez, Peter Leibiger, Andy Ray, Jorge, Stanislav Mayorov and 14 more
You don't need any loader for .json anymore (since webpack >= v2.0.0). webpack >= v4.0.0 introduced Module.type and JSON got it's own module.type = 'json' (It doesn't get parsed by the JS Parser anymore). Either remove json from Rule.test or explicitly set the Rule.type to another Module.type e.g type = 'javascript/auto'
Reacted by G.Burak, Kaely, Krishna Ravishankar, Karim Aboelela, John Santhosh, echodis, Dennis Brown, Roman Basinschi, Sam Kozin, Dima Dorogonov and 44 more
Reacted by garkin, Tapani Saarinen, zman, Sam Anderson, UX Engineering, Ибрагим Кандибобер and curt
In fact this is a problem when we do not want to bundle the JSON files for nodejs bundle (target="node").
For example, I have a situation where I need to do the bundle of a nodejs application. Some part of this application need to have access to a large number of JSON files containing large data. Thus requiring those files include them in the bundle but their size is too important and webpack crashes because of missing memory.
So I want to use file-loader on some specific json files to put them on the output directory of the bundle, with an hashed name and then read and parse the file with fs to access the content of the JSON files,without bundling, and keeping the hash name functionality to not expose the JSON directory structure on the output directory that will be provided to other people.
I do not see a way to pass the type="javascript/auto" in an inline loader (require("file-loader!file.json")) that will allow me to keep my build working. If webpack in version 4 doen't try to parse a JSON file when using a loader on it as for version 3, the problem will be solved.
Reacted by Pablo Barría Urenda, Tatsujinichi, xylops, Seph Reed, Simon Seyock, Hasun U Perera, Aleksandr Churbakov, nitinkhuranas, Stefan Dietz, Vašek Purchart and 13 more
I have to echo that this is not solved and the suggested code change does not achieve the same thing.
The goal is to take a manifest.json and copy it to the output dir (perhaps hashed), and get a URL for it with file-loader. Using built in json parsing I don't see how we can achieve this? I don't want the JSON inlined in my output/bundle, and I want to control the file name that it is assigned.
Reacted by Diego Pascual, Dan Van Brunt, Pablo Barría Urenda, Ville Saarinen, Acanthostega, Koike Michitaro, oszbart, Thai Pangsakulyanont, Will Dady, Aleksandr Churbakov and 12 more
I believe the actual problem was not properly understood. The problem is not loading JSON into your bundle, but rather loading JSON as a separate file that will be added to the output directory. Is there another way to achieve this on Webpack 4? If not, this should probably remain open.
EDIT: after fixing some unrelated issue, yes, adding type = 'javascript/auto' did exactly what I was expecting.
Reacted by Ville Saarinen, Jesus M. Gonzalez-Barahona, Blessan Abraham, Acanthostega, UnKnoWn, Tatsujinichi, Koike Michitaro, Sam Kozin, Aleksandr Churbakov, Florian Liesegang and 10 more
@michael-ciniawsky This is a problem for me as well, for example I need to replace text in JSON files before they are packed. But more importantly, I need Webpack to package those references. They are atlas json files, they contain file paths used by PIXIJS. References to images that otherwise will not be packed.
I'm still having trouble with this. Here's my situation:
My project has a bunch of JSON files. Most of the files I want to import directly as objects (i.e. what json-loader and Webpack 4 do), but since a few of them are very large, I want to import them using file-loader and fetch the JSON files dynamically when the app runs.
Previously, this worked fine by doing require('file-loader!!../path/to/large-file.json') for the large files. Since upgrading to Webpack 4, however, doing this produces the same error as described in the OP.
Based on the discussion in this issue, I tried to get around the problem by adding the following to the rules section of my Webpack config file:
Now I no longer get an error at build time, but run into a new problem: the emitted (large) JSON files now contain the following content:
module.exports = { <json content> }
Trying to fetch and parse these as JSON files obviously doesn't work.
The only way I've gotten this situation to work is by renaming my large JSON files to some other extension than JSON, but that's quite an ugly hack. What's the recommended solution to this situation?
Reacted by Ahmed Hassanien, Andy Ray, medniy, Mark Koo Chuan Yu, glenveegee, Faustus, Gareth Simons, redeyes420420, Mark Penner, egirlasm and 1 more
Alright. So do I have to change my default loader to be file-loader, and then override it with require('json-loader!../data/smaller-file.json') when I do want to import the contents of the JSON file?
I tried that approach but am still unable to load some of my JSON files using file-loader and some using json-loader/Webpack's native JSON importing – I get the same error as the OP for files that I tried importing with require('json-loader!../data/smaller-file.json').
The only thing that has worked so far is renaming the extension of the JSON files that I want to load using file-loader.
Sorry, I have a lot of questions about your code, and I'm also not really an expert, but I would suggest using StackOverflow to answer your question since it seems to be more specific to your code than related to the overall issue.
Reacted by Karim Aboelela, Gabriel, John Santhosh, ThisNameWasTaken, Sam Kozin, John Currie, sheila1227, John Lees-Miller, Ilya Dildin, Lean Vilas and 32 more
Reacted by Jacek Piętal, kensley, Ajay Rawat, Mitch Talmadge, Sergio Loaiza, Kallan, Vincent Barrois, Pierre DARRIEUTORT and Bruno Marini
Reacted by Gabriel, ThisNameWasTaken, Rafael Ferreira, Florian Liesegang, Desteel, Mitch Talmadge, Kallan, fqmoon and Pierre DARRIEUTORT
If someone needs different solution, just rename the .json to another extension and it will work.
Reacted by Gabriel and Seph Reed
Reacted by David Nunez, Filippos Zofakis, Andy Ray, jm3, Florian Liesegang, Brent Chow, Kingsley Solomon, Mitch Talmadge, Marek Feikus, jostFT and 13 more
javascript/auto used to be the default module in webpack 3 but webpack 4 completely abstracted the JavaScript specificity from the code base to allow for this change so that users can specify the kind of module they want.
Reacted by najimammeri-veonum, Mitch Talmadge and Justin Gomez
Reacted by najimammeri-veonum, Mitch Talmadge and Justin Gomez
Activity
michael-ciniawsky commentedon Feb 26, 2018
You don't need any loader for
.jsonanymore (sincewebpack >= v2.0.0).webpack >= v4.0.0introducedModule.typeand JSON got it's ownmodule.type = 'json'(It doesn't get parsed by the JS Parser anymore). Either removejsonfromRule.testor explicitly set theRule.typeto anotherModule.typee.gtype = 'javascript/auto'webpack.config.js
module: { rules:[{ + type: 'javascript/auto', test: /\.(json|html)/, exclude: /(node_modules|bower_components)/, use: [{ loader: 'file-loader', options: { name: '[name].[ext]' }, }], }] }or
webpack.config.js (recommended)
module: { rules:[{ - test: /\.(json|html)/, + test: /\.html/, exclude: /(node_modules|bower_components)/, use: [{ loader: 'file-loader', options: { name: '[name].[ext]' }, }], }] }enjikaka commentedon Feb 27, 2018
@michael-ciniawsky webpack-contrib/file-loader#259
Seems like Webpack 4 is not ignoring JSON loaded by other loaders?
Acanthostega commentedon Mar 1, 2018
In fact this is a problem when we do not want to bundle the JSON files for nodejs bundle (target="node").
For example, I have a situation where I need to do the bundle of a nodejs application. Some part of this application need to have access to a large number of JSON files containing large data. Thus requiring those files include them in the bundle but their size is too important and webpack crashes because of missing memory.
So I want to use file-loader on some specific json files to put them on the output directory of the bundle, with an hashed name and then read and parse the file with fs to access the content of the JSON files,without bundling, and keeping the hash name functionality to not expose the JSON directory structure on the output directory that will be provided to other people.
I do not see a way to pass the type="javascript/auto" in an inline loader (require("file-loader!file.json")) that will allow me to keep my build working. If webpack in version 4 doen't try to parse a JSON file when using a loader on it as for version 3, the problem will be solved.
rustam-gaifiev commentedon Mar 5, 2018
have the same issue
import '!file-loader?name=[name].[ext]!./manifest.json';
import 'file-loader?name=[name].[ext]!./.htaccess';
omichelsen commentedon Mar 6, 2018
I have to echo that this is not solved and the suggested code change does not achieve the same thing.
The goal is to take a
manifest.jsonand copy it to the output dir (perhaps hashed), and get a URL for it with file-loader. Using built in json parsing I don't see how we can achieve this? I don't want the JSON inlined in my output/bundle, and I want to control the file name that it is assigned.My current setup is using https://github.com/markdalgleish/web-app-manifest-loader with file-loader to further resolve some image paths in the manifest itself. If you have a better suggestion I am all ears :-)
diegopf commentedon Mar 6, 2018
@omichelsen I had the same issue and https://github.com/arthurbergmz/webpack-pwa-manifest seems to do the trick
differentsmoke commentedon Mar 7, 2018
I believe the actual problem was not properly understood. The problem is not loading JSON into your bundle, but rather loading JSON as a separate file that will be added to the output directory. Is there another way to achieve this on Webpack 4? If not, this should probably remain open.
EDIT: after fixing some unrelated issue, yes, adding
type = 'javascript/auto'did exactly what I was expecting.sergey-shpak commentedon Mar 12, 2018
adding
type = 'javascript/auto'or usingcopy-webpack-pluginsolves the problem (in my case)enjikaka commentedon Mar 12, 2018
I also ended up using
copy-webpack-plugininstead.Tatsujinichi commentedon Mar 27, 2018
@michael-ciniawsky This is a problem for me as well, for example I need to replace text in JSON files before they are packed. But more importantly, I need Webpack to package those references. They are atlas json files, they contain file paths used by PIXIJS. References to images that otherwise will not be packed.
vsaarinen commentedon Mar 28, 2018
I'm still having trouble with this. Here's my situation:
My project has a bunch of JSON files. Most of the files I want to import directly as objects (i.e. what json-loader and Webpack 4 do), but since a few of them are very large, I want to import them using file-loader and fetch the JSON files dynamically when the app runs.
Previously, this worked fine by doing
require('file-loader!!../path/to/large-file.json')for the large files. Since upgrading to Webpack 4, however, doing this produces the same error as described in the OP.Based on the discussion in this issue, I tried to get around the problem by adding the following to the rules section of my Webpack config file:
Now I no longer get an error at build time, but run into a new problem: the emitted (large) JSON files now contain the following content:
Trying to fetch and parse these as JSON files obviously doesn't work.
The only way I've gotten this situation to work is by renaming my large JSON files to some other extension than JSON, but that's quite an ugly hack. What's the recommended solution to this situation?
differentsmoke commentedon Mar 28, 2018
Please note that the examples that have worked here are using "file-loader" and NOT "json-loader".
vsaarinen commentedon Mar 28, 2018
Alright. So do I have to change my default loader to be file-loader, and then override it with
require('json-loader!../data/smaller-file.json')when I do want to import the contents of the JSON file?vsaarinen commentedon Mar 28, 2018
I tried that approach but am still unable to load some of my JSON files using file-loader and some using json-loader/Webpack's native JSON importing – I get the same error as the OP for files that I tried importing with
require('json-loader!../data/smaller-file.json').The only thing that has worked so far is renaming the extension of the JSON files that I want to load using file-loader.
differentsmoke commentedon Mar 28, 2018
Sorry, I have a lot of questions about your code, and I'm also not really an expert, but I would suggest using StackOverflow to answer your question since it seems to be more specific to your code than related to the overall issue.
akoliyot commentedon Apr 7, 2018
I was able to copy my .json config file to the build folder using this:
pristas-peter commentedon Apr 26, 2018
If someone needs different solution, just rename the .json to another extension and it will work.
johnsanthosh commentedon May 8, 2018
@differentsmoke @enjikaka @sergey-shpak @michael-ciniawsky @pristas-peter
Unexpected behavior while using file-loader with webpack to load large json files ( > 140 MB )
Stack Overflow Link
Added fix for `json` in `embedExtensions` & `copyExtensions`
the-simian commentedon May 27, 2018
Has anyone figured out a way to willfully bypass the native json loader so that we may define a partifular pipeline?
lecstor commentedon Jun 20, 2018
@the-simian adding
type: 'javascript/auto'to my json loader rule (as mentioned by @akoliyot 👍) fixed this for me.the closest I found to an explanation was https://scotch.io/tutorials/whats-new-in-webpack-4
Remove json-loader
Remove json-loader
Remove json-loader
blachawk commentedon Mar 3, 2019
Thank you @akoliyot for that solution with file-loader. I have it working on my end.
marigerr commentedon May 4, 2019
There is info on webpack docs under Module/Ruletype
https://webpack.js.org/configuration/module/#ruletype
delete json-loader [webpack/webpack#6586 (comment)]
elgs commentedon May 3, 2020
Just to be clear, you still need the file-loader to load JSON, is it correct?
panlina commentedon Jun 16, 2021
So there's no way to workaround it for inline loader? Since there's no way to set
type: "javascript/auto"on it.