简体   繁体   English

Webpack捆绑包未导出,无法导入

[英]Webpack bundle is not exported and can't be imported

I created my react Project A using Create-React-app . 我创造了我使用的反应项目A Create-React-app Then I bundle it them with Webpack and saved in my Git account. 然后,将它们与Webpack捆绑在一起,并保存在我的Git帐户中。 Now I create another project(Called it Project B )in different directory. 现在,我在不同目录中创建另一个项目(称为项目B )。 Download Project A directly from git. 直接从git的下载项目A。 And trying to use it like so: 并尝试像这样使用它:

import React from 'react';
import ReactDOM from 'react-dom';
import { Main } from 'project-A/dist/main'

ReactDOM.render(<Main />, document.getElementById('root'));

I am getting an error like following: 我收到如下错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

The webpack from Project A looks like this: 项目A的Webpack如下所示:

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const nodeExternals = require("webpack-node-externals");

module.exports = [
    {
        /*Client Side*/
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    use: {
                        loader: "babel-loader"
                    }
                },
                {
                    test: /\.html$/,
                    use: {
                        loader: "html-loader",
                        options: { minimize: true }
                    }
                },
                {
                    test: /\.css$/,
                    use: [MiniCssExtractPlugin.loader,"css-loader"]
                }
            ]
        },
        plugins: [
            new HtmlWebPackPlugin({
                template: "./public/index.html",
                filename:"./index.html"
            }),
            new MiniCssExtractPlugin({
                filename: "[name].css",
                chunkFilename:"[id].css"
            })
        ]
    }
]

I have research through the github and tried to change the name import, it still does not work. 我通过github进行了研究,并试图更改名称import,但仍然无法正常工作。

Project A's component looks like this: 项目A的组件如下所示:

App.js: App.js:

  render() {
    return (
      <div>
        {this.renderCodeAuthCard()}
        {this.renderConfirmCard()}
        {this.renderVerifyCard()}
      </div>
    );
  }

export default App;

index.js: index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

Apparently webpack is not exporting the bundle file that is created in Project A. Since the import yields " undefine ". 显然,webpack不会导出在Project A中创建的捆绑文件。由于导入会产生“ undefine ”。

I am trying to find a way to export my webpack bundle file and use it in another project. 我试图找到一种方法来导出我的webpack捆绑文件并在另一个项目中使用它。

Any help will be appreciated 任何帮助将不胜感激

Its because you are not exporting any thing from index.js of Project A . 这是因为您没有从Project A的 index.js导出任何内容。 The libraries installed by npm export functions from index.js . npm安装的库从index.js导出功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM