首页 > 解决方案 > npm 运行构建命令“未安装包”中的错误

问题描述

我创建了一个 react-redux 应用程序,它工作正常。现在我想用react-scripts package在 IIS 上发布它。当我使用npm run build时,我收到一条错误消息:

Creating an optimized production build...
Failed to compile.

.\src\_services\report.service.js
Cannot find module: 'config'. Make sure this package is installed.

You can install this package by running: npm install config.

但是这个配置是 webpack.config.js 中的一个外部,我写它有一个全局应用程序配置选项 这是我的 webpack.config.js

var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {    
    mode: 'development',
    resolve: {
        extensions: ['.js', '.jsx']
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: 'babel-loader'
            }
        ]
    },
    plugins: [new HtmlWebpackPlugin({
        template: './public/index.html',
        favicon: "./src/favicon.ico"
    })],
    devServer: {
        historyApiFallback: true
    },
    externals: {
        // global app config object
        config: JSON.stringify({
            apiUrlAuthentication: 'https://localhost:5001',
            apiUrlOperations: 'https://localhost:5003',
            apiUrlReports: 'https://localhost:5005',
            apiUrl: 'https://localhost:5001'
        })
    }
}

我以这种方式使用配置:

import config from "config";
import { authHeader } from "../_helpers";

export const reportService = {
  getTransactionsReport
};

function getTransactionsReport(JSONfield) {
  console.log("JSONFilterfield", JSONfield);  
  const requestOptions = {
    method: "POST",
    dataType: "json",
    headers: authHeader(),
    body: JSON.stringify(JSONfield),
  };
  console.log("requestBody", requestOptions);
  return fetch(
    `${config.apiUrlReports}/api/myApi`,
    requestOptions
  ).then(handleResponse);
}

我的错误是什么,或者我能为这个构建做些什么?

标签: reactjswebpackbuildreact-reduxreact-scripts

解决方案


推荐阅读