3

So I have this NodeJS project with the following folder tree

+ node-folder
   + react-app-folder
   + (other folders...)
   + config
       + default.json

What I am trying to do is import that default.json file to any file in that react-app-folder.

The node-folder and react-app-folder have their own package.json file.

Is there any way of doing this?

Thank you all very much.

EDIT: For more details about the node-folder . It is a NodeJS app server whose package.json has a script using concurrently to run both the nodejs-app on port 3000 and react-app on port 3001

Ngọc Hy
  • 298
  • 1
  • 6
  • 15
  • are they(folders under node-folder) relative? what about treating the config fold as an individual npm package and install it to where you need. – Narro Feb 16 '20 at 06:44
  • I am not sure what you mean by 'relative' but I've edited my question with more details, could you have a look, please? Thanks. – Ngọc Hy Feb 16 '20 at 06:59

2 Answers2

4

If I understood your question correctly, I believe that you are using create-react-app. The ModuleScopePlugin enforces a restriction that prevents you from importing files outside src folder. Removing this from your webpack configuration will allow you to import files out of react app scope. Please check this similar question.

Manoj Bhat
  • 64
  • 4
  • Thanks for your response. I've read the question that you suggested. But if the React developers tried to restrict the scope of imported files in the react-app, there must be some problems or some critical principals that they want us to follow, right? I mean, wouldn't breaking the restriction crash our app in some ways or some scenarios? – Ngọc Hy Feb 16 '20 at 08:03
  • I don't think doing this would crash or break anything. This rule is imposed to generate an optimised bundle once the app is built. – Manoj Bhat Feb 16 '20 at 09:12
-2

i think require is ok, isn't it?

commonjs

// node-folder/config/default.json
exports.PORT = 8080;
// node-folder/server/app.js
var config = require('../config/config');

console.log(config.PORT);
Narro
  • 430
  • 5
  • 14
  • bruh, I appreciate your help and everything, but I think you've missundertood my question. My goal is to import the ```default.json``` file to any file in the ```react-app-folder``` – Ngọc Hy Feb 16 '20 at 07:33