0

Hi I'm having a problem with browserify, I did browserify main.js > bundle.js everything work until it hit function. Now I'm getting:

Uncaught TypeError: search is not a function
at HTMLButtonElement.onclick (index.html:30)

Here is what is on my main.js:

function search() {
[..] trigger stuff
}

And on my html file I have:

<input type="text" id="search" placeholder="Search">
<button class="ui button" onclick="search()">

Else is just design and title.

1 Answer 1

5

If you want to export a function to be called from outside the bundle (e.g. in an onclick handler), the simplest thing to do would be something like this:

function search() {
  ...
}
window.search = search;

An alternative to the simple, window-based solution is the --standalone option. In your main.js, you would do something like this:

function search() {
  ...
}
exports.search = search;

And you would build the bundle with a command like this:

browserify main.js --standalone MyApp > bundle.js

And your onclick handler would be:

<input type="text" id="search" placeholder="Search">
<button class="ui button" onclick="MyApp.search()">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.