Reputation: 689
I'm working with existed nodejs webui app. And now I have to add some additional functional. Whole code compiling by webpack on local machine and then deploying to remote server.
My goal is to set variable in code according to value from additional json config file which stored on remote server(in /etc/...)
My attemts to make import or require json file in code led me to the fact that it load file and compiling code with data from my local machine. Also tried to make dynamic require(like require.ensure()) result was the same - it loads local file from my machine. Also I read docs about webpack-context and I think that is what I need but it still in not clear for me. As I undertand it should read file on runtime and not pre-compile it
I'm not good known with nodejs so probably question is not correct - my appologies. Can someone describe what I have to read or learn to understand where is my misstake?
Thanks
Upvotes: 0
Views: 1169
Reputation: 111506
The import
and require
will likely be hijacked by webpack. You may need to use fs.readFile()
plus JSON.parse()
yourself, or you can use something more general like the config
module:
Note that if you do anything with JSON.parse()
then make sure that you put it inside a try
/catch
block or use some module like tryjson
(disclaimer: I'm the author of tryjson
) or otherwise your app will crash for invalid JSON. See those answers for more details:
Upvotes: 1