brunch - require function from other file don't work

275 Views Asked by At

I'm working in a brunch project.

I have two javascript files, let's say A.js and B.js

A.js:

 function replacer(key, value) {
    if (typeof value === 'number' && !isFinite(value)) {
        return String(value);
    }
    return value;
};

B.js:

atts = ...
json = JSON.stringify(atts, replacer);

In my html I do:

<script type="text/javascript">
        require('scripts/front/A');
        require('scripts/front/B');
</script>`

When javascript B is executed I got replacer is not defined.

It is possible to call functions from different files?

1

There are 1 best solutions below

0
On BEST ANSWER

In a.js

module.exports = function (key, value) {
if (typeof value === 'number' && !isFinite(value)) {
    return String(value);
}
return value;
};

In b.js: var replacer = require('path/to/a.js');