Webpack 4 and Babel 7 “import” Uncaught SyntaxError: Unexpected identifier in React
I've been trying to set up Webpack 4 and Babel 7 to work with ES6 "import" in React. I keep getting "Uncaught SyntaxError: Unexpected identifier" at "import React from 'react';" in Chrome 71. It worked in Webpack 3 and Babel 6, so I think I'm doing something wrong with the Babel setup.
These are my dev dependencies:
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.4",
"webpack": "^4.26.0",
"webpack-dev-server": "^3.1.10"
My .babelrc looks like this:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }],
["@babel/syntax-dynamic-import"]
]
}
I've been trying babel.config.js as well, to no prevail.
My webpack-config.js file:
const HTMLWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/src/index.html',
filename: 'index.html',
injext: 'body'
})
module.exports = {
entry: ["babel-polyfill", __dirname + '/src/index.js'],
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
},
]
},
output: {
filename: 'transformed.js',
path: __dirname + 'build'
},
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
plugins: [
HTMLWebpackPluginConfig,
new ExtractTextPlugin('public/css/style.css', {
allChunks: true
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
}),
]}
reactjs webpack ecmascript-6 babel
|
show 2 more comments
I've been trying to set up Webpack 4 and Babel 7 to work with ES6 "import" in React. I keep getting "Uncaught SyntaxError: Unexpected identifier" at "import React from 'react';" in Chrome 71. It worked in Webpack 3 and Babel 6, so I think I'm doing something wrong with the Babel setup.
These are my dev dependencies:
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.4",
"webpack": "^4.26.0",
"webpack-dev-server": "^3.1.10"
My .babelrc looks like this:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }],
["@babel/syntax-dynamic-import"]
]
}
I've been trying babel.config.js as well, to no prevail.
My webpack-config.js file:
const HTMLWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/src/index.html',
filename: 'index.html',
injext: 'body'
})
module.exports = {
entry: ["babel-polyfill", __dirname + '/src/index.js'],
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
},
]
},
output: {
filename: 'transformed.js',
path: __dirname + 'build'
},
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
plugins: [
HTMLWebpackPluginConfig,
new ExtractTextPlugin('public/css/style.css', {
allChunks: true
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
}),
]}
reactjs webpack ecmascript-6 babel
Is Webpack actually bundling anything? Can you show the compiled script that is getting read by the browser?
– Sammy Taylor
Nov 20 at 14:30
I think I stumbled upon same problem last year, I downgraded my webpack to"webpack": "3.10.0",
. Lets see if that helps. Also my.babelrc
use"presets": ["react"]
. Im not sure aboutbabel/preset-react
. Snip your.webpack-config.js
file too, possibly missing something?
– Nikko Khresna
Nov 20 at 14:31
@SammyTaylor Thanks! I see there is no bundle, so that probably explains a lot. I will add my webpack config as an edit above. Please let me know if you need more info! Thanks
– Loerpert
Nov 20 at 14:46
@NikkoKhresna Yeah, the babel/preset-react is babel v4 syntax. :) I really prefer not to go back to Webpack 3. I have added my webpack config as an edit above.
– Loerpert
Nov 20 at 14:46
im sure this is not webpack4 syntaxinjext: 'body'
? Although im not sure if thats where the problem is..lemme check again
– Nikko Khresna
Nov 20 at 15:01
|
show 2 more comments
I've been trying to set up Webpack 4 and Babel 7 to work with ES6 "import" in React. I keep getting "Uncaught SyntaxError: Unexpected identifier" at "import React from 'react';" in Chrome 71. It worked in Webpack 3 and Babel 6, so I think I'm doing something wrong with the Babel setup.
These are my dev dependencies:
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.4",
"webpack": "^4.26.0",
"webpack-dev-server": "^3.1.10"
My .babelrc looks like this:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }],
["@babel/syntax-dynamic-import"]
]
}
I've been trying babel.config.js as well, to no prevail.
My webpack-config.js file:
const HTMLWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/src/index.html',
filename: 'index.html',
injext: 'body'
})
module.exports = {
entry: ["babel-polyfill", __dirname + '/src/index.js'],
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
},
]
},
output: {
filename: 'transformed.js',
path: __dirname + 'build'
},
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
plugins: [
HTMLWebpackPluginConfig,
new ExtractTextPlugin('public/css/style.css', {
allChunks: true
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
}),
]}
reactjs webpack ecmascript-6 babel
I've been trying to set up Webpack 4 and Babel 7 to work with ES6 "import" in React. I keep getting "Uncaught SyntaxError: Unexpected identifier" at "import React from 'react';" in Chrome 71. It worked in Webpack 3 and Babel 6, so I think I'm doing something wrong with the Babel setup.
These are my dev dependencies:
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.4",
"webpack": "^4.26.0",
"webpack-dev-server": "^3.1.10"
My .babelrc looks like this:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }],
["@babel/syntax-dynamic-import"]
]
}
I've been trying babel.config.js as well, to no prevail.
My webpack-config.js file:
const HTMLWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/src/index.html',
filename: 'index.html',
injext: 'body'
})
module.exports = {
entry: ["babel-polyfill", __dirname + '/src/index.js'],
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
},
]
},
output: {
filename: 'transformed.js',
path: __dirname + 'build'
},
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
plugins: [
HTMLWebpackPluginConfig,
new ExtractTextPlugin('public/css/style.css', {
allChunks: true
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
}),
]}
reactjs webpack ecmascript-6 babel
reactjs webpack ecmascript-6 babel
edited Nov 20 at 17:57
halfer
14.3k758109
14.3k758109
asked Nov 20 at 14:14
Loerpert
12
12
Is Webpack actually bundling anything? Can you show the compiled script that is getting read by the browser?
– Sammy Taylor
Nov 20 at 14:30
I think I stumbled upon same problem last year, I downgraded my webpack to"webpack": "3.10.0",
. Lets see if that helps. Also my.babelrc
use"presets": ["react"]
. Im not sure aboutbabel/preset-react
. Snip your.webpack-config.js
file too, possibly missing something?
– Nikko Khresna
Nov 20 at 14:31
@SammyTaylor Thanks! I see there is no bundle, so that probably explains a lot. I will add my webpack config as an edit above. Please let me know if you need more info! Thanks
– Loerpert
Nov 20 at 14:46
@NikkoKhresna Yeah, the babel/preset-react is babel v4 syntax. :) I really prefer not to go back to Webpack 3. I have added my webpack config as an edit above.
– Loerpert
Nov 20 at 14:46
im sure this is not webpack4 syntaxinjext: 'body'
? Although im not sure if thats where the problem is..lemme check again
– Nikko Khresna
Nov 20 at 15:01
|
show 2 more comments
Is Webpack actually bundling anything? Can you show the compiled script that is getting read by the browser?
– Sammy Taylor
Nov 20 at 14:30
I think I stumbled upon same problem last year, I downgraded my webpack to"webpack": "3.10.0",
. Lets see if that helps. Also my.babelrc
use"presets": ["react"]
. Im not sure aboutbabel/preset-react
. Snip your.webpack-config.js
file too, possibly missing something?
– Nikko Khresna
Nov 20 at 14:31
@SammyTaylor Thanks! I see there is no bundle, so that probably explains a lot. I will add my webpack config as an edit above. Please let me know if you need more info! Thanks
– Loerpert
Nov 20 at 14:46
@NikkoKhresna Yeah, the babel/preset-react is babel v4 syntax. :) I really prefer not to go back to Webpack 3. I have added my webpack config as an edit above.
– Loerpert
Nov 20 at 14:46
im sure this is not webpack4 syntaxinjext: 'body'
? Although im not sure if thats where the problem is..lemme check again
– Nikko Khresna
Nov 20 at 15:01
Is Webpack actually bundling anything? Can you show the compiled script that is getting read by the browser?
– Sammy Taylor
Nov 20 at 14:30
Is Webpack actually bundling anything? Can you show the compiled script that is getting read by the browser?
– Sammy Taylor
Nov 20 at 14:30
I think I stumbled upon same problem last year, I downgraded my webpack to
"webpack": "3.10.0",
. Lets see if that helps. Also my .babelrc
use "presets": ["react"]
. Im not sure about babel/preset-react
. Snip your .webpack-config.js
file too, possibly missing something?– Nikko Khresna
Nov 20 at 14:31
I think I stumbled upon same problem last year, I downgraded my webpack to
"webpack": "3.10.0",
. Lets see if that helps. Also my .babelrc
use "presets": ["react"]
. Im not sure about babel/preset-react
. Snip your .webpack-config.js
file too, possibly missing something?– Nikko Khresna
Nov 20 at 14:31
@SammyTaylor Thanks! I see there is no bundle, so that probably explains a lot. I will add my webpack config as an edit above. Please let me know if you need more info! Thanks
– Loerpert
Nov 20 at 14:46
@SammyTaylor Thanks! I see there is no bundle, so that probably explains a lot. I will add my webpack config as an edit above. Please let me know if you need more info! Thanks
– Loerpert
Nov 20 at 14:46
@NikkoKhresna Yeah, the babel/preset-react is babel v4 syntax. :) I really prefer not to go back to Webpack 3. I have added my webpack config as an edit above.
– Loerpert
Nov 20 at 14:46
@NikkoKhresna Yeah, the babel/preset-react is babel v4 syntax. :) I really prefer not to go back to Webpack 3. I have added my webpack config as an edit above.
– Loerpert
Nov 20 at 14:46
im sure this is not webpack4 syntax
injext: 'body'
? Although im not sure if thats where the problem is..lemme check again– Nikko Khresna
Nov 20 at 15:01
im sure this is not webpack4 syntax
injext: 'body'
? Although im not sure if thats where the problem is..lemme check again– Nikko Khresna
Nov 20 at 15:01
|
show 2 more comments
1 Answer
1
active
oldest
votes
Okay, i seemed to have fixed to issue after spitting through my webpack config. The issue was caused by this:
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
I'll see if i can find what exactly causes Webpack not to bundle with this. Thanks for all the help anyway!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394953%2fwebpack-4-and-babel-7-import-uncaught-syntaxerror-unexpected-identifier-in-re%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Okay, i seemed to have fixed to issue after spitting through my webpack config. The issue was caused by this:
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
I'll see if i can find what exactly causes Webpack not to bundle with this. Thanks for all the help anyway!
add a comment |
Okay, i seemed to have fixed to issue after spitting through my webpack config. The issue was caused by this:
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
I'll see if i can find what exactly causes Webpack not to bundle with this. Thanks for all the help anyway!
add a comment |
Okay, i seemed to have fixed to issue after spitting through my webpack config. The issue was caused by this:
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
I'll see if i can find what exactly causes Webpack not to bundle with this. Thanks for all the help anyway!
Okay, i seemed to have fixed to issue after spitting through my webpack config. The issue was caused by this:
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
I'll see if i can find what exactly causes Webpack not to bundle with this. Thanks for all the help anyway!
answered Nov 20 at 15:38
Loerpert
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394953%2fwebpack-4-and-babel-7-import-uncaught-syntaxerror-unexpected-identifier-in-re%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is Webpack actually bundling anything? Can you show the compiled script that is getting read by the browser?
– Sammy Taylor
Nov 20 at 14:30
I think I stumbled upon same problem last year, I downgraded my webpack to
"webpack": "3.10.0",
. Lets see if that helps. Also my.babelrc
use"presets": ["react"]
. Im not sure aboutbabel/preset-react
. Snip your.webpack-config.js
file too, possibly missing something?– Nikko Khresna
Nov 20 at 14:31
@SammyTaylor Thanks! I see there is no bundle, so that probably explains a lot. I will add my webpack config as an edit above. Please let me know if you need more info! Thanks
– Loerpert
Nov 20 at 14:46
@NikkoKhresna Yeah, the babel/preset-react is babel v4 syntax. :) I really prefer not to go back to Webpack 3. I have added my webpack config as an edit above.
– Loerpert
Nov 20 at 14:46
im sure this is not webpack4 syntax
injext: 'body'
? Although im not sure if thats where the problem is..lemme check again– Nikko Khresna
Nov 20 at 15:01