javascript - 无法导入模块 : You may need an appropriate loader

标签 javascript node.js reactjs webpack bower

我有一个使用 bowerwebpack 的 React 项目。

我正在尝试使用这个模块 https://github.com/jrowny/react-absolute-grid .

我用 npm 安装它,然后像这样将它添加到我的代码中:

import AbsoluteGrid from 'react-absolute-grid/lib/AbsoluteGrid.jsx';

导入模块时,我遇到了这个问题:

Line 3: Unexpected token
You may need an appropriate loader to handle this file type.
| 'use strict';
|
| import React from 'react';

我怀疑问题是在 webpack.config.js 中我只从我的代码所在的位置加载文件:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');

var config = {
  entry: APP_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  module : {
    loaders : [
      {
        test : /\.jsx?/,
        include : APP_DIR,
        loader : 'babel'
      }
    ]
  }
}

module.exports = config;

不过,我不确定。

我在 SO 上看到了带有类似错误消息的问题,但据我所知,它们还有其他问题。

最佳答案

更改导入模块的方式:

来自:

import AbsoluteGrid from 'react-absolute-grid/lib/AbsoluteGrid.jsx';

收件人:

import AbsoluteGrid from 'react-absolute-grid';

你最好在 webpack 配置中排除 node_modulesbower_components:

...
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/

另外,你最好使用babel-loaderreactes2015 babel 预设。

安装它们:

npm install --save-dev babel-loader babel-core babel-preset-react babel-preset-es2015

并包含在 webpack 配置中:

module : {
  loaders : [
    {
      test : /\.jsx?/,
      loader: 'babel-loader',
      exclude: /(node_modules|bower_components)/,
      query: {
        presets: ['react', 'es2015']
      }
    }
  ]  
}

关于javascript - 无法导入模块 : You may need an appropriate loader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486861/

相关文章:

javascript - Node.JS MySql 模块 - 无法连接到任何数据库 - ECONNRESET

javascript - 如何通过样式表中定义的 Javascript 读出 CSS 文本?

javascript - Angular API 引用代码

node.js - 制作一个接受命令行参数的脚本

javascript - 回调、错误捕获和 MongoDB 的问题

node.js - Nodejs 传递流

javascript - 处理异步回调和渲染 DOM 的更好方法

javascript - 为什么我不能在 React 中返回一个对象

javascript - React-router 中的多个子路由

javascript - 刷新页面时标题外观不起作用