0

I am using node.js to build an app where I have a lot of static text (may change over months) in the code. I want to move the text in a separate file and refer that file data as a variable in the handler file.

E.g.

const result = await client.views.open({
            view: {
                type: 'new',
                text:  [{
                    text: {
                        type: "plain_text",
                        text: "One",
                        emoji: true
                    },
                    value: "One"
                },
                {
                    text: {
                        type: "plain_text",
                        text: "Two",
                        emoji: true
                    },
                    value: "Two"
                }
                ]
                }
            });
            } catch (error) {
                 console.error(error);
            }

The above is original file code. What I want to do is move the below code to a separate file:

                   [{
                        text: {
                            type: "plain_text",
                            text: "One",
                            emoji: true
                        },
                        value: "One"
                    },
                    {
                        text: {
                            type: "plain_text",
                            text: "Two",
                            emoji: true
                        },
                        value: "Two"
                    }
                    ]

And there after use something like require(./newfile.js) to refer to that as a variable in the handler file.

The issue I am facing is this is not a valid JSON, but an object with JSON structure so unsure how to work around this .

1
  • Declare the object as a variable and export that. Commented Jan 11, 2021 at 22:05

1 Answer 1

1

In newfile.js

module.exports = [
    {
        text: {
             type: "plain_text",
             text: "One",
             emoji: true
        },
        value: "One"
     },
     {
         text: {
              type: "plain_text",
              text: "Two",
              emoji: true
         },
         value: "Two"
     }
]

Then import it in your original file:

const innerText = require('./newfile.js');

const result = await client.views.open({
    view: {
        type: 'new',
        text: innerText,
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.