Parcel: Unable to call functions due to ReferenceError

Created on 8 May 2018  路  8Comments  路  Source: parcel-bundler/parcel

馃悰 bug report


Unable to call functions from inside a <script> tag due to

Uncaught ReferenceError: foobar is not defined

馃帥 Configuration (.babelrc, package.json, cli command)

package.json

{
  "name": "parc",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "parcel-bundler": "^1.8.1"
  }
}
yarn parcel index.html

馃 Expected Behavior

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

馃槸 Current Behavior

js foobar
Uncaught ReferenceError: foobar is not defined

馃敠 Context

This is a simplified example with only index.html and index.js.

馃捇 Code Sample

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())

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.8.1
| Node | 8.11.1
| npm/Yarn | yarn 1.6.0
| Operating System | Windows 7

Question

Most helpful comment

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();

All 8 comments

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

Was this page helpful?
4 / 5 - 1 ratings

Related issues

tarepan picture tarepan  路  46Comments

jpsc picture jpsc  路  81Comments

Znarkus picture Znarkus  路  38Comments

v0idifier picture v0idifier  路  51Comments

devcati picture devcati  路  45Comments