main.jsx
import React from 'react';
import ReactDOM from 'react-dom';
const src = require('../images/av.png');
require('../css/styles.css');
class APP extends React.Component{
render(){
//Render main component
return (
<div >
Hello!
<img src={src} />
</div>
);
}
}
ReactDOM.render(
//React routing
(<APP />),
document.getElementById('app'));
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
context: __dirname,
entry: {
javascript: './static/jsx/main.jsx'
},
output: {
path: path.resolve('./static/js/app/'),
filename: 'bundle.js'
},
module: {
preLoaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'source-map'
}
],
loaders: [
{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'stage-0', 'react']
}
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{
test: /\.(jpg|png)$/,
loader: 'url-loader?limit=25000',
}
]
},
};
package.json
{
"name": "",
"version": "0.0.1",
"description": "",
"license": "",
"repository": {
"type": "git"
},
"scripts": {
"gulp": "gulp",
"webpack": "webpack",
"start-webpack-server": "webpack-dev-server --hot --inline --colors --progress"
},
"dependencies": {
"react": "^15.3.2",
"react-dom": "^15.3.2"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.16.0",
"babel-core": "^6.6.5",
"babel-loader": "^6.2.4",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-3": "^6.11.0",
"babelify": "^7.3.0",
"css-loader": "^0.23.1",
"es6-promise": "^4.0.5",
"eslint": "^3.10.0",
"eslint-plugin-react": "^6.6.0",
"file-loader": "^0.8.5",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-concat": "^2.6.0",
"gulp-html-replace": "^1.6.1",
"gulp-react": "^3.1.0",
"gulp-uglify": "^2.0.0",
"lodash": "^4.16.4",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.16.2",
"webpack-stream": "^3.2.0",
"source-map-loader": "^0.1.5"
}
}
As you can see I'm trying to load image and css files into my jsx . I had problems with images but now they get loaded fine. Anyway css files are interpreted as jsx by webpack although I set the css-loader for it. When I run transpiling for the project I get this error:
ERROR in ./static/css/styles.css
Module parse failed: D:\work\simpleLeafLet\static\css\styles.css Unexpected token (2:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:0)
The reason is a simple typo
...
loaders: [
{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'stage-0', 'react']
}
},
...
It should be
...
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'stage-0', 'react']
}
},
...
I missed \ in "test: /\.jsx?$/"
Related
I use React+Webpack. I got the error, You may need an appropriate loader to handle this file type. After it, SyntaxError: Unexpected token at ../../acorn.js.
Why is it related to acorn.js? Also, I don't how to solve it. I think I already installed loaders that I needed. webpack.config.dev.js is working well, but when trying building webpack.config.prod.js I got this error.
Let me show my codes...
webpack.config.prod.js
module: {
loaders: [
{
test: /\.jsx$/,
loader: 'babel',
exclude: /node_modules/,
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
{
test: /\.css$/,
loaders: ['style', 'css']
},
{
test: /\.png$/,
loader: "url-loader?limit=100000"
},
{
test: /\.jpg$/,
loader: "file-loader"
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=image/svg+xml'
},
{
test: /\.gif$/i,
loaders: [
'file?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
]
},
{
test: /\.json?$/,
loader: 'json'
}
]
},
resolve: {
root: [
path.resolve('./src'),
path.resolve('./style')
],
extensions: ['', '.js', '.jsx', '.css']
},
plugins: [
new HtmlWebpackPlugin({
tempalte: './public/index.html'
}),
new webpack.DefinePlugin({
'process.env':{
'NODE_ENV': JSON.stringify('production')
}
}),
],
package.json
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"babel-preset-stage-0": "^6.22.0",
"css-loader": "^0.26.1",
"file-loader": "^0.10.0",
"html-webpack-plugin": "^2.28.0",
"image-webpack-loader": "^3.2.0",
"json-loader": "^0.5.4",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.15.3",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"lodash": "^4.17.4",
"path": "^0.12.7",
"react": "^15.4.2",
"react-bootstrap": "^0.30.7",
"react-bootstrap-table": "^3.1.7",
"react-dnd": "^2.2.3",
"react-dnd-html5-backend": "^2.2.3",
"react-dom": "^15.4.2",
"react-js-pagination": "^2.0.2",
"react-modal": "^1.7.3",
"react-redux": "^5.0.2",
"react-router": "^3.0.2",
"react-slick": "0.14.5",
"react-toastr": "^2.8.2",
"redux": "^3.6.0",
"redux-form": "^6.5.0",
"redux-promise": "^0.5.3",
"redux-thunk": "^2.2.0",
"slick-carousel": "^1.6.0"
}
Your module loader only transpile the .jsx files, change the test expression to transpile the .js files as well
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/,
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
I get the following error when running:
webpack watch
ERROR in multi ./main.js watch
Module not found: Error: Can't resolve 'watch' in '/Users/name/directory/src'
# multi ./main.js watch
Heres my webpack.config.js file
const path = require('path');
module.exports = {
context: path.join(__dirname, 'src'),
entry: [
'./main.js',
],
output: {
path: path.join(__dirname, 'www'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
include: [
path.resolve(__dirname, "src")
],
exclude: /node_modules/,
use: [
'babel-loader',
],
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
}, {
loader: "css-loader" // translates CSS into CommonJS
}, {
loader: "sass-loader" // compiles Sass to CSS
}]
}
],
},
resolve: {
modules: [
path.join(__dirname, 'node_modules'),
],
},
};
Here is my package.json
{
"name": "name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2",
"webpack-dev-middleware": "^1.10.1"
},
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.0",
"babel-preset-latest": "^6.24.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-2": "^6.22.0",
"css-loader": "^0.27.3",
"express": "^4.15.2",
"node-sass": "^4.5.0",
"react-helmet": "^4.0.0",
"react-hot-loader": "^1.3.1",
"sass-loader": "^6.0.3",
"style-loader": "^0.14.1",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-dev-server": "^2.4.2"
}
}
Below is my main.js file
import "./styles/stylesheet.scss";
import { Header } from "./components/headercomponent";
import React from "react";
import ReactDOM from "react-dom";
import { render } from "react-dom";
class App extends React.Component {
render(){
return (
<div id="container">
<Header/>
<h1>hello world</h1>
</div>
);
}
}
render(<App/>, window.document.getElementById("app"));
Do you know why I'm getting the webpack error?
Arguments you pass to the webpack CLI will be treated as entry points. You want to use the --watch option, not add watch as an entry. The correct command is:
webpack --watch
Installed Rodal modals for React but when i add
import 'rodal/lib/rodal.css';
i get an error -> "ERROR in ./~/rodal/lib/rodal.css
Module parse failed"
Looking around it appears to be a babel.config.js issues but even when copying some of other peoples answers, the problem persists.
Below is the babel.config.js file
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loaders: 'babel', query: { presets: ['react', 'es2015', 'stage-1'] } },
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
Add package.json
{
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"repository": "",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.3.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"css-loader": "^0.26.2",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"npm": "^4.3.0",
"react-addons-test-utils": "^0.14.7",
"style-loader": "^0.13.2",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.15.3",
"babel-preset-stage-1": "^6.1.18",
"classnames": "^2.2.5",
"flexboxgrid": "^6.3.1",
"lodash": "^3.10.1",
"material-design-icons": "^3.0.1",
"material-ui": "^0.17.0",
"next": "^2.0.0-beta",
"react": "^15.4.2",
"react-carousel": "^3.3.0",
"react-dom": "^15.4.2",
"react-flexbox-grid": "^0.10.2",
"react-instantsearch": "^3.2.1",
"react-instantsearch-theme-algolia": "^3.2.1",
"react-redux": "^4.0.0",
"react-router": "^3.0.2",
"react-slick": "https://github.com/johntron/react-slick/archive/0.14.6-patch.tar.gz",
"react-tap-event-plugin": "^2.0.1",
"redux": "^3.0.4",
"redux-promise": "^0.5.3",
"rodal": "^1.4.0"
}
}
The component calling it is
import React, { Component } from 'react';
import Rodal from 'rodal';
import 'rodal/lib/rodal.css';
export default class ModalButton extends Component {
render () {
return (
<div>
<h2>Hello</h2>
</div>
);
}
}
EDIT: Working Version for anyone else who needs this.
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
},
resolve: {
extensions: ['', '.js', '.jsx', '.css']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
Your error means that webpack does not know how to parse css files.
To resolve this problem you need to npm install --save-dev style-loader css-loader and in your webpack file include those loaders as follows
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loaders: 'babel', query: { presets: ['react', 'es2015', 'stage-1'] } },
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
and include .css extension
resolve: {
extensions: ['', '.js', '.jsx', '.css']
}
I just restarted my computer and now when I try to run my React project locally I get this error. Before restarting I was able to work on my project just fine. I tried removing my node_modules folder and npm installing again, still not working. Anybody have any ideas what my problem could be? Here is my package.json and webpack.config:
package.json:
{
"version": "1.0.0",
"description": "== README",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Stefan",
"license": "ISC",
"dependencies": {
"babel-core": "6.18.2",
"babel-loader": "6.2.7",
"babel-preset-es2015": "6.18.0",
"babel-preset-react": "6.16.0",
"css-loader": "0.26.1",
"file-loader": "0.9.0",
"less": "2.3.1",
"less-loader": "2.2.3",
"react": "15.4.1",
"react-addons-test-utils": "15.4.1",
"react-dom": "15.3.2",
"react-fontawesome": "1.5.0",
"react-redux": "4.4.5",
"react-router": "3.0.0",
"redux": "3.6.0",
"style-loader": "0.13.1",
"url-loader": "0.5.7",
"webpack": "1.13.3"
}
}
webpack.config:
module.exports = {
context: __dirname,
entry: "./frontend/app.jsx",
output: {
path: "app/assets/javascripts",
filename: "bundle.js",
publicPath: "/assets/"
},
resolve: {
extensions: ["", ".js", ".jsx"]
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
},{
test: /\.node$/,
loader: "node-loader"
},{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/octet-stream"
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/svg+xml"
}
]
},
devtool: 'source-map',
};
It's my first time with Webpack and I don't undersand why my sass file (.scss) is not loaded. I spend all the afternoon trying and searching, but I don't understand what is wrong in my code.
webpack.config.js
module.exports = {
entry: "./main.js",
output: {path: __dirname, filename: "bundle.js"},
module: {
loaders: [
{
test: /.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/,
query: {
presets: ["es2015", "react"]
}
},
{
test: /\.css$/,
loader: "css-loader!autoprefixer-loader"
},
{
test: /\.scss$/,
loader: "css-loader!sass-loader"
}
]
}
};
package.json dependencies:
//...
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.4.0",
"redux": "^3.3.1",
"webpack-dev-server": "^1.14.1"
},
"devDependencies": {
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.6.5",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"node-sass": "^3.4.2",
"redux-devtools": "^3.1.1",
"sass-loader": "^3.1.2",
"webpack": "^1.12.14"
}
//...
And in my app.js I added this line:
import "./app/sass/app.scss";
There aren't errors in the terminal and neither in the Chrome console. But the SASS file is not loading.
What is wrong in my code?
Thanks!
You have to install style-loader, css-loader and sass-loader as well.
To do so, run:
npm i css-loader style-loader sass-loader --save-dev
Then, add loaders to your webpack configuration.
// ...
{
test: /\.scss$/,
loader: 'style!css!sass'
}]
// ...
You were on good path, though