最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - JS, Browserify: function not defined - Stack Overflow

matteradmin10PV0评论

I have files as represented:

-js/
    - calc.js
    - tool.js
-index.html

calc.js is a node module of following structure:

module.exports = {
   calculate: function() {...},
   getPrecision: function() {...}
}

and tool.js use require and adds some functions, like that:

const fpcalc = require('./fpcalc');

function changeState() {
//some code using fpcalc
}

I used Browserify to generate bundle.js and added that as script src. One of my buttons on HTML page is using onclick=changeState(). After clicking I'm getting

ReferenceError: changeState is not defined
at HTMLAnchorElement.onclick

Why is that? Is there any other way to make it work?

I have files as represented:

-js/
    - calc.js
    - tool.js
-index.html

calc.js is a node module of following structure:

module.exports = {
   calculate: function() {...},
   getPrecision: function() {...}
}

and tool.js use require and adds some functions, like that:

const fpcalc = require('./fpcalc');

function changeState() {
//some code using fpcalc
}

I used Browserify to generate bundle.js and added that as script src. One of my buttons on HTML page is using onclick=changeState(). After clicking I'm getting

ReferenceError: changeState is not defined
at HTMLAnchorElement.onclick

Why is that? Is there any other way to make it work?

Share Improve this question edited Aug 23, 2017 at 8:01 annterina asked Aug 23, 2017 at 7:55 annterinaannterina 1731 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

The function "changeState" is not exported in your tool.js. That means it is only visible inside your bundle.js, but not outside.

Have a look at this: https://makerlog/posts/creating-js-library-builds-with-browserify-and-other-npm-modules

It shows you how to expose your code to the global namespace in javascript.

Here's a very simple way to make it work like you want.

const fpcalc = require('./fpcalc');

window.changeState = () => {
    //some code using fpcalc
}

I have same error, here is my working example.

mac, browserify https://github./perliedman/reproject

  1. Must use sudo install globally

    sudo npm install -g brwoserify

    https://github./perliedman/reproject

    sudo npm install reproject // install locally is fine

    Must manually create 'dist' folder for later output file use

  2. Must use --s expose global variable function 'reproject' and or 'toWgs84' you will use later in browser js.

    Without --s , will get 'reproject' undefined error . https://makerlog/posts/creating-js-library-builds-with-browserify-and-other-npm-modules browserify --help will list all options.
    -o means output file directory

    browserify node_modules/reproject/index.js --s reproject -o node_modules/reproject/dist/reproject.js

HTML script tag include your above dist/reproject.js

Now, you will not get 'reproejct' undefined error

return reproject(_geometry_, ___from_projection, proj4.WGS84, crss)
Post a comment

comment list (0)

  1. No comments so far