简体   繁体   中英

Exporting object in Webpack 5

I'm building a JS library with Webpack and trying to export an object.

import jwt_decode from "jwt-decode";

console.log(location.hash.replace('#', ''));

export var upstream = {
  user: {
    getUserDetails: () => {
      if (location.hash) {
        return jwt_decode(location.hash.replace('#', ''));
      } else {
        return null;
      }
    }
  }
}

In my client-side code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UpStream</title>
</head>

<body>
    <script src="http://localhost:8080/app.js"> <!--server is up, connects fine-->
    </script>
    <script>
        console.log(upstream);
    </script>
</body>

</html>

The console.log(); statement works as intended, but I cannot access the upstream object. Any pointers?

To be able to access upstream via window or just upstream you would need to ensure you specify the export as a Library with libraryTarget of 'window' .

Hopefully that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM