Appropriate loader error on index.js - javascript

I'm stuck on this same error and I need fresh eyes for help.... I'm confused on the "appropriate loader". I was thinking this was from an improper regular expression in the webpack file. I checked the babel docs and this issue for guidance.
This is the error...
ERROR in ./src/index.js
Module parse failed: Unexpected token (6:16)
You may need an appropriate loader to handle this file type.
# multi (webpack)-dev-server/client?http://localhost:8080 ./src
This is my src/index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './Components/App'
ReactDOM.render(<App />, document.getElementById('root'))
.babelrc
{
"presets": ["env", "react"],
"env": {
"development": {
"plugins": [["react-transform",{
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}]
}]]
}
}
}
webpack.dev.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build'),
publicPath: '/',
},
module: {
rules: [
{ test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
include: [path.resolve(__dirname, "src")]
}
],
},
devServer: {
historyApiFallback: true,
inline: true,
hot: true,
},
plugins: [
new HtmlWebpackPlugin({template: 'src/index.html'}),
new webpack.HotModuleReplacementPlugin()
],
}
and my package.json
{
"name": "webpack-starter-kit",
"version": "1.0.0",
"description": "starter files for basic development and production using React, Babel, Webpack",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --progress --mode=development",
"build": "webpack --mode=production"
},
"author": "Kevin Turney",
"license": "ISC",
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.2.2"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-plugin-react-transform": "^3.0.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.11",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.11",
"html-webpack-plugin": "^3.0.7",
"postcss-loader": "^2.1.2",
"react-transform-hmr": "^1.0.4",
"style-loader": "^0.20.3",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "^4.6.0",
"webpack-cli": "^2.0.15",
"webpack-dev-server": "^3.1.1"
}
}

Related

Cannot use CSS in Reactjs SSR

Hi there I am trying to apply custom CSS to my react component in React SSR project
however, it causes an error:
ERROR in ./node_modules/css-loader/dist/runtime/api.js
Module build failed: Error: Couldn't find preset "#babel/preset-env" relative to directory "R:\\Webdev\\AlmaJ\\server\\node_modules\\css-loader"
at R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\options\option-manager.js:293:19
at Array.map (<anonymous>)
at OptionManager.resolvePresets (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\options\option-manager.js:275:20)
at OptionManager.mergePresets (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\options\option-manager.js:264:10)
at OptionManager.mergeOptions (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\options\option-manager.js:249:14)
at OptionManager.init (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\options\option-manager.js:368:12)
at File.initOptions (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\index.js:212:65)
at new File (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\file\index.js:135:24)
at Pipeline.transform (R:\Webdev\AlmaJ\server\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
at transpile (R:\Webdev\AlmaJ\server\node_modules\babel-loader\lib\index.js:50:20)
at Object.module.exports (R:\Webdev\AlmaJ\server\node_modules\babel-loader\lib\index.js:175:20)
# ./node_modules/css-loader/dist/cjs.js!./src/style.css 1:27-84
# ./src/style.css
# ./src/index.js
when I try to import a CSS file in a js file.
my package.json file is here:
"version": "1.0.0",
"description": "AlmaJ ssr project",
"main": "index.js",
"scripts": {
"dev": "npm-run-all --parallel dev:*",
"dev:server": "nodemon --watch build --exec node build/bundle.js",
"dev:build-server": "webpack --config webpack.server.js --watch",
"dev:build-client": "webpack --config webpack.client.js --watch"
},
"author": "",
"dependencies": {
"axios": "0.16.2",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
"babel-loader": "7.1.2",
"babel-preset-env": "1.6.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-es2017": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "6.24.1",
"compression": "1.7.0",
"concurrently": "3.5.0",
"css-loader": "2.1.0",
"express": "4.15.4",
"express-http-proxy": "1.0.6",
"lodash": "4.17.4",
"nodemon": "1.12.0",
"npm-run-all": "4.1.1",
"react": "16.3.0",
"react-bootstrap": "^1.4.3",
"react-dom": "16.0.0",
"react-helmet": "5.2.0",
"react-redux": "5.0.6",
"react-router-config": "1.0.0-beta.4",
"react-router-dom": "4.2.2",
"redux": "3.7.2",
"redux-thunk": "2.2.0",
"serialize-javascript": "1.4.0",
"style-loader": "0.23.1",
"styled-components": "^5.2.1",
"webpack": "3.5.6",
"webpack-dev-server": "2.8.2",
"webpack-merge": "4.1.0",
"webpack-node-externals": "1.6.0"
},
here are my webpack files
webpack.server.js:
const path = require("path");
const merge = require("webpack-merge");
const baseConfig = require("./webpack.base.js");
const webpackNodeExternals = require("webpack-node-externals");
const config = {
target: "node",
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "build"),
},
externals: [webpackNodeExternals()],
};
module.exports = merge(config, baseConfig);
webpack.client.js:
const path = require("path");
const merge = require("webpack-merge");
const baseConfig = require("./webpack.base.js");
const config = {
entry: "./src/client/client.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "public"),
},
};
module.exports = merge(config, baseConfig);
webpack.base.js:
module.exports = {
module: {
rules: [
{
test: /\.js?$/,
loader: "babel-loader",
exclude: "/node_modules/",
options: {
presets: [
"react",
"stage-0",
["env", { targets: { browsers: ["last 2 versions"] } }],
],
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
};
I also tried to use styled-components
import styled from "styled-components";
const Some = styled.header`
h1 {
color: red;
}
`;
<Some>
<h1>hi</h1>
</Some>
The server runs before it gets a request from a browser but when the request comes in:
TypeError: element.type.toLowerCase is not a function
at ReactDOMServerRenderer.renderDOM (R:\Webdev\AlmaJ\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:2776:28)
at ReactDOMServerRenderer.render (R:\Webdev\AlmaJ\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:2755:23)
at ReactDOMServerRenderer.read (R:\Webdev\AlmaJ\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:2722:19)
at renderToString (R:\Webdev\AlmaJ\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:2980:25)
at exports.default (R:\Webdev\AlmaJ\server\build\bundle.js:396:44)
at R:\Webdev\AlmaJ\server\build\bundle.js:329:42
at process._tickCallback (internal/process/next_tick.js:68:7)
[BABEL] Note: The code generator has deoptimised the styling of "R:/Webdev/AlmaJ/server/node_modules/react-dom/cjs/react-dom.development.js" as it exceeds the max of "500KB".
I've been spending so much time solving this problem,
I would really appreciate any help

Webpack throws error when trying to build JSX

There are so many answers to this question on SO, I swear i've tried them all. I am not sure what is wrong with config setup
The Error I get when running webpack in the terminal:
ERROR in ./src/index.js 13:8
Module parse failed: Unexpected token (13:8)
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
| // const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
| return (
> <HomeShell/>
| );
| };
my index.js
import "#babel/polyfill";
import React from 'react';
import ReactDOM from 'react-dom';
import HomeShell from "./HomeShell";
const Root = () => {
return (
<HomeShell/>
);
};
ReactDOM.render(<Root />, document.getElementById('#main'));
^^ HomeShell is js file with React Components that are all js files.
My webpack.config.cjs
import path from 'path';
module.exports = {
entry: ["#babel/polyfill", "./src/index.js"],
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js'
},
target: "web",
mode: 'production',
module: {
rules: [
{
test: /\.m?js$/,
include: /src/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ["#babel/preset-env", "#babel/preset-react"]
}
}
},
{
test: /\.css$/,
include: /node_modules/,
loader: ['style-loader', 'css-loader'],
}
]
},
};
my .babelrc
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
my package.json
{
"name": "site name here",
"version": "1.0.0",
"description": "site desc",
"main": "index.js",
"type": "module",
"repository": {
"type": "git",
"url": "git url"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "issues url"
},
"homepage": "readme url",
"dependencies": {
"#babel/polyfill": "^7.10.1",
"#types/express": "^4.17.6",
"axios": "^0.19.2",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"nodemailer": "^6.4.5",
"path": "^0.12.7",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-redux": "^7.2.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"redux": "^4.0.5"
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.9.0",
"#babel/preset-env": "^7.9.0",
"#babel/preset-react": "^7.9.4",
"babel-loader": "^8.1.0",
"css-loader": "^3.5.3",
"file-loader": "^5.1.0",
"html-loader": "^1.1.0",
"html-webpack-plugin": "^4.3.0",
"image-webpack-loader": "^3.6.0",
"style-loader": "^1.2.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rm -rf dist && mkdir dist && webpack && babel server -s -d dist",
"clean": "rm -rf dist",
"production": "npm run build && node bin/production",
"start": "npm run production"
}
}
I am far from an expert on react/webpack/babel but this exact setup seems to work fine on another basic app I have running. Not sure what I am doing different between the two apps.
You mentioned that you are using a webpack.config.cjs file, which at the moment anyway, Webpack does not support by default: https://github.com/webpack/webpack-cli/issues/1165
You'd need to explicitly pass --config webpack.config.cjs option to load this file.
I see
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
in your .babelrc file and also in the loader options. Maybe there is a conflict.

how to fix warning with React and babel: Parsing error: Unexpected token <

I have this app where I have added react, babel and webpack.
Repo in github: https://github.com/GiorgioMartini/holli
Its seems to work ok, buti get this error:
WARNING in ./src/scripts/index.js
Module Warning (from ./node_modules/eslint-loader/dist/cjs.js):
/Users/giorgio/Documents/frontend-assessment-test/src/scripts/index.js
7:7 error Parsing error: Unexpected token <
✖ 1 problem (1 error, 0 warnings)
that line 7:7 is this one where the div starts after the reurn:
import React from 'react'
import ReactDOM from 'react-dom'
class App extends React.Component {
render() {
return (
<div>
Hella
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
)
So im guessing its some babel misconfiguration or something.
This is my babel config file:
const Path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
app: Path.resolve(__dirname, '../src/scripts/index.js')
},
output: {
path: Path.join(__dirname, '../build'),
filename: 'js/[name].js'
},
optimization: {
splitChunks: {
chunks: 'all',
name: false
}
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([
{ from: Path.resolve(__dirname, '../public'), to: 'public' }
]),
new HtmlWebpackPlugin({
template: Path.resolve(__dirname, '../src/index.html')
})
],
resolve: {
alias: {
'~': Path.resolve(__dirname, '../src')
}
},
module: {
rules: [
{ test: /\.(js)$/, use: ['babel-loader', 'eslint-loader'], exclude: /node_modules/},
{ test: /\.css$/, use: [ 'style-loader', 'css-loader' ]} ]
}
};
And this is the package.json:
"name": "frontend-assessment-test",
"version": "1.0.0",
"description": "A frontend assessment test for our new pirates, which are willing to come on board.",
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config webpack/webpack.config.prod.js --colors",
"start": "webpack-dev-server --open --config webpack/webpack.config.dev.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/holidaypirates/frontend-assessment-test"
},
"author": "HolidayPirates",
"license": "MIT",
"bugs": {
"url": "https://github.com/holidaypirates/frontend-assessment-test/issues"
},
"devDependencies": {
"#babel/core": "^7.7.4",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/preset-env": "^7.7.4",
"#babel/preset-react": "^7.7.4",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^5.0.4",
"cross-env": "^6.0.3",
"css-loader": "^3.2.0",
"eslint": "^6.7.1",
"eslint-loader": "^3.0.2",
"file-loader": "^4.2.0",
"html-webpack-plugin": "^4.0.0-beta.11",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "^4.13.0",
"sass-loader": "^8.0.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"#babel/polyfill": "^7.6.0",
"core-js": "^3.3.3",
"react": "^16.12.0",
"react-dom": "^16.12.0"
}
}
Any ideas where could be the issue and how to fix it?
here is the repo: https://github.com/GiorgioMartini/holli
I found the answer,
that is just a eslint error, so the app works fine is just eslint complaining.
I had to add this to the eslintrc file and now its fixed:
"extends": [
"plugin:react/recommended"
]

Require is not defined using ReactJs with babel and webpack

I'm getting this error on Chrome developer tools:
"Uncaught Reference: Require is not defined"
webpack.config
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './src/app.js',
output: {
filename: 'app.js',
path: __dirname + "public/scripts"
},
module: {
loaders: [
{
test: /.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/,
query: {
presets: ["env", "react"]
}
}
]
}
};
package.json
{
"name": "widget",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Diogo Matias",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"npm": "^5.6.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-google-maps": "^9.4.3",
"react-leaflet": "^1.7.8"
},
"devDependencies": {
"webpack": "^3.10.0"
}
}
This error comes from the import on my source javascript file where I have:
import {GoogleMap, Marker} from "react-google-maps";
I use the cdn for react and react-dom. I'm used to use live-server and I'm new to webpack. I run with webpack-dev-server on the root directory.
Hope that is enough for you to help me.
Thanks.
Here is a minimum configuration to get you started. Issue npm run start to bring up webpack-dev-server at port 9000. babel-core was missing from your configuration.
project structure
├── build
│   └── index.html
├── package.json
├── src
│   └── app.js
└── webpack.config.js
package.json
{
"name": "widget",
"version": "1.0.0",
"description": "Get started with Webpack!",
"scripts": {
"build": "webpack",
"start": "webpack-dev-server"
},
"author": "Diogo Matias",
"license": "ISC",
"dependencies": {
"leaflet": "^1.2.0",
"prop-types": "^15.5.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-google-maps": "^9.4.3",
"react-leaflet": "^1.7.8"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.9.7"
}
}
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './src/app.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build')
},
devServer: {
contentBase: path.join(__dirname, 'build'),
compress: true,
port: 9000
},
module: {
loaders: [
{
test: /.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/
}
]
}
};
.babelrc
{
"presets": [
["env", {
"targets": {
"browsers": ["last 3 Chrome versions"]
}
}]
]
}
build/index.html
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>

Unexpected token import webpack babel es2015

I have visited hundred webpages but couldn't find solution which fits me. I'm using webpack babel but have error "Unexpected token import" when i try to import express:
.babelrc:
{
"moduleId": "myModule",
"presets": ["es2015", "stage-0"],
"plugins": ["add-module-exports"]
}
webpack.config.js:
const path = require('path');
const webpack = require("webpack");
const ExtractTextPlugin=require("extract-text-webpack-plugin");
module.exports = {
context:__dirname + '/front',
entry:__dirname + '/front/index.js',
output:{
path:__dirname + '/public/assets',
filename:'bundle.js',
publicPath:'assets'
},
resolveLoader: {
root: path.join(__dirname, 'node_modules')
},
plugins:[
new webpack.DefinePlugin({
TEST_ENV:process.env.NODE_ENV === "test"
}),
new ExtractTextPlugin("bundle.css",
{
publicPath: 'assets/core/css',
allChunks: true
})
],
module: {
loaders: [
{
test: /\.js/,
loader: "babel-loader",
exclude: /(node_modules|bower_components)/,
query: {
presets: ['es2015']}
},
{
test: /\.html/,
loader: "raw-loader"},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader!autoprefixer-loader?browsers=last 3 version")
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader!autoprefixer-loader?browsers=last 3 version!sass-loader")
}
]
},
devServer: {
port: 7000,
},
devtool: 'source-map',
}
package.json:
{
"name": "SA-19KV",
"version": "1.0.0",
"description": "Project Boilerplate",
"scripts": {
"ngdev": "webpack-dev-server --content-base public/ --progress --colors ",
"nghot": "webpack-dev-server --content-base public/ --progress --colors --inline --hot",
"test": "set NODE_ENV=test && karma start",
"prod": "webpack -p",
"backdev": "nodemon server.js --watch back/"
},
"dependencies": {
"angular": "1.5.0",
"css-loader": "^0.26.1",
"express": "^4.14.1",
"style-loader": "^0.13.1"
},
"devDependencies": {
"angular-mocks": "^1.6.1",
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-0": "^6.5.0",
"chai": "^3.5.0",
"css-loader": "^0.26.1",
"extract-text-webpack-plugin": "^1.0.1",
"html-webpack-plugin": "1.6.1",
"karma": "^1.4.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-html-detailed-reporter": "^1.1.15",
"karma-mocha": "^1.3.0",
"karma-webpack": "^2.0.2",
"mocha": "^3.2.0",
"node-sass": "^4.4.0",
"nodemon": "^1.11.0",
"raw-loader": "^0.5.1",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"webpack": "^1.14.0",
"webpack-dev-server": "1.14.1"
},
"author": "Me :-)",
"license": "ISC"
}
Your package.json scripts aren't loading your webpack's config. You can load it by adding the --config parameter:
webpack-dev-server --config ./webpack.config.js

Categories