【问题标题】:Webpack and Express - Critical Dependencies WarningWebpack 和 Express - 严重依赖警告
【发布时间】:2017-06-01 06:01:27
【问题描述】:

我有以下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'
  }
};

当我运行 webpack 时,我收到关于依赖项的警告:

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

我从这里开始的快递服务器只不过是一个Hello World 示例,并且功能应该如此,但我担心这个警告。

我的 googlefu 没有透露任何可行的解决方案。我见过这个问题的一个特定实例,但解决方案是通过不显示来绕过警告。

【问题讨论】:

  • 能否将view.js的代码包含进来?
  • 尝试使用 webpack-node-externals 插件npmjs.com/package/webpack-node-externals
  • @leo 这不是我的仓库中的文件,它只是通过快递包含的,我想当我 di import * from express
  • 我真的很困惑。 OP 说该文件是“webpack.config.*ts*”(强调我的),但我看到的所有其他内容都说这应该是一个 .js 文件,实际上它的语法似乎与 JavaScript 文件更一致。这只是一个错字吗?
  • @Everettss 太糟糕了,您没有提供该提示作为答案...哈哈。

标签: express webpack


【解决方案1】:

使用 webpack-node-externals。

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

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

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

【讨论】:

  • 这消除了我的错误,但它没有生成块文件 - Webpack: output server.0.chunk.js, Webpack: output server.1.chunk.js, Webpack: output server.2.chunk.js, Webpack: output server.3.chunk.js
  • 我怎样才能同时拥有 import 和 require?
  • @mohit,也许你忘记了 nodeExternal 的圆括号,就像我一样 :):external: [nodeExternal()]
【解决方案2】:

对于那些由于此处提到的视图库而只需要删除 express 的人,您还可以从 webpack 配置中显式地将 express 定位到外部。

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

【讨论】:

  • 不错的解决方案。在我的情况下,我还需要在我的 webpack.config 中添加 output: {...clientOutput, libraryTarget: 'commonjs'}, 以避免 module.exports = undefined 在我的包中(根据github.com/webpack/webpack/issues/2030
【解决方案3】:

除了排除所有要与 nodeExternals 捆绑的 npm 依赖项之外,您还可以通过替换来排除仅 express 的本地要求

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

const express = __non_webpack_require__('express');

这将抑制由 express 引起的警告

【讨论】:

    【解决方案4】:

    我的警告只得到了解决:

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

    【讨论】:

    • 这需要安装 node_modules。
    猜你喜欢
    • 2017-12-08
    • 2017-07-30
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多