Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Init and use a js module in Laravel Blade

I am trying to do something simple in laravel. I have a simple js module but I am unable to init it with laravel (laravel-mix to compile). To test I want to do something as simple as:

<button onclick="console.log(Test.sum);">Print</button>
Uncaught TypeError: Cannot read properties of undefined (reading 'sum')

test.js

var Test = function() {
    var a = 0;
    var b = 1;

    var _sum = function() {
        return a + b;
    }

    return {
        sum: function() {
            _sum();
        }
    };
}();

app.js

const Test = require('./test');

My question, what is the propper way to init this module to use in my html web?

thanks a lot!

Solution:

test.js

var test = function() {
    var a = 0;
    var b = 1;

    var sum = function() {
        return a + b;
    }

    return {
        sum: function() {
            return sum();
        }
    };
}();

export { test }

app.js

import { test } from './test';
window.test = test ;
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!