Module not found: Error: Can't resolve './component/Hello' - reactjs

I´m trying to import a simple Component but the webpack seems to cant find it. The route is good and the "resolve" in the webpack config is great too, therefore I cant understand where is the issue.
Give it at look please.
By the way, its a Sails/React environment.
ERROR in ./assets/src/component/Hello.jsx 6:12
Module parse failed: Unexpected token (6:12)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
Hello.jsx:
import React from 'react'
class Hello extends React.Component {
render() {
return(
<div> // Err supposed to be here (line6)
Hello World!
</div>
)
}
}
export default Hello;
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import Hello from './component/Hello'
const App = () => {
return (
<div>
Simple Sails-React stater
<Hello/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
.babelrc:
{
"presets": ["#babel/env", "#babel/react"]
}
webpack config file:
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
entry: './assets/src/index.js'
},
output: {
path: __dirname + '/.tmp/public',
filename: 'bundle.js'
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
use: ['style-loader', 'css-loader'],
test: /\.css$/
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
plugins: [
new HtmlWebpackPlugin({
template: 'assets/src/index.html'
})
]
};
the structure is like this:
-src
--component
----Hello.jsx
--index.js
--index.html

Could you try change extension to Hello.js or change bable-loader test to
test: /\.(js|jsx)$/,

Related

Module not found: Error: Can't resolve ' ' when trying to run webpack

I get this error when I try to run webpack but it doesnt really show me what module it cant find. I get this error 4 times in different files. I am starting webpack via commandline with "webpack". I dont see the point
ERROR in ./src/index.js
Module not found: Error: Can't resolve '' in 'C:\Users\topal\IdeaProjects\cts-abwesendheitstool\cts-abwesendheit-fe'
# ./src/index.js 4:0-21
This is my code in index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<App/>,
document.getElementById('root')
);
my webpack config
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: path.resolve(__dirname, './src/index.js'),
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [[
'#babel/preset-env', {
targets: {
esmodules: true
}
}],
'#babel/preset-react']
}
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader!',
},
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env',
'#babel/react', {
'plugins': ['#babel/plugin-proposal-class-properties']
}]
}
},
{test: /\.json$/, loader: 'json-loader'}
],
},
resolve: {
extensions: ['*', '.js', '.jsx','.ts', '.tsx'],
},
output: {
path: path.resolve(__dirname, './dist'),
filename: 'bundle.js',
},
plugins: [new webpack.HotModuleReplacementPlugin()],
devServer: {
contentBase: path.resolve(__dirname, './dist'),
hot: true,
},
};
In a different file:
import "./App.css";
import Datagrid from "./pages/Datagrid"
import {React, useEffect, useState} from "react";
import * as config from "./config/config";
import DaterangePicker from "./components/DaterangePicker";
function App() {
const [data,setData]= useState([]);
useEffect(()=>{
const requestOptions = {
method: 'GET'
};
fetch(config.SERVER_URL + `/caldav/getEvents/${config.CALENDAR}`, requestOptions)
.then((response) => response.json())
.then(data => setData(data))
.catch(error => console.log(error));
},[data.length]);
return (
<div className="App">
<header className="App-header">
Abwesendheitscheckliste
</header>
<DaterangePicker/>
<Datagrid rows={data}></Datagrid>
</div>
);
}
export default App;
I get the same error:
ERROR in ./src/App.js
Module not found: Error: Can't resolve '' in 'C:\Users\topal\IdeaProjects\cts-abwesendheitstool\cts-abwesendheit-fe'
# ./src/App.js 2:0-19
# ./src/index.js
Can you try something like this in webpack
use: ["style-loader", "css-loader"],

Why is this react-hot-loader not working I get error about loaders when they call rules

I followed this how-to-add-hot-module-reloading-to-a-react-app but get an error.
I know this is an easy one because i'm new to this. Also most tutorials are so out of date so its hard to find something consistent to learn this
I think the webpack server is ok and can be started ok
Please advice:
ERROR in ./src/index.js 5:16
Module parse failed: Unexpected token (5:16)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import App from "./App";
|
> ReactDOM.render(<App />, document.getElementById("root"));
Here's webpack.config.js
const webpack = require('webpack');
const port = process.env.PORT || 3000;
module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
],
rules: [
{
test: /\.json$/,
loader: 'json-loader'
}
],
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
devServer: {
contentBase: './dist',
hot: true
},
devServer: {
host: 'localhost',
port: port,
historyApiFallback: true,
open: true
}
};
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));
UPDATE
Adding App.js
import React, { Component } from "react";
import Header from "./components/structure/Header";
import Content from "./components/structure/Content";
import Footer from "./components/structure/Footer";
import Resume from "./resume.json";
import { hot } from 'react-hot-loader/root'
class App extends Component {
componentDidMount() {
document.title = [
Resume.basics.name,
Resume.basics.label,
[Resume.basics.location.region, Resume.basics.location.country].join(", ")
].join(" | ");
}
render() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
}
}
export default hot(App)
Also get this error now .babelrc cant find schema:

Minified React error #130 when building react module

I'm getting
Minified React error #130
when i import my react module to another application maybe the module build is wierd or not correct.
I referenced a similar question.
Uncaught Error: Minified React error #130
but i think i have it set up correctly.
src/Fetch
import React, {Component} from 'react';
import MyFetch from './MyFetch';
const Fetch = (props) => (
<div>
<form onSubmit={props.onSubmit}>
<input type="text" value={props.url} placeholder="enter a url" onChange={props.onChange}/>
</form>
<MyFetch url={props.url} method={props.method}/>
</div>
)
export default Fetch;
src/index.js
import Fetch from './Fetch';
export default Fetch
dist/index.js
import React from 'react';
import { render} from 'react-dom';
import Fetch from '../src'
const App = () => (
<Fetch />
);
render(<App />, document.getElementById("root"));
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: path.join(__dirname, "dist/index.html"),
filename: "./index.html"
});
module.exports = {
entry: path.join(__dirname, "dist/index.js"),
output: {
path: path.join(__dirname, './'),
filename: 'index.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components|build)/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env', '#babel/react'],
plugins:['#babel/plugin-proposal-class-properties']
}
}
}
]
},
plugins: [htmlWebpackPlugin],
resolve: {
extensions: [".js", ".jsx"]
},
devServer: {
port: 3001
}
};
fixed it
plugins: [htmlWebpackPlugin,new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
}
})]
React - Minified exception occurred

Webpack doesn't understand React's syntax

I am learning both React and Webpack. Now I have written a component that displays a name ;-)
const app = document.getElementById('app');
class World extends React.Component {
constructor(props) {
super(props);
this.name = 'Tomek';
this.callByName = this.callByName.bind(this); /* Or bind directly when calling */
}
callByName() {
alert(this.name);
}
render() {
return (
<div>
<h2>Hello, {this.name}</h2>
<button onClick={this.callByName}>Alert</button>
</div>
)
}
}
ReactDOM.render(<World />, app);
I import it with React and ReactDOM:
import React from 'react';
import ReactDOM from 'react-dom';
import './components/posts/index.js';
I use Webpack to process my JS:
module.exports = {
entry: [
'./_babel/index.js'
],
output: {
path: __dirname + '/_js',
filename: 'index.js'
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }
]
}
};
Unfortunately, when I run webpack, I get
ERROR in ./_babel/components/posts/index.js
Module build failed: SyntaxError: /Users/tomek/Sites/wordpress/wp-content/themes/Devoid/_babel/components/posts/index.js: Unexpected token (17:6)
render() {
return (
<div>
<h2>Hello, {this.name}</h2>
<button onClick={this.callByName}>Alert</button>
</div>
Propably I just forgot something, but I can't seem to find what.
You need to add the react loader to your webpack.config.js. I also suggest adding ES2015 loader. Try this:
module.exports = {
entry: [
'./_babel/index.js'
],
output: {
path: __dirname + '/_js',
filename: 'index.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {presets: ['es2015']}
},
{
test: /\.jsx$/,
loader:'babel-loader',
query: {presets: ['es2015', 'react']}
}
]
}
};

How to render a foo.md markdown file in react?

I have several .md files (containing long texts) and I want to render them through react. I tried to use markedown-it but the loader returns an error. Here is the webpack.config.js file:
var path = require('path');
var webpack = require('webpack');
var subscript = require('markdown-it');
var superscript = require('markdown-it');
module.exports = {
entry: ['./src/first.jsx'],
devtool: 'cheap-module-eval-source-map',
output: { path: __dirname+"/app", filename: 'bundle.js' },
module: {
loaders: [
{ test: /\.jsx?$/,
loader: 'babel-loader',
query: { presets: ['es2015', 'react'] },
include: path.join(__dirname, 'src')
},
{ test: /\.md/,
loader: 'markdown-it'
}
]
},
'markdown-it': {
preset: 'default',
typographer: true,
use: [subscript, superscript]
}
};
Is there something wrong with that file? How else I can add my *.md files to react?
After reading http://www.shoutinginfrench.com/today-i-made-react-load-markdown/ I tried to use markdown-loader. Following that, I added this to webpack.config file:
{ test: /\.md$/,
loader: "html!markdown"
}
which worked with no problem. Then I tried to add the markdown file to the react component as follow:
import React from 'react';
import { Link } from 'react-router'
import markdownFile from './test-file.md';
export const Test = React.createClass({
rawMarkup(){
return { __html: markdownFile };
},
render() {
return (
<div className="something">
<div className="row">
<div className="col-10">
<div dangerouslySetInnerHtml={this.rawMarkup()} />
</div>
</div>
</div>
);
}
});
But I'm getting the following error:
ERROR in ./src/components/tst.jsx
Module not found: Error: Cannot resolve module 'html' in /Users/..../src/components
# ./src/components/tst.jsx 14:15-39
How can I fix it?!
add { test: /\.md$/, loader: "html!markdown" },{ test: /\.json$/, loader: "json" } to your webpack.config.js .
npm install react-markdown --save-dev

Resources