【问题标题】:Webpack does not find node_modules when --config flag is used使用 --config 标志时,Webpack 找不到 node_modules
【发布时间】:2018-03-26 08:56:01
【问题描述】:

我想使用我当前工作目录之外的webpack.config.js。 此外,这个webpack.config.js 不在我当前工作目录的父目录中,它被分隔在另一个位置。

webpack.config.js 我有要求,比如:

const CircularDependencyPlugin = require('circular-dependency-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
... More requires

在我的 cmd 中,我定位到我的项目目录,当我使用 --config 标志运行 Webpack 时

webpack --config ../path/to/file/webpack.config.js

Webpack 找到并使用了配置文件,但它给出了一个错误,即找不到循环依赖插件。

似乎 Webpack 正在搜索 node_modules 目录,而不是当前工作目录中的 webpack.config.js

至少我认为是这样,我可能错了。

有什么方法可以让 Webpack 在另一个位置读取配置文件,但仍然在当前工作目录中使用 node_modules

【问题讨论】:

    标签: node.js webpack webpack.config.js


    【解决方案1】:

    请在此处查看如何在解析模块时指向不同的目录。 https://webpack.js.org/configuration/resolve/#resolve-modules。简而言之

    const baseDir = process.cwd();
    

    并在您的 webpack.config.js 中添加一个条目

    modules :[path.resolve(path.relative(baseDir,__dirname),'..path to circular dependency']
    

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 2017-12-01
      • 2020-06-30
      • 2021-03-30
      • 1970-01-01
      • 2020-03-24
      • 2021-04-23
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多