Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module error on importing html file in webpack

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')
  }
};
like image 744
Shyamal Parikh Avatar asked Sep 14 '17 17:09

Shyamal Parikh


People also ask

Can not find Webpack JS?

To solve the "Cannot find module 'webpack'" error, make sure to install webpack globally by running the npm i -g webpack command and create a symbolic link from the globally-installed package to node_modules by running the npm link webpack command. Copied! Once you run the two commands, the error should be resolved.


1 Answers

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;
}
like image 73
Stefan Kendall Avatar answered Sep 20 '22 14:09

Stefan Kendall