为什么我不能用 React 运行 Webpack?



几天来,我一直在尝试用 React 安装 webpack。我使用了几个教程,但我仍然无法安装它。最近,我使用了本教程,但在最后一步中,当我尝试运行webpack时,出现错误:

CLI 移动到一个单独的包中:webpack-cli。
除了 webpack 本身之外,请安装 'webpack-cli' 才能使用 CLI。
-> 使用 npm 时: npm 安装 webpack-cli -D
-> 使用纱线时:纱线添加 webpack-cli -D

然后,我运行npm install webpack-cli -D,但它没有帮助,我不知道如何解决这个问题。当我尝试只安装 webpack 时,我收到同样的错误。谢谢你的建议。

package.json 和 webpack.config.js 内容:

{
  "name": "proj",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/react": "^16.0.40",
    "@types/react-dom": "^16.0.4",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  },
  "devDependencies": {
    "awesome-typescript-loader": "^3.5.0",
    "source-map-loader": "^0.2.3",
    "typescript": "^2.7.2",
    "webpack": "^3.0.0",
    "webpack-cli": "^2.0.10"
  }
}
module.exports = {
    entry: "./src/index.tsx",
    output: {
        filename: "bundle.js",
        path: __dirname + "/dist"
    },
    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",
    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },
    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /.tsx?$/, loader: "awesome-typescript-loader" },
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /.js$/, loader: "source-map-loader" }
        ]
    },
    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

好的,我解决了我的问题。首先,我从package.json中删除了"webpack-cli": "^2.0.10"。然后我安装 npm install webpack@3.0.0 并添加到 package.json
"scripts": { "build": "webpack --config webpack.config.js" },
现在我可以通过 npm run-script build 运行我的 webpack 。多谢。

尝试安装 Webpack^4.1.0, webpack-cli^2.0.10

npm i --save-dev webpack@4.1.0 webpack-cli@2.0.10

最新更新