当前位置:  开发笔记 > 编程语言 > 正文

错误:使用webpack时无法"文件"或"目录"./lib/React

如何解决《错误:使用webpack时无法"文件"或"目录"./lib/React》经验,为你挑选了1个好方法。

我一直在使用反应,现在想用webpack尝试.

这是我的webpack.config.js:

var path = require('path');

module.exports = {
    entry: './app.js',
    output: {
        path: path.resolve(__dirname, "build"),
        publicPath: "/",
        filename: "bundle.js"
    },
    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['react', 'es2015']
                }
            }
        ]
    },
    resolve: {
        extensions: ['', 'js', 'jsx']
    }
}

发生的错误如下:

ERROR in ./~/react/react.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./lib/React in /Users/username/path/to/app/node_modules/react
 @ ./~/react/react.js 3:17-39

当我通过使用var React = require('react')或导入反应到我的文件时发生此错误import React from 'react';

我安装了babel所需的预设包:

  "devDependencies": {
    "babel-core": "^6.4.0",
    "babel-loader": "^6.2.1",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "webpack": "^1.12.10",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "highcharts-release": "^4.2.1",
    "immutable": "^3.7.6",
    "react": "^0.14.5",
    "react-highcharts": "^6.0.0",
    "react-redux": "^4.0.6",
    "redux": "^3.0.5"
  }

谢谢!



1> taylorc93..:

根据您的引擎收录例子,你可以看到的WebPack正在寻找React,ReactjsReactjsxReact.js:

/Users/bIgB/Private/Code/go-projects/src/github.com/bIgBV/cereberus/app/node_modules/react/lib/React doesn't exist
  /Users/bIgB/Private/Code/go-projects/src/github.com/bIgBV/cereberus/app/node_modules/react/lib/Reactjs doesn't exist
  /Users/bIgB/Private/Code/go-projects/src/github.com/bIgBV/cereberus/app/node_modules/react/lib/Reactjsx doesn't exist

要解决此问题,只需.在扩展程序的前面添加一个,如下所示:

resolve: {
    extensions: ['', '.js', '.jsx']
}

https://webpack.github.io/docs/configuration.html#resolve-extensions

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有