Unable to Import JSON data from local file using ES6 import in a React Redux Webpack App












2














I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.



I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.



Note: This block is inside in IIFE block.



Find the service code below.



import jsonVal from './josnFile.json';

export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}

function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());


After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.



I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?



Note: I am using babel-node to transpile ES6 code










share|improve this question
























  • 1. Are you sure the path to json is correct? I see that you're trying to import ./josnFile.json. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
    – lukaleli
    Aug 23 '17 at 21:06








  • 1




    on a side note, you don't need an IIFE because everything is scoped to the module
    – azium
    Aug 23 '17 at 21:50










  • @jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
    – BadriNarayanan Sridharan
    Aug 24 '17 at 21:31
















2














I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.



I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.



Note: This block is inside in IIFE block.



Find the service code below.



import jsonVal from './josnFile.json';

export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}

function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());


After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.



I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?



Note: I am using babel-node to transpile ES6 code










share|improve this question
























  • 1. Are you sure the path to json is correct? I see that you're trying to import ./josnFile.json. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
    – lukaleli
    Aug 23 '17 at 21:06








  • 1




    on a side note, you don't need an IIFE because everything is scoped to the module
    – azium
    Aug 23 '17 at 21:50










  • @jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
    – BadriNarayanan Sridharan
    Aug 24 '17 at 21:31














2












2








2







I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.



I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.



Note: This block is inside in IIFE block.



Find the service code below.



import jsonVal from './josnFile.json';

export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}

function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());


After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.



I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?



Note: I am using babel-node to transpile ES6 code










share|improve this question















I am trying to learn building a React + Redux application architecture using Web Pack 3 for bundling. I am trying to work with temporary JSON files for data sets and importing them into Service files which want to use the data returned from the imported JSON.



I try to use the imported JSON in Promise.resolve(JSON) and somehow the value seems to be undefined.



Note: This block is inside in IIFE block.



Find the service code below.



import jsonVal from './josnFile.json';

export default (function() {
return {
method1: methodName1,
method2: methodName2,
...
}

function methodName1(){
return new Promise((resolve, reject) => {
resolve(jsonVal);
// ...usual then, catch blocks for returning rejects
});
}
}());


After all this flow, I have set up the reducers and actions to appropriately update the Redux state. But no update happens because the JSON data is always undefined and when I try to pass this down to the presentational React components via Redux connect (mapStateToProps), its undefined too.



I want to know what should I do to include this JSON for mocking APIs.
Whether I have to use a JSON loader in webpack or any other solution?



Note: I am using babel-node to transpile ES6 code







json reactjs webpack redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 23 '17 at 23:20

























asked Aug 23 '17 at 20:52









BadriNarayanan Sridharan

516




516












  • 1. Are you sure the path to json is correct? I see that you're trying to import ./josnFile.json. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
    – lukaleli
    Aug 23 '17 at 21:06








  • 1




    on a side note, you don't need an IIFE because everything is scoped to the module
    – azium
    Aug 23 '17 at 21:50










  • @jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
    – BadriNarayanan Sridharan
    Aug 24 '17 at 21:31


















  • 1. Are you sure the path to json is correct? I see that you're trying to import ./josnFile.json. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
    – lukaleli
    Aug 23 '17 at 21:06








  • 1




    on a side note, you don't need an IIFE because everything is scoped to the module
    – azium
    Aug 23 '17 at 21:50










  • @jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
    – BadriNarayanan Sridharan
    Aug 24 '17 at 21:31
















1. Are you sure the path to json is correct? I see that you're trying to import ./josnFile.json. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
– lukaleli
Aug 23 '17 at 21:06






1. Are you sure the path to json is correct? I see that you're trying to import ./josnFile.json. There's a typo: "josnFile.json" -> "jsonFile.json" ;) 2. Have you tried with json-loader for webpack? npmjs.com/package/json-loader
– lukaleli
Aug 23 '17 at 21:06






1




1




on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50




on a side note, you don't need an IIFE because everything is scoped to the module
– azium
Aug 23 '17 at 21:50












@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31




@jimmyweb - Yes, Path to json is correct, this is just a sample code I used here, and also I have tried using webpack JSON loader. Still same results.
– BadriNarayanan Sridharan
Aug 24 '17 at 21:31

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45849081%2funable-to-import-json-data-from-local-file-using-es6-import-in-a-react-redux-web%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45849081%2funable-to-import-json-data-from-local-file-using-es6-import-in-a-react-redux-web%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin