ReferenceError:需求未定义@react

时间:2020-02-20 10:48:20

标签: javascript node.js reactjs npm webpack

我制作了自己的Logon组件<script> export default { name: "OutPatient", data(){ return{ out_patients: {} } }, methods: { index(){ axios.get('/data/out_patient').then(({data}) => (this.out_patients = data.data)); }, update(){} }, created(){ this.index(); } } </script> <tr v-for="out_patient in out_patients" v-bind:key="out_patient.id"> <td>{{out_patient.id}}</td> <td>{{out_patient.first_name}}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td> <a href="#"><i class="fa fa-edit blue"></i></a> | <a href="#"><i class="fa fa-trash red"></i></a> </td> </tr> 并在npm中发布,现在在其他CRA应用程序中导入和使用它时,在命令行中运行react时出现此错误。 Browser Error

并在控制台中:

Console Error

我的webpack.config.js文件:

npm package

我的package.json文件:

npm start

我的.babelrc文件:

var webpack = require('webpack');
var path = require('path');
var nodeExternals = require('webpack-node-externals');

module.exports = {
    target: 'web',
    entry: './src/index.js',
    output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                include: path.resolve(__dirname, 'src'),
                exclude: /(node_modules|bower_components|build)/,
                use: {
                loader: 'babel-loader',
                options: {
                    presets: ['env']
                }
                }
            },
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.(png|jpg|woff|woff2|eot|ttf|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'url?limit=512&&name=[path][name].[ext]?[hash]'
            }
        ]
    },
    mode: 'development',
    externals: {
        'react': 'commonjs react'
    }
};

我跟随本文创建并发布了npm包: https://codeburst.io/extracting-a-react-js-component-and-publishing-it-on-npm-2a49096757f5

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

externals配置似乎是错误的–您可能只需要externals: [nodeExternals()](来自const nodeExternals = require("webpack-node-externals");)。