Unable to call functions from inside a <script> tag due to
Uncaught ReferenceError: foobar is not defined
package.json
{
  "name": "parc",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "parcel-bundler": "^1.8.1"
  }
}
yarn parcel index.html
I should be able to call functions from inside a <script> tag.
In other words, the code sample below should print
js foobar
html foobar
js foobar
Uncaught ReferenceError: foobar is not defined
This is a simplified example with only index.html and index.js.
Full test cast at https://github.com/piedar/parc
index.html
<!DOCTYPE html>
<html>
<body>
<script src="./index.js"></script>
<script>
  console.log("html " + foobar())
</script>
</body>
</html>
index.js
export function foobar() {
  return "foobar"
}
console.log("js " + foobar())
| Software         | Version(s) |
| ---------------- | ---------- |
| Parcel              | 1.8.1
| Node              | 8.11.1
| npm/Yarn         | yarn 1.6.0
| Operating System | Windows 7
This should work
yarn parcel index.html --global window
Hmm, that didn't help somehow. Should the following work?
<script>
  console.log("foobar is " + window.foobar)
  console.log("html " + foobar())
</script>
yarn parcel index.html --global window
For now it prints
foobar is undefined
Uncaught ReferenceError: foobar is not defined
Yeah i don't know anymore, haven't tested any of the new umd features
No worries, and thanks for your time. If anyone wants to replicate, I published the test case at https://github.com/piedar/parc.
And if I'm using the "new umd features", what would I change to try it the old way instead?
As far as I understand, the --global option works as long as you don't name it window. 馃槄
I currently run my project like this:
parcel index.html --global index
And call exported functions inside my html like this:
index.someFunction();
Indeed it does! Thanks!
Does this also apply for bundling the application for production via:
parcel build index.html --global index
 ?
Does this also apply for bundling the application for production via:
parcel build index.html --global index?
Yes it does. I just tested it on version 1.12.4
Most helpful comment
As far as I understand, the
--globaloption works as long as you don't name itwindow. 馃槄I currently run my project like this:
And call exported functions inside my
htmllike this: