Aleski
Aleski

Reputation: 1442

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.

Upvotes: 30

Views: 31350

Answers (4)

Kewyn Vieira
Kewyn Vieira

Reputation: 458

My warning only got fixed with:

module.exports =
{
    target: 'node',
    externals: {
        "express": "require('express')"
    }
}

Upvotes: 3

Tofandel
Tofandel

Reputation: 3565

Instead of excluding all of the npm dependencies to be bundled with nodeExternals you can also exclude only express by natively requiring it by replacing

import express from 'express';
// Or
const express = require('express');

To

const express = __non_webpack_require__('express');

That will suppress the warning caused by express

Upvotes: 1

Jadam
Jadam

Reputation: 1775

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' } }]

Upvotes: 15

Jonathan Borges
Jonathan Borges

Reputation: 1069

Use webpack-node-externals.

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

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

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

Upvotes: 77

Related Questions