Webpack错误:找不到模块:错误:无法解析' file'或者'目录'

时间:2016-09-24 02:57:26

标签: reactjs webpack jsx create-react-app

我正在使用create-react-app自学React。但是,我遇到了一个对我没有任何意义的错误。

错误是:

Error: Child compilation failed: Module not found: Error: Cannot resolve 'file' or 'directory' ./src/name-icon. png in C:\Users\Jacob\college-canoe: Error: Cannot resolve 'file' or 'directory' ./src/name-icon.png in C:\Users\Ja cob\college-canoe

我看到WebPack"认为"问题是,但我的代码实际上并没有在任何地方调用name-icon。它曾经,但我改名为nameIcon。实际使用该图像的唯一代码片段是:

import React, { Component } from 'react';

import nameIcon from './photos/nameIcon.png';

export default class NavBar extends Component {
  render() {
    return (
  <div className="navbar-container">
    <ul className="navbar-top-right">
      <img className="icon" src={nameIcon} />
      <li><a className="navbar" href="#">Search</a></li>
      <li><a className="navbar" href="#contact">Contact</a></li>
      <li><a className="navbar" href="#">Compare</a></li>
      <li><a className="navbar" href="#">Home</a></li>
    </ul>
  </div>
    )
  }
}

当我使用nameIcon注释掉代码时,问题甚至会持续存在。我完全迷失在这里,任何帮助都会很棒。

编辑:在webpack.config.js中添加

var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CaseSensitivePathsPlugin = require('../index.js'); // use inside the npm package

// We use the NODE_ENV in our automation commands to differentiate environments
var production =
  process.env.NODE_ENV === 'production' ||
  process.env.NODE_ENV === 'preprod';

// Setup our plugins.
var plugins = [
  // attaches the webpack-generated JS to our main HTML file
  new HtmlWebpackPlugin({template: './src/index.html'}),
  // create global access to the NODE_ENV within our Webpacked code:
  new webpack.DefinePlugin({
    __ENV__: JSON.stringify(process.env.NODE_ENV)
  }),
  // http://gaearon.github.io/react-hot-loader/getstarted/
  new webpack.HotModuleReplacementPlugin(),
  // Mac doesn't care about case, but linux servers do, so enforce...
  new CaseSensitivePathsPlugin()
];

// In production we do a bit more...
if (production) {
  plugins.concat(
    [
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.OccurenceOrderPlugin(),
      new webpack.optimize.UglifyJsPlugin({
        compress: {
          warnings: false
        }
      })
    ]);
}

const devEntry = [
    'webpack-dev-server/client?http://0.0.0.0:3000', // tells client where to get hot reloads
    'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
    'babel-polyfill', // for full ES6 compatibility on older devices
    './src/init.js'
  ];
const prodEntry = [
    'babel-polyfill', // for full ES6 compatibility on older devices
    './src/init.js'
];

const theEntry = (production) ? prodEntry : devEntry;

module.exports = {

  // Bundle to our dist folder as a main.js file.
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'main.js',
    publicPath: '/'
  },

  devtool: 'sourcemap',

  // Our master entry point.
  entry: theEntry,

  // Extra helpers to make require or include easier.
  resolve: {
    extensions: ['', '.js', '.jsx', '.json']
  },

  module: {
    loaders: [{
      test: /\.(js|jsx)$/,
      // in dev only, hotload
      loader: production ? 'babel' : 'react-hot!babel',
      // other babel options are specified in .babelrc
      exclude: /node_modules/
    }, {
      test: /\.json$/,
      loader: 'json'
    }]
  },

  plugins: plugins
};

编辑2:添加了项目结构。

Project Structure

编辑3:仍然不知道问题是什么,但是将nameIcon重命名为name-icon并将其重新放回/ src /并在my / photos /文件夹中有第二个副本以某种方式修复它。我稍后将不得不回到此处。

1 个答案:

答案 0 :(得分:0)

你必须安装文件加载器

npm install --save-dev file-loader

然后模块设置 -

 module: {
    loaders: [{
        test: /\.(js|jsx)$/,
        // in dev only, hotload
        loader: production ? 'babel' : 'react-hot!babel',
        // other babel options are specified in .babelrc
        exclude: /node_modules/
      }, {
        test: /\.json$/,
        loader: 'json'
      },
      {test: /\.(jpe?g|png|gif)$/i, loader: 'file?name=[name].[ext]'}
    ]
  },