admin管理员组文章数量:1319486
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 badges3 Answers
Reset to default 7The 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
Must use
sudo
install globallysudo 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
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 directorybrowserify 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)
本文标签: javascriptJSBrowserify function not definedStack Overflow
版权声明:本文标题:javascript - JS, Browserify: function not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740047970a2222007.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论