2017-10-11 98 views
2

我是Reactjs的新手,开始学习它。我一直在尝试开始一个基本的Hello World程序,但它在编译级别上失败。 用create-react-app hello-world创建了一个启动hello-word程序,它给了我一个很好的文件夹结构。 ,在这里你可以看到编译错误Reactjs模块构建错误

Failed to compile 
./src/index.js 
Module build failed: Error: Failed to load plugin import: Cannot find module 
'eslint-plugin-import' 
Referenced from: 
at Array.forEach (native) 
at Array.reduceRight (native) 
This error occurred during the build time and cannot be dismissed. 

这里的错误状态无法找到模块,所以我试图安装eslint插件进口,标准..等,但仍然它没有奏效。 下面是我webpack.config.dev.js

// @remove-on-eject-begin 
/** 
* Copyright (c) 2015-present, Facebook, Inc. 
* 
* This source code is licensed under the MIT license found in the 
* LICENSE file in the root directory of this source tree. 
*/ 
// @remove-on-eject-end 
'use strict'; 

const autoprefixer = require('autoprefixer'); 
    const path = require('path'); 
    const webpack = require('webpack'); 
    const HtmlWebpackPlugin = require('html-webpack-plugin'); 
    const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack- 
    plugin'); 
    const InterpolateHtmlPlugin = require('react-dev- 
    utils/InterpolateHtmlPlugin'); 
    const WatchMissingNodeModulesPlugin = require('react-dev- 
    utils/WatchMissingNodeModulesPlugin'); 
    const eslintFormatter = require('react-dev-utils/eslintFormatter'); 
    const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'); 
    const getClientEnvironment = require('./env'); 
    const paths = require('./paths'); 


const env = getClientEnvironment(publicUrl); 

    module.exports = { 
    // You may want 'eval' instead if you prefer to see the compiled output in 
DevTools. 
    // See the discussion in https://github.com/facebookincubator/create- 
    react-app/issues/343. 
    devtool: 'cheap-module-source-map', 

entry: [ 
// We ship a few polyfills by default: 
require.resolve('./polyfills'), 
// Include an alternative client for WebpackDevServer. A client's job is to 
// connect to WebpackDevServer by a socket and get notified about changes. 
// When you save a file, the client will either apply hot updates (in case 
// of CSS changes), or refresh the page (in case of JS changes). When you 
// make a syntax error, this client will display a syntax error overlay. 
// Note: instead of the default WebpackDevServer client, we use a custom one 
// to bring better experience for Create React App users. You can replace 
// the line below with these two lines if you prefer the stock client: 
// require.resolve('webpack-dev-server/client') + '?/', 
// require.resolve('webpack/hot/dev-server'), 
require.resolve('react-dev-utils/webpackHotDevClient'), 
// Finally, this is your app's code: 
paths.appIndexJs, 
// We include the app code last so that if there is a runtime error during 
// initialization, it doesn't blow up the WebpackDevServer client, and 
// changing JS code would still trigger a refresh. 
], 
output: { 
// Next line is not used in dev but WebpackDevServer crashes without it: 
path: paths.appBuild, 
// Add /* filename */ comments to generated require()s in the output. 
pathinfo: true, 
// This does not produce a real file. It's just the virtual path that is 
// served by WebpackDevServer in development. This is the JS bundle 
// containing code from all our entry points, and the Webpack runtime. 
filename: 'static/js/bundle.js', 
// There are also additional JS chunk files if you use code splitting. 
chunkFilename: 'static/js/[name].chunk.js', 
// This is the URL that app is served from. We use "/" in development. 
publicPath: publicPath, 
// Point sourcemap entries to original disk location (format as URL on 
Windows) 
devtoolModuleFilenameTemplate: info => 
    path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'), 
}, 
resolve: { 

modules: ['node_modules', paths.appNodeModules].concat(
    // It is guaranteed to exist because we tweak it in `env.js` 
    process.env.NODE_PATH.split(path.delimiter).filter(Boolean) 
), 

    extensions: ['.web.js', '.js', '.json', '.web.jsx', '.jsx'], 
    alias: { 
    // @remove-on-eject-begin 
    // Resolve Babel runtime relative to react-scripts. 
    // It usually still works on npm 3 without this but it would be 
    // unfortunate to rely on, as react-scripts could be symlinked, 
    // and thus babel-runtime might not be resolvable from the source. 
    'babel-runtime': path.dirname(
    require.resolve('babel-runtime/package.json') 
), 
    // @remove-on-eject-end 
    // Support React Native Web 
    // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future- 
    with-react-native-for-web/ 
    'react-native': 'react-native-web', 
    }, 
    plugins: [ 
    // Prevents users from importing files from outside of src/ (or 
    node_modules/). 

    new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]), 
    ], 
    }, 
    module: { 
    strictExportPresence: true, 
    rules: [ 
    // TODO: Disable require.ensure as it's not a standard language feature. 
    // We are waiting for https://github.com/facebookincubator/create-react- 
    app/issues/2176. 
    // { parser: { requireEnsure: false } }, 

    // First, run the linter. 
    // It's important to do this before Babel processes the JS. 
    { 
    test: /\.(js|jsx)$/, 
    enforce: 'pre', 
    use: [ 
     { 
     options: { 
      formatter: eslintFormatter, 
      eslintPath: require.resolve('eslint'), 
      // @remove-on-eject-begin 
      baseConfig: { 
      extends: [require.resolve('eslint-config-react-app')], 
      }, 
      ignore: false, 
      useEslintrc: false, 
      // @remove-on-eject-end 
     }, 
     loader: require.resolve('eslint-loader'), 
     }, 
     ], 
    include: paths.appSrc, 
    }, 
    { 
    // "oneOf" will traverse all following loaders until one will 
    // match the requirements. When no loader matches it will fall 
    // back to the "file" loader at the end of the loader list. 
    oneOf: [ 


     { 
     test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/], 
     loader: require.resolve('url-loader'), 
     options: { 
      limit: 10000, 
      name: 'static/media/[name].[hash:8].[ext]', 
     }, 
     }, 
     // Process JS with Babel. 
     { 
     test: /\.(js|jsx)$/, 
     include: paths.appSrc, 
     loader: require.resolve('babel-loader'), 
     options: { 
      // @remove-on-eject-begin 
      babelrc: false, 
      presets: [require.resolve('babel-preset-react-app')], 

      cacheDirectory: true, 
     }, 
     }, 
     // "postcss" loader applies autoprefixer to our CSS. 
     // "css" loader resolves paths in CSS and adds assets as dependencies. 
     // "style" loader turns CSS into JS modules that inject <style> tags. 
     // In production, we use a plugin to extract that CSS to a file, but 
     // in development "style" loader enables hot editing of CSS. 
     { 
     test: /\.css$/, 
     use: [ 
      require.resolve('style-loader'), 
      { 
      loader: require.resolve('css-loader'), 
      options: { 
       importLoaders: 1, 
      }, 
      }, 
      { 
      loader: require.resolve('postcss-loader'), 
      options: { 
       // Necessary for external CSS imports to work 

       ident: 'postcss', 
       plugins:() => [ 
       require('postcss-flexbugs-fixes'), 
       autoprefixer({ 
        browsers: [ 
        '>1%', 
        'last 4 versions', 
        'Firefox ESR', 
        'not ie < 9', // React doesn't support IE8 anyway 
        ], 
        flexbox: 'no-2009', 
       }), 
       ], 
      }, 
      }, 
     ], 
     }, 

     { 
     // Exclude `js` files to keep "css" loader working as it injects 
     // it's runtime that would otherwise processed through "file" 
     loader. 
     // Also exclude `html` and `json` extensions so they get processed 
     // by webpacks internal loaders. 
     exclude: [/\.js$/, /\.html$/, /\.json$/], 
     loader: require.resolve('file-loader'), 
     options: { 
      name: 'static/media/[name].[hash:8].[ext]', 
     }, 
     }, 
     ], 
    }, 

    ], 
    }, 
    plugins: [ 

    new InterpolateHtmlPlugin(env.raw), 
    // Generates an `index.html` file with the <script> injected. 
    new HtmlWebpackPlugin({ 
    inject: true, 
    template: paths.appHtml, 
     }), 

    new webpack.NamedModulesPlugin(), 

    new webpack.DefinePlugin(env.stringified), 

    new webpack.HotModuleReplacementPlugin(), 

    new CaseSensitivePathsPlugin(), 

    new WatchMissingNodeModulesPlugin(paths.appNodeModules), 

    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), 
    ], 

    node: { 
dgram: 'empty', 
fs: 'empty', 
net: 'empty', 
tls: 'empty', 
child_process: 'empty', 
}, 

    performance: { 
    hints: false, 
}, 
}; 

任何一个可以指导我如何走出这场生成错误的。

+0

在做了create-react-app之后,你做了一个npm安装(或者纱线安装,如果你正在使用纱线)吗? – Sri

+0

是的,我确实安装了 –

+0

这个github问题涉及到你的问题。切换到纱线似乎解决了这个问题。 https://github.com/facebookincubator/create-react-app/issues/2779 – Sri

回答

0

新鲜npm install eslint-plugin-import并重新启动应用程序应该解决此问题。

如果没有解决,请尝试升级您的npm版本:

npm install -g [email protected] 
+0

感谢您的回复,我尝试了他们,但它不起作用 –

+0

请问您可以评论节点和npm版本的输出吗? 'node -v'和'npm -v' – Abhijith

+0

npm版本:5.4.2节点版本:6.11.1 –

0

最后问题得到了解决下面的包安装几个全球后:

eslint-config-react-app 
eslint 
babel-eslint 
eslint-plugin-react 
eslint-plugin-import 
eslint-plugin-jsx-a11y 
eslint-plugin-flowtype 

和已删除文件package.lock.json然后运行npm install 最后npm start它刚刚工作。谢谢

相关问题