Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack TypeScript module.hot does not exist

I want to enable Webpack HMR in a NodeJS project written in TypeScript.

But module.hot is not available:

  • @types/webpack-env defines:

    declare var module: __WebpackModuleApi.Module 
  • Which conflicts with @types/node definition:

    declare var module: NodeModule 

Removing @types/node, solves the issue, but disables process:

process.env.NODE_ENV === 'production' // [ts] Cannot find name 'process' 
like image 238
kube Avatar asked Nov 12 '16 22:11

kube


People also ask

How do I enable hot module replacement in Webpack?

To enabling HMR in your project, you need to let your application know how to handle hot updates. You can do so by implementing the module. hot API exposed by Webpack. Once the hot update is accepted, the HMR runtime and the loaders will take over to handle the update.

How does Webpack hot reload work?

Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways: Retain application state which is lost during a full reload. Save valuable development time by only updating what's changed.

What is Webpack_hmr?

Hot Module Replacement (or HMR) is one of the most useful features offered by webpack. It allows all kinds of modules to be updated at runtime without the need for a full refresh. This page focuses on implementation while the concepts page gives more details on how it works and why it's useful.

Which command helps to enable hot module replacement in the dev server?

Using the webpack-dev-server we can set up hot module replacement with React. This means whenever we modify a component and save the file webpack will replace the module on the page without reloading, without losing component state.


2 Answers

As few guys wrote here it's the best way:

npm i -D @types/webpack-env 

For me it works as expected, resolving issue with not recognized hot property.

In my project I'm using those versions:

"@types/node": "^8.0.19", "@types/webpack-env": "^1.13.0" 

I don't know if question is still up to date but for my problem installing types for webpack help me.

like image 108
WooCaSh Avatar answered Oct 01 '22 05:10

WooCaSh


Conflict resolution

@types/webpack-env was since updated:

  • Conflict on module was solved in this PR

  • process was added in this commit

The code in the original question now only needs @types/webpack-env.

But importing @types/node alongside won't conflict anymore.


Installation

npm install --save-dev @types/webpack-env 

And if you also need NodeJS environment definitions:

npm install --save-dev @types/node 
like image 33
kube Avatar answered Oct 01 '22 04:10

kube