question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose variable doesn't work in watch mode

See original GitHub issue

Bug report

What is the current behavior? When I expose a variable using var libraryTarget

the exposed variable is empty object in watch mode dev server.

It works fine when I open the html file directly from filesystem.

If the current behavior is a bug, please provide the steps to reproduce.

Here’s minimal reproducable project https://github.com/ingun37/webpack-bug-reproduce

Write entry file like this

var aaa = "aafufufa"
export {aaa}

Configure webpack like this

const path = require("path");
module.exports = {
    output: {
        library: 'yourLibName',
        libraryTarget: 'var',
    },
    devServer: {
        hot: true,
        contentBase: path.resolve(__dirname, "dist"),
        publicPath: "/",
    },
}

Write HTML like this

<script src="./main.js"></script>
<script>console.log(yourLibName)</script>

Run webpack dev server in watch mode

npx webpack serve --open

You can see the console outputs

{}

While it outputs correctly when you just directly open the html file.

[Log] Object (index.html, line 9)
__esModule: true
aaa: 
Symbol(Symbol.toStringTag): "Module"
Object Prototype

What is the expected behavior?

Expose the variable correctly.

Other relevant information: webpack version: 5.15.0 Node.js version: 12 Operating System: MacOS Additional tools:

Side note

I remember I had the same problem when I was using Webpack 4. Also I found an article addressing the same problem https://stackoverflow.com/a/51924958/3769433

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
alexander-akaitcommented, Jan 19, 2021

Yes, it is fixed in webpack-dev-server@4-beta.0

0reactions
alexander-akaitcommented, Mar 3, 2021

webpack-dev-server@beta-0

Read more comments on GitHub >

github_iconTop Results From Across the Web

NetBeans-Xdebug works, but won't expose some PHP variables
Right click on the variable pane. Select "Filters ". You will find the secret.
Read more >
Environment Variables - webpack
The webpack command line environment option --env allows you to pass in as many environment variables as you like. Environment variables will be...
Read more >
Using dotenv .env config file correctly #865 - GitHub
It is my understanding that process.env won't be available during runtime. If I understand that correctly, why is the code that exposes these ......
Read more >
Modes and Environment Variables - Vue CLI
If they don't contain a NODE_ENV variable, it will be set accordingly. For example, NODE_ENV will be set to "production" in production mode, ......
Read more >
Next.js Tutorial - 64 - Environment Variables - YouTube
Courses - https://learn.codevolution.dev/⚡️ Checkout Taskade! https://www.taskade.com/ Support UPI - https://support.codevolution.dev/  ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found