Cannot find module error on importing html file in webpack
Asked Answered
F

1

17

Trying to import html in TypeScript with html-loader plugin with:

import buttonHtml from './button.html';

Gives TypeScript error:

TS2307: Cannot find module './button.html'

Webpack config:

const path = require('path');

module.exports = {
  entry: {
      'background.js':path.resolve(__dirname, './background.ts'),
      'content.js': path.resolve(__dirname,'./content.ts')
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /\.html$/,
        exclude: /node_modules/,
        use: {loader: 'html-loader'}
    }
    ]
  },
  resolve: {
    extensions: [ ".tsx", ".ts", ".js" ]
  },
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist')
  }
};
Fetlock answered 14/9, 2017 at 17:19 Comment(0)
I
21

If you want to load html like that, you need a typings.d.ts file with this entry:

declare module '*.html' {
  const value: any;
  export default value;
}
Indult answered 14/9, 2017 at 17:21 Comment(1)
It removes the error superficially. When I compile with webpack the error is still there.Fetlock

© 2022 - 2024 — McMap. All rights reserved.