Webpack 2 compatibility : ERROR in ReferenceError: self is not defined
See original GitHub issueI can’t having it working with Webpack 2, because it runs on server and returns :
ERROR in ReferenceError: self is not defined
Does one have been able solving it? Would it be possible to run it on client only ?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:9
- Comments:17
Top Results From Across the Web
Typescript + Webpack library generates "ReferenceError: self ...
This issue is related to the option output.globalObject of which is described ... I believe webpack has set it as self for a...
Read more >node-self - npm
Start using node-self in your project by running `npm i ... But, in Node.js console.log(self); // ReferenceError: self is not defined.
Read more >Module Methods - webpack
This section covers all methods available in code compiled with webpack. When using webpack to bundle your application, you can pick from a...
Read more >self is not defined nextjs | The AI Search Engine You Control
This happened with me because I was using Next JS which has server side rendering. When you are using server side rendering there...
Read more >ReferenceError: exports is not defined in TypeScript | bobbyhadz
If you get the error in a Node.js application, try removing the type attribute from your package.json file if it's set to module...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@tharnach So I figured it out. The problem is actually caused by Webpack target: https://webpack.js.org/configuration/target/#target
Basically, packages that are supposed to be able to run in node AND web environments (like isomorphic-fetch), have to deal with these environments separately, and many times have separate builds for both. The thing is that when you’re building with Webpack for a web application, you probably have the target set to “web”, so the package will act like there’s a browser there, when there isn’t. So our application is telling all of our dependencies that we’re running in a browser, but static-site-generator-webpack-plugin is actually running in node!
There are two ways to solve this for real:
1. Change the Target
Webpack Config adjustments for static site creation:
1.5 For Universal/Isomorphic Sites, use Two Builds
For universal/isomorphic sites and applications, you should probably use two builds, one for the static site, and one for the client code. Use a node target for building the html files with static-site-generator-plugin, and then use a web target WITHOUT the static-site-generator-plugin for your client javascript. This kind of makes sense anyway, since in general, bundling a normal universal app requires building the node stuff and the web stuff separately…
Webpack Config adjustments for client code:
2. Run static-site-generator-webpack-plugin as if target is node
If we want to update this plugin to prevent this from happening, it would probably be a sane default for static-site-generator-webpack-plugin to assume a node target while building html. This seems like something that a bored/motivated person could look into (I don’t know a ton about building webpack plugins, and I’m too lazy to learn this right now) 😆
This way, the plugins that are meant to be isomorphic/universal, and tell that they’re in a node environment, and adjust accordingly.
Hacky Way
There is, of course, always a hacky way as well… 😛
Maybe it’d be nice now to have an up-to-date working example available under the plugin repository?