首页 > 解决方案 > npm start 或 npx webpack 编译失败

问题描述

当我尝试 npm start 或 npx webpack 一个项目时,我使用 reactjs、draft.js、webpack 构建了一个关于简单富文本编辑器的项目,我遇到了一个显示编译信息的问题:

ERROR in ./src/index.js
Module not found: Error: Can't resolve '' in 'F:\draft_react\my_react'
 @ ./src/index.js 25:0-55

我正在使用 npm@6.4.1、webpack@4.32.2、node-v10.15.3。

这是我的 package.json 文件详细信息

const webpack = require('webpack');

module.exports = {
    entry: './src/index.js',
    output: {
      path: __dirname + '/dist',
      publicPath: '/',
      filename: 'bundle.js'
    },
    devServer: {
      contentBase: './dist',
      hot:true
    },
    module: {
        rules: [
          {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: ['babel-loader']
          },
          {
            test: /\.css$/,
            use: ['style-loader', 'css-loader']
        }
        ]
      },
    resolve: {
        extensions: ['*', '.js', '.jsx']
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ],
  };

和 index.js 文件

import React, { Component } from 'react';
import Editor, { createEditorStateWithText } from 'draft-js-plugins-editor';
import createToolbarPlugin from 'draft-js-static-toolbar-plugin';
import  editorStyles from './editorStyles.css';
import 'draft-js-static-toolbar-plugin/lib/plugin.css';

const staticToolbarPlugin = createToolbarPlugin();
const { Toolbar } = staticToolbarPlugin;
const plugins = [staticToolbarPlugin];
const text = 'The toolbar above the editor can be used for formatting text, as in conventional static editors  …';

export default class SimpleStaticToolbarEditor extends Component {
  state = {
    editorState: createEditorStateWithText(text),
  };

  onChange = (editorState) => {
    this.setState({
      editorState,
    });
  };

  focus = () => {
    this.editor.focus();
  };

  render() {
    return (
      <div>
        <div className={editorStyles.editor} onClick={this.focus}>
          <Editor
            editorState={this.state.editorState}
            onChange={this.onChange}
            plugins={plugins}
            ref={(element) => { this.editor = element; }}
          />
          <Toolbar />
        </div>
      </div>
    );
  }
}

我是 reactjs 框架的新手,我不知道如何解决这个错误好几个小时。谢谢。

标签: javascriptreactjswebpackdraftjs

解决方案


推荐阅读