简体   繁体   中英

Why am I getting this error: Module build failed (from ./node_modules/babel-loader/lib/index.js): Syntax Error Unexpected token, expected ","

I have this code in javascript/packs/style.js I required it in application.js using `require("packs/style"). but I get this error in my console when I refresh the page.

Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/maryan/Desktop/workspace/collabfield/app/javascript/posts/style.js: Unexpected token, expected "," (20:0)

  18 |
  19 |
> 20 | var colorSet = randomColorSet();
     | ^
  21 | var mode = Math.floor(Math.random() * 2);
  22 |
  23 | function randomColorSet() {
    at Parser._raise (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:776)
    at Parser.raiseWithData (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:769)
    at Parser.raise (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:737)
    at Parser.unexpected (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:9735)
    at Parser.expect (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:9721)
    at Parser.parseCallExpressionArguments (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10890)
    at Parser.parseCoverCallAndAsyncArrowHead (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10816)
    at Parser.parseSubscript (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10752)
    at Parser.parseSubscripts (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10725)
    at Parser.parseExprSubscripts (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10714)
    at Parser.parseUpdate (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10688)
    at Parser.parseMaybeUnary (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10666)
    at Parser.parseExprOps (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10523)
    at Parser.parseMaybeConditional (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10497)
    at Parser.parseMaybeAssign (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10460)
    at Parser.parseExpressionBase (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10405)
    at :3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10399
    at Parser.allowInAnd (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12098)
    at Parser.parseExpression (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:10399)
    at Parser.parseStatementContent (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12390)
    at Parser.parseStatement (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12259)
    at Parser.parseBlockOrModuleBlockBody (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12845)
    at Parser.parseBlockBody (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12836)
    at Parser.parseProgram (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12190)
    at Parser.parseTopLevel (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:12181)
    at Parser.parse (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:13892)
    at parse (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/parser/lib/index.js:13944)
    at parser (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/core/lib/parser/index.js:54)
    at parser.next (<anonymous>)
    at normalizeFile (:3000/home/maryan/Desktop/workspace/collabfield/node_modules/@babel/core/lib/transformation/normalize-file.js:55)
    at Object../app/javascript/posts/style.js (application.js:16)
    at __webpack_require__ (bootstrap:19)
    at Module../app/javascript/packs/application.js (application.js:1)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83
./app/javascript/posts/style.js @ application.js:16
__webpack_require__ @ bootstrap:19
./app/javascript/packs/application.js @ application.js:1
__webpack_require__ @ bootstrap:19
(anonymous) @ bootstrap:83
(anonymous) @ bootstrap:83

This is the code in style.js

$(document).on('turbolinks:load', function() {
    if ($(".single-post-card").length) {
        // set a solid background color style
        if (mode == 1) {
            $(".single-post-card").each(function() {
                $(this).addClass("solid-color-mode");
                $(this).css('background-color', randomColor());
            });
        }
        // set a border color style
        else {
            $(".single-post-card").each(function() {
                $(this).addClass("border-color-mode");
                $(this).css('border', '5px solid ' + randomColor());
            });
        }   
    }


    $('#feed').on( 'mouseenter', '.single-post-list', function() {
        $(this).css('border-color', randomColor()); 
    });

    $('#feed').on( 'mouseleave', '.single-post-list', function() {
        $(this).css('border-color', 'rgba(0, 0 , 0, 0.05)');    
    });

});

var colorSet = randomColorSet();
var mode = Math.floor(Math.random() * 2);

// Randomly returns a color scheme
function randomColorSet() {
    var colorSet1 = ['#45CCFF', '#49E83E', '#FFD432', '#E84B30', '#B243FF'];
    var colorSet2 = ['#FF6138', '#FFFF9D', '#BEEB9F', '#79BD8F', '#79BD8F'];
    var colorSet3 = ['#FCFFF5', '#D1DBBD', '#91AA9D', '#3E606F', '#193441'];
    var colorSet4 = ['#004358', '#1F8A70', '#BEDB39', '#FFE11A', '#FD7400'];
    var colorSet5 = ['#105B63', '#FFFAD5', '#FFD34E', '#DB9E36', '#BD4932'];
    var colorSet6 = ['#04BFBF', '#CAFCD8', '#F7E967', '#A9CF54', '#588F27'];
    var colorSet7 = ['#405952', '#9C9B7A', '#FFD393', '#FF974F', '#F54F29'];
    var randomSet = [colorSet1, colorSet2, colorSet3, colorSet4, colorSet5, colorSet6, colorSet7];
    return randomSet[Math.floor(Math.random() * randomSet.length )];
}

// Randomly returns a color from an array of colors
function randomColor() {
    var color = colorSet[Math.floor(Math.random() * colorSet.length)];
    return color;
}

I am building it on Rails 6.0.3.2

I don't know if my code is wrong or if there's something else.

Any help or thoughts are highly appreciated...

This./node_modules/babel-loader/lib/index.js) Module build error sure is randomly appearing in Webpacker in rails 6. from my understanding the whole webpacker setup in rails 6 is pretty fragile. to fix this I did a clean reinstall as such:

yarn remove @rails/webpacker
yarn remove webpack webpack-cli webpack-dev-server
rm -fr node_modules
rm -fr public/packs
bundle exec rails webpacker:install
# REMOVE any invalid babel preset from package.json added by "webpacker:install"
yarn add node-releases
bundle exec rails webpacker:compile

add style.js to application.js using require("packs/style")

#Success

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