S
S
Sashqa2020-01-23 10:59:32
Node.js
Sashqa, 2020-01-23 10:59:32

Calling a function from a file in node js?

There is a file with content

import {} from ....
import {} from ....
import {} from ....

export const func = [
{
    fname: 'тест функ',
    func: (contract) => {
      console.log('1')
      }
    },
},
{
    fname: 'тест функ2',
    func: (auto) => {
      console.log('2')
      }
    },
}]

I read that the fs module is needed to work with files
. I cut it, I output data to the console, I get the whole file. Can you tell me how I can pass an argument and call certain functions from this file?
for example, I pass auto and the function is called from the 2nd object, I pass contract - from the 1st

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kuzmenko, 2020-01-23
@kuzmrom7

It's not entirely clear why you want to use the "fs" module to include modules .
To connect in NodeJS you can use

const myScript = reqiure("./index.js");
const config = myScript.getConfig();

and if you exported the module in that module, then you can safely use the functions that are inside :)
in your example, import is written via
import {} from ....
this syntax is not supported out of the box in NodeJS, you will need to use babel, to use such an import.
read https://metanit.com/web/nodejs/2.1.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question