Webpack警告:./node_modules/express/lib/view.js关键依赖项:一个依赖项的请求是一个表达式。

5
我正在开发我的第一个全栈应用程序(一个简单的笔记应用程序),在打包Webpack时出现了以下警告:
``` WARNING in ./node_modules/express/lib/view.js 81:13-25 Critical dependency: the request of a dependency is an expression @ ./node_modules/express/lib/view.js @ ./node_modules/express/lib/application.js @ ./node_modules/express/lib/express.js @ ./node_modules/express/index.js @ ./routes/server.js ```
这里是我的 `webpack.config.js` 文件:
const webpack = require('webpack');

const path = require('path');

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

const config = {

  entry: {

      app: './routes/server.js'

  },

  output: {

    path: __dirname + "/public",

    filename: 'build/bundle.js'

  },

  module : {

    rules : [

      {

        test : /\.jsx?/,

        loader : 'babel-loader',

        exclude: /node_modules/,

        query: {

            "presets" : ["es2015", "react"]

          }

      }

    ]

  },

  target: 'node',

  externals: [nodeExternals({
    whitelist: ['express', 'mongodb', 'body-parser', 'react', 'react-dom', 'random-color']
  })]

};

module.exports = config;

这是我的package.json文件:
{
  "name": "quick-notes-app",
  "version": "1.0.0",
  "description": "my first full-stack javascript app",
  "main": "./routes/server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --exec babel-node ./routes/server.js --ignore public/",
    "dev": "webpack -wd",
    "lint": "eslint ./"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/CandiW/quick-notes-app.git"
  },
  "author": "CandiW",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/CandiW/quick-notes-app/issues"
  },
  "homepage": "https://github.com/CandiW/quick-notes-app#readme",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.3",
    "mongodb": "^2.2.35",
    "randomcolor": "^0.5.3",
    "react": "^16.3.2",
    "react-dom": "^16.3.2"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "^8.2.3",
    "babel-loader": "^7.1.4",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "eslint": "^4.19.1",
    "eslint-plugin-react": "^7.7.0",
    "nodemon": "^1.17.3",
    "webpack": "^3.11.0",
    "webpack-node-externals": "^1.7.2"
  }
}

我安装了webpack-node-externals,因为webpack经常会出现错误和警告。我发现webpack没有忽略node_modules,尽管我在exclude中已经包含了它。
我不确定自己是否做错了什么,或者是否有什么做法可以解决这个问题?如果有人有任何想法,请帮忙提供建议!你也可以在这里找到我项目的github存储库:Quick-Notes-App

这个问题已经解决了......我回退到之前的webpack.config.js文件版本,并发现我错误地将entry更改为app: "./routes/server.js",而实际上应该是app: "./routes/index.js" - Candis W
1个回答

8

对我来说有效。我在webpack.server.config.js中更改了代码,你的可能是webpack.config.js或webpack.server.js等

.... webpack配置...

..from...

entry:{...},
output: {
  path: path.join(__dirname, '..', 'build'),
  publicPath: '/',
  libraryTarget: "commonjs2"
},
target: 'node',

to

entry:{...},
output: {
  path: path.join(__dirname, '..', 'build'),
  publicPath: '/',
  libraryTarget: "commonjs2"
},
target: 'node',

externals: {
  express: 'express',
},

只需添加以下代码(对于其他模块也是如此)。
externals: {
  express: 'express',
},

这个做法虽然消除了警告,但在部署时给我带来了问题。当节点被部署后,找不到express依赖项,因此这个解决方案解决了我的问题。意思是在那里添加"require('express')"而不仅仅是'express'。 - Ahmed HABBACHI
这也解决了我的其他一些问题,特别是这个错误:“webpack <5默认情况下会包含node.js核心模块的polyfill。” - DutchPrince

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接