【问题标题】:You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件
【发布时间】:2019-11-01 18:15:01
【问题描述】:

我正在尝试将 webpack 与我的 react 项目集成,但收到以下错误消息。

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.

我试图在这里找到一些答案,但无济于事。看起来问题出在我尝试渲染整个应用程序的 index.js 页面

ReactDOM.render(<Main />, document.getElementById('root'));

这是我的 package.json 文件:

{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^4.4.2",
"@material-ui/icons": "^4.4.1",
"axios": "^0.19.0",
"babel-core": "^6.26.3",
"material-ui": "^0.20.2",
"react": "^16.10.2",
"react-activity": "^1.2.2",
"react-dom": "^16.10.2",
"react-dropzone": "^10.1.10",
"react-redux": "^7.1.1",
"react-responsive-carousel": "^3.1.51",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-router-flux": "^1.0.0",
"react-scripts": "3.1.1",
"react-tap-event-plugin": "^3.0.3",
"redux": "^4.0.4",
"redux-persist": "^6.0.0",
"redux-promise": "^0.6.0",
"redux-thunk": "^2.3.0",
"serve": "^11.2.0",
"superagent": "^5.1.0"
},
"scripts": {
"start": "PORT=8080 react-scripts start",
"build": "webpack --env.API_URL=https://neighbor-hood.herokuapp.com --config 
webpack.config.build.js",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
  ">0.2%",
  "not dead",
  "not op_mini all"
],
"development": [
  "last 1 chrome version",
  "last 1 firefox version",
  "last 1 safari version"
]
},
"devDependencies": {
"webpack": "^4.39.1",
"webpack-cli": "^3.3.10"
}
}

这是我的 webpack.config.build.js 文件:

const webpack = require('webpack'); 
module.exports = (env) => {
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev;
}, {});

module:{
    rules:[{
        loader: 'babel-loader',
        test: /\.js$|jsx/,
        exclude: /node_modules/
    }]
}


return {
plugins: [
  new webpack.DefinePlugin(envKeys)
]
};
};

如有必要,任何我的 .babelrc 文件:

.babelrc
{
"presets": ["env", "react","@babel/preset-env"],
"plugins": [
    "transform-class-properties"
]
}

这是我的 index.js 文件:

import React from 'react';
import ReactDOM from 'react-dom';

import Main from './Main';
import * as serviceWorker from './serviceWorker';


ReactDOM.render(<Main />, document.getElementById('root'));

现在不知道这意味着什么......(可能已经回答了)

【问题讨论】:

  • 您需要详细说明错误发生的位置,它指的是哪种文件类型?
  • @logos_164 我在上面编辑了我的帖子。问题来自我的 index.js 文件,我在该文件中尝试渲染整个应用程序 --> ReactDOM.render(
    , document.getElementById('root'));

标签: javascript reactjs web-deployment


【解决方案1】:

您的直接问题就在这里。在--config 之后有一个回车或换行。

"build": "webpack --env.API_URL=https://neighbor-hood.herokuapp.com --config 
webpack.config.build.js",

试试这个。

"build": "webpack --env.API_URL=https://neighbor-hood.herokuapp.com --config webpack.config.build.js",

【讨论】:

  • 你的第一个解决方案正是我所拥有的......?
  • 你有一个回车。您需要删除它或添加 " + " 以连接这些行。
  • 当我复制/粘贴您的第一个答案时,我收到相同的错误消息。第二个抛出“JSON 中的意外令牌 +”
  • 哦,那可能不是解决您问题的答案。当我将它复制/粘贴到我的 vscode 窗口中以进行格式化时,这只是我在您的代码中注意到的一个问题。我将更新答案以删除第二个选项。您的问题可能还有更多。你介意发布你的 index.js 吗?
  • 我刚刚添加了它。感谢您对此的帮助
猜你喜欢
  • 2021-08-11
  • 2020-01-15
  • 2021-11-21
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 2021-12-11
  • 2020-06-22
相关资源
最近更新 更多