Webpack:移动webpack.config.json后找不到模块

6l7fqoea  于 2023-03-18  发布在  Webpack
关注(0)|答案(1)|浏览(142)

这是一个很简单的问题,我似乎弄不明白。
我有一个具有此配置的项目

- Folder
    - SubFolder 1
    - react
        - webpack.config.json
        - package.json
        - package-lock.json
        - node_modules
        - src
            - somecode.js
            - somecode2.js

我想将Webpack移动到根文件夹,如下所示:

- Folder
    - SubFolder 1
    - webpack.config.json
    - react
        - package.json
        - package-lock.json
        - node_modules
        - src
            - somecode.js
            - somecode2.js

这可能吗?
搬家前我的网络包如下:

/// <binding />
"use strict";
const path = require("path");
const WebpackNotifierPlugin = require("webpack-notifier");
const BrowserSyncPlugin = require("browser-sync-webpack-plugin");
const AssetsPlugin = require('assets-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const outputPath = path.resolve(__dirname, "../Scripts/ReactScript");

const app = {
    entry: [
        'babel-polyfill',
        "./src/index.js"
    ],
    output: {
        path: outputPath,
        filename: "[name].[chunkhash].js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            }
        ]
    },
    devtool: "inline-source-map",
    plugins: [
        new WebpackNotifierPlugin(),
        new BrowserSyncPlugin(),
        new AssetsPlugin({
            removeFullPathAutoPrefix: true,
            fileName: 'webpack.assets.json',
            path: outputPath
        }),
        new CleanWebpackPlugin({
            cleanOnceBeforeBuildPatterns: [
                path.resolve(outputPath, '*.js'),
                path.resolve(outputPath, '*.js.map')
            ],
            dangerouslyAllowCleanPatternsOutsideProject: true,
        })
    ],
    resolve: {
        extensions: [
            '.js',
            '.jsx'
        ],
    },
};

if (process.env.NODE_ENV === 'development') {
    app.output.filename = '[name].js';
}
module.exports = app;

我原以为只添加context属性就可以运行context: path.resolve(__dirname, 'react'),但是看起来不太好用,因为require属性显然不能正常工作。可以这样做吗?我是否也要移动package.json和package-lock.json(以及snode_modules)?
谢谢!

des4xlb0

des4xlb01#

如果我没有理解错这个问题,那么当您运行webpack脚本时,您需要指定config的路径

--config [your path]

相关问题