react js flux implementation using redux gives an error: unexpected character '?' - reactjs

I created a React simple flux project implemented using Redux.It was giving continuous errors, after fixing many of them it's giving an error in my App.jsx file.It's actually reading '⇒' character in my App.jsx file as '?'.
Here's my webpack.config.js file
module.exports = {
entry: './main.js',
mode: 'production',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
cacheDirectory: true,
presets: ['react', 'es2015']
}
}
]
}
}
and here's my package.json
{
"name": "ReactApp1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"redux": "^4.0.0",
"save": "^2.3.2",
"webpack": "^4.6.0",
"webpack-cli": "^2.0.15",
"webpack-dev-server": "^3.1.3"
}
}
Here's my App.jsx file
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { addTodo } from './actions/actions'
import AddTodo from './components/AddTodo.js'
import TodoList from './components/TodoList.js'
class App extends Component {
render() {
const { dispatch, visibleTodos } = this.props
return (
<div>
<AddTodo onAddClick = {text⇒dispatch(addTodo(text))} />
<TodoList todos = {visibleTodos}/>
</div>
)
}
}
function select(state) {
return {
visibleTodos: state.todos
}
}
export default connect(select)(App);
and this is the error I got
ERROR in ./App.jsx
Module build failed: SyntaxError: C:/Users/Administrator/Desktop/ReactApp/App.js
x: Unexpected character '?' (15:39)
13 | return (
14 | <div>
> 15 | <AddTodo onAddClick = {text?dispatch(addTodo(text))} />
| ^
16 | <TodoList todos = {visibleTodos}/>
17 | </div>
18 | )
# ./main.js 13:11-31
# multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-serve
r ./main.js
i ?wdm?: Failed to compile.

Don't copy paste the code. Type by hand instead, so that you can avoid error like this:
<AddTodo onAddClick = {text⇒dispatch(addTodo(text))} />
// ^
It should be:
<AddTodo onAddClick = {text=>dispatch(addTodo(text))} />

Related

Why is a function imported to the react jsx file return "Object is not a function error message"?

I am implementing code for a CRUD application using react(JSX) and redux for front-end. If I try to run a function imported from actions.js file inside of the react render/return statement, it does not recognize the imported function, but returns the error message of:
"Uncaught TypeError: Object(...) is not a function
at InputBox.onSubmitForm (InputBox.jsx:100)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306)
at executeDispatch (react-dom.development.js:389)
at executeDispatchesInOrder (react-dom.development.js:414)
at executeDispatchesAndRelease (react-dom.development.js:3278)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3287)
at forEachAccumulated (react-dom.development.js:3259)
onSubmitForm # InputBox.jsx:100
callCallback # react-dom.development.js:188"
and so on...
when I click the inputBox.jsx:96, it refers to: "Object(_actions_action_js__WEBPACK_IMPORTED_MODULE_2__["default"])(e.target[0].value);"
We have tried searching for the same error message in stackoverflow and google. We have also installed #babel/plugin-proposal-class-properties devDependency.
InputBox.jsx(component):
import React, { Component } from 'react';
import { connect } from 'react-redux';
import postToDo from '../actions/action.js'
class InputBox extends Component {
constructor(props) {
super(props)
}
onSubForm (e) {
e.preventDefault();
console.log(e.target[0].value)
postToDo(e.target[0].value)
}
render() {
return (
<div>
<form onSubmit={this.onSubForm}>
<input
type="text"
className="form-control"/>
<button className="btn btn-success">Add</button>
</form>
</div>
)
};
}
export default InputBox;
action.js:
export const postToDo = (url) => {
console.log('I fired');
fetch('http://localhost:3000/todos', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(url),
})
.then((res) => res.json())
.then((data) => {
const urlObj = {
url,
status: data.status,
url_Id: data.url_Id,
};
})
.then(() => this.props.dispatchCheckStatus(statusObj))
.catch((err) => {
console.error(err.messsage);
});
};
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'development',
entry: './client/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.jsx?/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env', '#babel/preset-react']
},
},
exclude: /node_modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.js', '.jsx'],
},
devServer: {
historyApiFallback: true,
contentBase: path.resolve(__dirname, './client'),
port: 8080,
proxy: {
'/api': 'http://localhost:3000',
},
publicPath: '/build/',
},
};
package.json:
{
"name": "Scratch-Project",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"start": "nodemon server/server.js",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "cross-env NODE_ENV=production webpack",
"frontend": "NODE_ENV=development webpack-dev-server --open",
"dev": "concurrently \"cross-env NODE_ENV=development webpack-dev-server --open\" \"nodemon ./server/server.js\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/JoonyoungKim025/Scratch-Project.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/JoonyoungKim025/Scratch-Project/issues"
},
"homepage": "https://github.com/JoonyoungKim025/Scratch-Project#readme",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"pg": "^8.3.3",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-dom": "^16.13.1",
"react-redux": "^7.2.1",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"redux": "^4.0.5"
},
"devDependencies": {
"#babel/core": "^7.11.6",
"#babel/preset-env": "^7.11.5",
"#babel/preset-react": "^7.10.4",
"babel-core": "^6.26.3",
"babel-loader": "^8.1.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"concurrently": "^5.3.0",
"cross-env": "^7.0.2",
"css-loader": "^4.3.0",
"isomorphic-fetch": "^2.2.1",
"nodemon": "^2.0.4",
"redux-devtools-extension": "^2.13.8",
"style-loader": "^1.2.1",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
}
}
.babelrc :
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": [
["#babel/plugin-proposal-class-properties", { "loose": true }]
]
}
// {
// "presets": [
// [
// "next/babel",
// {
// "preset-env": { "targets": { "node": true } }
// }
// ]
// ],
// "plugins": []
// }
Sorry for posting potentially unnecessary files, I am a beginner.
You import using default import syntax here:
import postToDo from '../actions/action.js'
But in the action:
export const postToDo = (url) => {
you're using named export syntax.
Either change the import to use the named import instead:
import { postToDo } from '../actions/action.js'
Or change the named export to a default export:
export default (url) => {

ReactJS Error: Module Build failed: SyntaxError: Unexpected token '<'

My ReactJS code throws an error when I try to compile it. I used ReactJS before but I never ran into this error (see below)
ERROR in ./kapiche/#kapiche_react/teacher/login.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /mnt/d/src/kapiche/#kapiche_react/teacher/login.jsx: Unexpected token (59.6)
57 | render() {
58 | return (
> 59 | <div>
| ^
60 | Hello world
61 | </div>
62 | );
at Parser.raise (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:6420:17)
at Parser.unexpected (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:7773:16)
at Parser.parseExprAtom (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8996:20)
at Parser.parseExprSubscripts (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8556:23)
at Parser.parseMaybeUnary (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8536:21)
at Parser.parseExprOps (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8402:23)
at Parser.parseMaybeConditional (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8375:23)
at Parser.parseMaybeAssign (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8325:21)
at Parser.parseParenAndDistinguishExpression (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9133:28)
at Parser.parseExprAtom (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8917:21)
at Parser.parseExprSubscripts (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8556:23)
at Parser.parseMaybeUnary (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8536:21)
at Parser.parseExprOps (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8402:23)
at Parser.parseMaybeConditional (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8375:23)
at Parser.parseMaybeAssign (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8325:21)
at Parser.parseExpression (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8275:23)
at Parser.parseReturnStatement (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10378:28)
at Parser.parseStatementContent (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10057:21)
at Parser.parseStatement (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10009:17)
at Parser.parseBlockOrModuleBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10585:25)
at Parser.parseBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10572:10)
at Parser.parseBlock (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10556:10)
at Parser.parseFunctionBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9584:24)
at Parser.parseFunctionBodyAndFinish (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9554:10)
at Parser.parseMethod (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9508:10)
at Parser.pushClassMethod (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10987:30)
at Parser.parseClassMemberWithIsStatic (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10912:12)
at Parser.parseClassMember (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10851:10)
at /mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10806:14
at Parser.withTopicForbiddingContext (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9884:14)
at Parser.parseClassBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10783:10)
at Parser.parseClass (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10757:22)
at Parser.parseStatementContent (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10051:21)
at Parser.parseStatement (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10009:17)
at Parser.parseBlockOrModuleBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10585:25)
at Parser.parseBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10572:10)
This is my webpack.config.js where I define my entry and output points.
// create proper string representing path to file
const path = require('path');
// files to export
let export_list = [];
// helpful log message for debugging
console.log('webpack watch starting for the files:\n');
// general files
const export_dict = {
'student': ['login'],
'teacher': ['login']
};
// loop through all directories and files in export dictionary
for (let [key, value] of Object.entries(export_dict)) {
for (let i = 0; i < export_dict[key].length; i++) {
// helpful log message for debugging
console.log(`entry:\t/kapiche/#kapiche_react/${key}/${value[i]}.jsx`);
console.log(`output:\t/kapiche/static/js/${key}/${value[i]}_compiled.js\n`);
// add object to export list
export_list.push(
{
entry: `./kapiche/#kapiche_react/${key}/${value[i]}.jsx`,
output: {
path: path.join(__dirname, `/kapiche/static/js/${key}/`),
filename: `${value[i]}_compiled.js`,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
}
)
}
}
console.log(export_list);
// list of exports
module.exports = export_list;
Here is the ReactJS file I am trying to compile (in jsx)
import React from 'react';
import ReactDOM from 'react-dom';
class LoginTeacher extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
componentDidMount() {
}
render() {
return (
<div>
Hello world
</div>
);
}
}
ReactDOM.render(
<LoginTeacher/>,
document.getElementById('login'),
);
And finally, here is my package.json
{
"name": "kapiche",
"version": "1.0.0",
"description": "Promoting classroom communication",
"main": "webpack.config.js",
"scripts": {
"watch": "webpack --watch --mode production",
"start": "webpack-dev-server --open --hot --mode development",
"build": "webpack --mode production"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jbseg/BlueSlide.git"
},
"author": "Joshua S, Riley C, Hannah Z, Xiangliang N",
"license": "MIT",
"bugs": {
"url": "https://github.com/jbseg/BlueSlide/issues"
},
"homepage": "https://github.com/jbseg/BlueSlide#readme",
"devDependencies": {
"#babel/core": "^7.6.4",
"#babel/preset-env": "^7.6.3",
"#babel/preset-react": "^7.6.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"sass": "^1.23.3",
"style-loader": "^1.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
},
"dependencies": {
"#material-ui/core": "^4.5.2",
"#material-ui/icons": "^4.5.1",
"chart.js": "^2.9.2",
"react": "^16.11.0",
"react-chartjs-2": "^2.8.0",
"react-dom": "^16.11.0",
"react-tabs": "^3.0.0",
"socket.io": "^2.3.0"
}
}
Any ideas what might be causing this error?
EDIT: Here is my .babelrc as well
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
After a few hours of trying to figure this one out, I finally managed to catch the bug. It was a typo... yep, a programmer's worst enemy. I misspelled my .babelrc as .bablerc and that was the root of my issue. Thank you everyone for the feedback, I am going to go be embarrassed now...
Make sure you have imported the right thing !
the vs code has imported
import { Button , Modal } from "bootstrap";
You should import
import { Button , Modal } from "react-bootstrap";

Module parse failed: unexpected token you may need an appropriate loader to handle this file type

I keep getting this error and I'm not sure why. Here's my error:
ERROR:
ERROR in ./components/TaglocationList 6:4
Module parse failed: Unexpected token (6:4)
You may need an appropriate loader to handle this file type.
|
| const TaglocationList = (props) => (
<div>
| Taglocation List:
| <ul>
# ./components/DashBoard.js 11:23-51
# ./routers/AppRouter.js
# ./App/index.js
# ./index.js # multi ../node_modules/webpack-dev-server/client?
http://0.0.0.0:3000 webpack/h
ot/dev-server react-hot-loader/patch webpack-dev-server/client?
http://localhost:
3000 ./index.js
i ?wdm?: Failed to compile.
webpack.config
const { resolve } = require('path')
const webpack = require('webpack')
module.exports = {
context: resolve(__dirname, 'src'),
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:3000',
'./index.js',
'webpack/hot/only-dev-server',
],
output: {
filename: 'bundle.js',
path: resolve(__dirname, 'dist')
},
devServer: {
contentBase: resolve(__dirname, 'dist'),
host: '0.0.0.0',
hot: true,
port: 3000,
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /\.s?css$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
}
]
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
]
}
package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"main": "src/index.js",
"dependencies": {
"axios": "0.18.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"enzyme": "^2.9.1",
"eslint": "^4.7.2",
"eslint-config-standard": "^10.2.1",
"eslint-config-standard-jsx": "^4.0.2",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.1.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-react": "^7.3.0",
"eslint-plugin-standard": "^3.0.1",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-hot-loader": "^3.0.0-beta.7",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
"style-loader": "^0.18.2",
"stylelint": "^8.1.1",
"stylelint-config-standard": "^17.0.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4",
"babel-cli": "6.24.1",
"babel-preset-env": "1.6.1",
"node-sass": "4.8.3",
"sass-loader": "6.0.7"
},
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
"build": "webpack --mode production",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"webpack": "^4.14.0"
}
}
TaglocationList.js
import React from 'react';
import { connect } from 'react-redux';
import Taglocation from './Taglocation';
const TaglocationList = (props) => (
<div>
Taglocation List:
<ul>
{props.taglocations.map(taglocation => {
return (
<li key={taglocation.id}>
<Taglocation {...taglocation} />
</li>
);
})}
</ul>
</div>
);
const mapStateToProps = (state) => {
return {
taglocations: state
};
}
export default connect(mapStateToProps)(TaglocationList);
./App/index.js
import React from 'react'
import ReactDOM from 'react-dom';
import AppRouter from '../routers/AppRouter';
import getAppStore from '../store/store';
import { getTaglocations } from '../actions/taglocations';
import { Provider } from 'react-redux';
const store = getAppStore();
const template = (
<Provider store={store}>
<AppRouter />
</Provider>
);
store.dispatch(getTaglocations()).then(() => {
ReactDOM.render(template, document.getElementById('app'));
});
export default App

NPM Self Published Component cannot be found when using it in a React Project

I created my own React component and published it for use.
I just finished minifying for distribution and routing that through webpack.
(Babel used to just transpile and copy all files to dist).
however I now can't seem to import it for my projects anymore.
I install like:
npm i react-subreddit-posts
and then import like:
import SubredditPosts from 'react-subreddit-posts';
And then get this error:
Module not found: Can't resolve 'react-subreddit-posts'
so I must be exporting the module wrong or minifying it wrong???
Here is the source code:
import React, { Component } from 'react';
import ListContainer from './ListContainer';
import ListItemComponent from './ListItemComponent';
const redditAPI = 'https://www.reddit.com/r/';
export default class SubredditPosts extends Component {
constructor(props) {
super(props);
this.state = {
redditPosts: [],
isLoading: true
};
}
componentDidMount() {
const uri = `${redditAPI}${this.props.subreddit}.json`;
fetch(uri)
.then(data => data.json())
.then(this.handlePosts)
.catch(err => console.error(err));
}
handlePosts = (posts) => {
const apiPosts = posts.data.children.map((post, index) => {
return {
key: index,
title: post.data.title,
media: this.getMediaFromPost(post),
link: post.data.url
};
});
this.setState({
redditPosts: apiPosts,
isLoading: false
});
}
getMediaFromPost = (post) => {
const extension = post.data.url.split('.').pop();
if (post.data.hasOwnProperty('preview') && !extension.includes('gif')) {
return post.data.preview.images[0].source.url;
}
//do not use includes! because of Imgur's gifv links are not embeddable
if (extension === 'gif' || extension.includes('jpg') || extension.includes('jpeg')) {
return post.data.url;
}
//if can't load media then place placeholder
return this.props.placeholder;
}
render() {
return(
<ListContainer display={this.props.display}>
{ !this.state.isLoading && this.state.redditPosts.map(post => (
<ListItemComponent
display={this.props.display}
key={post.key}
link={post.link}
media={post.media}
title={post.title}
height={this.props.height}
width={this.props.width}
/>
))}
</ListContainer>
);
}
}
Here is what is in node_modules after install through npm:
I can export it just fine from the src, but not when it's published and distributed!
Package.json:
{
"name": "react-subreddit-posts",
"version": "1.0.12",
"description": "React component for displaying subreddit posts in different styles",
"main": "dist/main.js",
"repository": {
"type": "git",
"url": "https://github.com/stcalica/react-subreddit-posts"
},
"directories": {
"example": "example"
},
"scripts": {
"test": "jest",
"start": "webpack-dev-server --mode development",
"transpile": "rm -rf dist && webpack",
"prepublishOnly": "npm run transpile",
"compile": "webpack --config ./webpack.config.js --progress"
},
"jest": {
"setupTestFrameworkScriptFile": "./test/setupTest.js"
},
"keywords": [
"react",
"reddit"
],
"author": "Kyle Calica",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-jest": "^23.0.1",
"babel-loader": "^7.1.4",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-es2015-arrow-functions": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.11",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"html-webpack-plugin": "^3.2.0",
"jest": "^23.1.0",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-test-renderer": "^16.4.1",
"style-loader": "^0.21.0",
"uglifyjs-webpack-plugin": "^1.2.5",
"webpack": "^4.12.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
},
"dependencies": {}
}
webpack.config.js
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
// const htmlWebpackPlugin = new HtmlWebpackPlugin({
// template: path.join(__dirname, 'example/src/index.html'),
// filename: './index.html'
// });
module.exports = {
entry: path.join(__dirname, 'example/src/index.js'),
output: {
libraryTarget: 'commonjs2',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['react'],
plugins: ['transform-class-properties']
}
}
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
// plugins: [ htmlWebpackPlugin ],
resolve: {
extensions: ['.js', '.jsx']
},
devServer: {
port: 3001
}
};
In your github I saw that you have the dist path in your .gitignore file but if you see your package.json you would see that your repo is pointing to the dist/index.js file which does not exist because you added it to the .gitignore.
Try:
Remove the exclusion in your gitignore
Recompile your app and create the dist folder
Publish it again in npm
Re-download your dependency by npm/yarn install
And make sure you got the latest version of your dependency

React server run error + Redux + Webpack + Babel [TODO LIST]

I am new in React and doing a Todo List but i can't start my server because i am getting error when I try yarn run build
Error
ERROR in ./src/index.js
Module parse failed: C:\Users\PixyDigital\Desktop\Getto\src\index.js Unexpected token (8:4)
You may need an appropriate loader to handle this file type.
|
| render(
| <Provider store={store}>
| <App />
| </Provider>,
# multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/index.js
webpack: Failed to compile.
index.js
import React from "react"
import {render} from "react-dom"
import {Provider} from "react-redux"
import store from "./stores/store"
import App from "./components/app.js"
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("app")
)
webpack.config.js
module.exports = {
entry: __dirname + "/src/index.js",
output:{
filename: "bundle.js",
path: __dirname + "/dist"
},
module:{
loaders:[
{
test:/\.css$/,
loader:"style-loader$css-loader"
},
{
test:/\.jsx$/,
loader:"babel-loader",
exclude:/node_modules/
}
]
},
resolve:{
extensions:['*','.js','.jsx','.css']
},
devServer:{
contentBase: __dirname + "/dist"
}
}
package.json
{
"name": "todoApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack-dev-server --inline --hot"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.4",
"style-loader": "^0.18.2",
"webpack": "^3.1.0",
"webpack-dev-server": "^2.5.1"
},
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-hot-loader": "^1.3.1",
"react-redux": "^5.0.5",
"redux": "^3.7.1"
}
}
So how can I resolve my problem, Do you have any idea?
Thanks.
{
test:/\.jsx?$/,
loader:"babel-loader",
exclude:/node_modules/,
options: {
presets: ["react"]
}
}
UPD: This happens because babel doesn't transform JSX out of the box;
Also i'd recommend to configure babel using .baberlc file

Resources