javascriptnode.jsbrunch

brunch - require function from other file don't work


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?


Solution

  • 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');