Webpack TypeScript module.hot 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40568176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:00:20  来源:igfitidea点击:

Webpack TypeScript module.hot does not exist

node.jstypescriptwebpackwebpack-hmr

提问by kube

I want to enable Webpack HMRin a NodeJSproject written in TypeScript.

我想启用的WebPack HMR的NodeJS项目写的打字稿

But module.hotis not available:

module.hot不可用:

  • @types/webpack-envdefines:

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

    declare var module: NodeModule
    
  • @types/webpack-env定义:

    declare var module: __WebpackModuleApi.Module
    
  • 这与@types/node定义冲突:

    declare var module: NodeModule
    

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

删除@types/node,解决了问题,但禁用process

process.env.NODE_ENV === 'production' // [ts] Cannot find name 'process'

采纳答案by kube

Conflict resolution

解决冲突

@types/webpack-envwas since updated:

@types/webpack-env已更新:

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

原始问题中的代码现在只需要@types/webpack-env

But importing @types/nodealongside won't conflict anymore.

但是同时导入@types/node不会再发生冲突。



Installation

安装

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

And if you also need NodeJS environment definitions:

如果您还需要 NodeJS 环境定义:

npm install --save-dev @types/node

回答by WooCaSh

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 hotproperty.

对我来说,它按预期工作,解决了无法识别hot财产的问题。

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.

我不知道问题是否仍然是最新的,但是对于我的问题,为 webpack 安装类型可以帮助我。

回答by user1595858

Could be as simple as adding following line at the top of the file.

可能就像在文件顶部添加以下行一样简单。

///<reference types="webpack-env" />

回答by Daniel Rosenwasser

You can augment the global scope and use interface merging to reopen the NodeModuleinterface and add the missing hotproperty.

您可以扩大全局范围并使用界面合并重新打开NodeModule界面并添加缺少的hot属性。

import webpack = require("webpack");

declare global {
    interface NodeModule {
        hot: {
            accept(dependencies: string[], callback: (updatedDependencies: string[]) => void): void;
            accept(dependency: string, callback: () => void): void;
            accept(errHandler?: (err: any) => void): void;
            decline(dependencies: string[]): void;
            decline(dependency: string): void;
            decline(): void;

            dispose(callback: (data: any) => void): void;
            addDisposeHandler(callback: (data: any) => void): void;

            removeDisposeHandler(callback: (data: any) => void): void;
            // ...
        }
    }
}

But really, this augmentation should potentially be done in the Webpack declaration file itself.

但实际上,这种扩充应该在 Webpack 声明文件本身中完成。

回答by Raúl Martín Ramos

Change .hot by ['hot']

将 .hot 更改为 ['hot']

if (module.hot) {
    module.hot.accept();
    module.hot.dispose(() => {

Use

利用

if (module['hot']) {
    module['hot'].accept();
    module['hot'].dispose(() => {

回答by Gal Bracha

This fixes it

这修复了它

yarn add @types/webpack-env --dev

yarn add @types/webpack-env --dev

VScode would work immediately

VScode 会立即工作

Intellij would need a restart

Intellij 需要重新启动