Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack and Express - Critical Dependencies Warning

I have the following webpack.config.ts:

var webpack = require( 'webpack' );
var path = require( 'path' );

module.exports = {

  entry: [
    './api/bin/www.ts'
  ],
  output: {
    path: path.resolve( __dirname, './dist/api' ),
    filename: 'index.js'
  },
  module: {
    loaders: [
      { test: /\.ts$/, loader: 'awesome-typescript-loader' },
      { test: /\.json$/, loader: 'json-loader' }
    ]
  },
  resolve: {
    extensions: [ '', '.js', '.ts' ]
  },
  target: 'node',
  node: {
    console: true,
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }
};

When I run webpack I get a warning about a dependency:

WARNING in ./~/express/lib/view.js
Critical dependencies:
78:29-56 the request of a dependency is an expression
@ ./~/express/lib/view.js 78:29-56

The express server I start with this is no more than a Hello World example and functions as should but I am concerned about this warning.

My googlefu hasn't revealed any passable solutions. I have seen one particular instance of this problem but the solutions were to bypass the warning by not showing it.

like image 417
Aleski Avatar asked Jan 17 '17 08:01

Aleski


3 Answers

Use webpack-node-externals.

const nodeExternals = require('webpack-node-externals');

{
  target: 'node',
  externals: [nodeExternals()],
}

https://www.npmjs.com/package/webpack-node-externals

like image 166
Jonathan Borges Avatar answered Oct 23 '22 21:10

Jonathan Borges


For those that only need to remove the express due to the view lib as mentioned here you can also explicitly target express in externals from your webpack config.

externals: [{ 'express': { commonjs: 'express' } }]

like image 32
Jadam Avatar answered Oct 23 '22 20:10

Jadam


My warning only got fixed with:

module.exports =
{
    target: 'node',
    externals: {
        "express": "require('express')"
    }
}
like image 2
Kewyn Vieira Avatar answered Oct 23 '22 20:10

Kewyn Vieira