Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require.extensions is not supported by webpack. Use a loader instead

I use express + webpack3 + ejs + typescript

when I build, the stdout output give me some warning:

WARNING in ./node_modules/ejs/lib/ejs.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./src/environment.ts 4:10-24
 @ ./src/server.ts

WARNING in ./node_modules/ejs/lib/ejs.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./src/environment.ts 4:10-24
 @ ./src/server.ts

WARNING in ./node_modules/express/lib/view.js
80:29-41 Critical dependency: the request of a dependency is an expression

here is part of webpack.config.ts:

import * as webpack from 'webpack';
import * as path from 'path';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';

const config: webpack.Configuration = {
  target: 'node',
  devtool: 'source-map',
  entry: {
    server: path.resolve(__dirname, './src/server.ts')
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'server.js'
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        loader: 'ts-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js']
  },
  plugins: [
    new CopyWebpackPlugin([
      { from: './src/views', to: 'views' },
      { from: './src/public', to: 'public' }
    ])
  ]
};

export default config;

and part of tsconfig.json:

"include": [
    "src/server.ts",
    "src/typings"
  ],
  "exclude": [
    "node_modules",
    "src/public",
    "src/views"
  ]
like image 506
slideshowp2 Avatar asked Jul 23 '17 06:07

slideshowp2


3 Answers

The best solution for this is using webpack-node-externals to not worry about node_modules at all when working with webpack for backend.

When bundling with Webpack for the backend - you usually don't want to bundle its node_modules dependencies. This library creates an externals function that ignores node_modules when bundling in Webpack.

like image 197
Justice Mba Avatar answered Nov 15 '22 21:11

Justice Mba


I had the same problem and I solved it by adding the following code to my webpack.config.js file:

resolve: {
   alias: {
       'express-handlebars': 'handlebars/dist/handlebars.js'
   }
}
like image 24
Mario Minondo Avatar answered Nov 15 '22 20:11

Mario Minondo


This set up uses ejs and the ejs-specific solution is to set this in your webpack config:

resolve: {
   alias: {
     'ejs': 'ejs.min.js'
   }
}
like image 34
Yangshun Tay Avatar answered Nov 15 '22 20:11

Yangshun Tay