6

I'm trying to use React-file-viewer. Npm tutorial is here But I have an error in the console : "you may need an appropriate loader to handle this file type" This is my code :

import FileViewer from 'react-file-viewer';
import { CustomErrorComponent } from 'custom-error';
const file = 'http://example.com/image.png'
const type = 'png'


 onError = (e) => {
    logger.logError(e, 'error in file-viewer');
 }

 <FileViewer
 fileType={type}
 filePath={file}
 errorComponent={CustomErrorComponent}
 onError={this.onError}/>

I specify, i have babel-preset-es2015 and i use it

How can I do ? Thank you

5 Answers 5

1
module: {
        loaders: [
            // .ts(x) files should first pass through the Typescript loader, and then through babel
            { test: /\.tsx?$/, loaders: ['babel', 'ts-loader'] },
            { test: /\.css$/, loaders: ['style', 'css-loader'] },
            { test: /\.scss$/, loaders: ['style', 'css-loader?modules&importLoaders=1&localIdentName=[local]-[hash:base64:5]', 'postcss-loader', 'sass'] },
            { test: /\.(png|svg|gif|jpg|jpeg)$/, loaders: [ 'url-loader', 'image-webpack?bypassOnDebug'] },
            { test: /\.(eot|woff|ttf|woff2)$/, loader: "file?name=[name].[ext]" }


        ]
    }
Sign up to request clarification or add additional context in comments.

1 Comment

does adding this fixed your problem? Please add more details on your answer so other can get help form this.
1

You have to include babel in webpack config as

loaders: [
      {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
      { test: /\.jsx$/, exclude: /node_modules/, loader: 'babel-loader' }]

As you are using

onError = (e) => {
    logger.logError(e, 'error in file-viewer');
 }

which is es6 syntax.To make it browser compatible you have to add

{test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']}

5 Comments

can you post webpack config also?
you are using es6 function so you have to add {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']} also
thank you for your help, however if i add this line in my webpack, app crash and not build :( . In the console, i have "path is not defined" Must I import or define "path" ? And how ?
use only { test: /\.js?$/, loaders: ['babel'] } for your case
The app build, but I have the same error "You may need an appropriate loader to handle this file type""
1

Make sure you have installed the following presets and plugins, as listed in node-modules/react-file-viewer/.babelrc file:

{
  "presets": [
    "react",
    "es2015",
    "stage-0"
  ],
  "plugins": [
    "transform-class-properties",
    "transform-es2015-classes",
    "transform-es2015-object-super",
    "transform-runtime"
  ]
}

Assuming you already have the react and es2015 in your project, the npm command will be:

npm install --save-dev babel-preset-stage-0 \
    babel-plugin-transform-class-properties \
    babel-plugin-transform-es2015-classes \
    babel-plugin-transform-es2015-object-super \
    babel-plugin-transform-runtime 

Comments

1

You need to import logger from Logging library, something like this:

import logger from 'logging-Lib';

see more: https://www.npmjs.com/package/react-file-viewer

Comments

1

You need to import the file using require.

// import FileViewer from "react-file-viewer";
const FileViewer = require('react-file-viewer');

After that if you are getting any error like Module not found: Can't resolve 'console'. you can run

npm install console

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.