Webpack 4 and Babel 7 “import” Uncaught SyntaxError: Unexpected identifier in React












0














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')
}),
]}









share|improve this question
























  • 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










  • @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


















0














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')
}),
]}









share|improve this question
























  • 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










  • @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
















0












0








0







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')
}),
]}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • @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




















  • 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










  • @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


















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














1 Answer
1






active

oldest

votes


















0














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!






share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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!






    share|improve this answer


























      0














      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!






      share|improve this answer
























        0












        0








        0






        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!






        share|improve this answer












        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 15:38









        Loerpert

        12




        12






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin