Y
Y
Yaroslav2017-06-27 21:19:51
Node.js
Yaroslav, 2017-06-27 21:19:51

How to include a function in nodeJS that is exported in a module as export default ()=>{}?

Folks, this is a problem.
There is a modul.js module written in es6 (inside imports and all things), which throws out the function
export default () => {}
This module is processed by webpac'om (babel-loader)
I get module_after_babel.js
Next, in nodeJS, I try to acquire this module to get its function
const fn = require('./module_after_babel.js');
Error that fn is not a function.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2017-06-28
@YarkoDzech

I 'll add to RidgeA
's answer in the webpack config add:

{
    output: {
        filename: 'out_file.js',
        path: 'out/path',
        library: 'тут что угодно',
        libraryTarget: 'commonjs2'
    },
    target: 'node' //чтоб нативные модули ноды не полифилил
}

R
RidgeA, 2017-06-27
@RidgeA

It seems not difficult to see what is exported. Or is console.log no longer working?
And it's not difficult to see what Babel has done with the file.
If I'm not mistaken, it should

const fn = require('./module_after_babel.js').default;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question