简体   繁体   中英

Webpack Unexpected Token ';'

I'm using webpack to compile React.js code for my Electron.js app, but when compiling using webpack it gives the following error:

ERROR in ./app/javascript/configs.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/alvinzheng/Projects/BlankReact/app/javascript/configs.js: Unexpected token (4:13)

  2 | import * as path from 'path';
  3 | 
> 4 | const dataDir;
    |              ^

The configs.js file is where I store paths and urls that my app needs, and at the end of the file I export all of them using es6 syntax.

Snippet of config.js

const dataDir;
if(process.platform === "linux"){
    dataDir = path.resolve(require('os').homedir(), 'FOLDER')
}else if(process.platform === "win32"){
    dataDir = path.resolve(process.env.APPDATA, 'FOLDER')
}

export {dataDir}

Webpack Config:

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
    mode: "production",
    target: 'node',
    externals: [nodeExternals()],

    entry: './app/javascript/index.js',
    output: {
        filename: "index-bundle.js",
        path: path.join(__dirname + "/app/dist/")
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
            },

            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },

            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'fonts/',
                    publicPath: url => '../assets/fonts/' + url
                }
            }
        ],
    },
}

Babelrc:

{
    "presets": ["@babel/preset-env", "@babel/react"]
}

Constants should be initialized. Because you cannot reassign values later.

See this doc: SyntaxError: missing = in const declaration .

Should be

let dataDir;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM