I create a grunt project and when I run the command grunt in a terminal to build the project, I encounter the error message Loading “Gruntfile.js” tasks…ERROR >> SyntaxError: Invalid or unexpected token. This article will tell you how to fix it.
1. How To Fix Loading “Gruntfile.js” tasks…ERROR >> SyntaxError: Invalid or unexpected token.
- Open a terminal and go to the grunt project saved folder.
- Run the command grunt, then it shows the below error message.
$ grunt Loading "Gruntfile.js" tasks...ERROR >> SyntaxError: Invalid or unexpected token Warning: Task "default" not found. Use --force to continue. Aborted due to warnings.
- From the above error message, we can see that this is a syntax error, but it does point out which code line throws the error.
- So we should run the command grunt –verbose in the terminal again, this time it will tell you which code line in the file Gruntfile.js throws the error.
$ grunt --verbose Initializing Command-line options: --verbose, --gruntfile=/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/js-game-engine/phaser/hello-world/Gruntfile.js Reading "Gruntfile.js" Gruntfile...OK Registering Gruntfile tasks. Loading "Gruntfile.js" tasks...ERROR >> /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/js-game-engine/phaser/hello-world/Gruntfile.js:12 >> pkg: grunt.file.readJSON(‘package.json’), >> >> >> SyntaxError: Invalid or unexpected token >> at wrapSafe (internal/modules/cjs/loader.js:979:16) >> at Module._compile (internal/modules/cjs/loader.js:1027:27) >> at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) >> at Module.load (internal/modules/cjs/loader.js:928:32) >> at Function.Module._load (internal/modules/cjs/loader.js:769:14) >> at Module.require (internal/modules/cjs/loader.js:952:19) >> at require (internal/modules/cjs/helpers.js:88:18) >> at loadTask (/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/js-game-engine/phaser/hello-world/node_modules/grunt/lib/grunt/task.js:313:10) >> at Task.task.init (/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/js-game-engine/phaser/hello-world/node_modules/grunt/lib/grunt/task.js:450:5) >> at Object.grunt.tasks (/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/js-game-engine/phaser/hello-world/node_modules/grunt/lib/grunt.js:129:8) No tasks specified, running default tasks.
- The above output shows that the syntax error happens in code line 12 of the file Gruntfile.js.
- Open the Gruntfile.js in a text editor, and go to the code line 12, I found the error is because of the wrong encoding of the single quotes( ‘ ), below is the correct one.
pkg: grunt.file.readJSON('package.json'),
- Run the command grunt in the terminal again, and you will find the error is fixed.
- Below is my grunt project’s file structure.
-rw-r--r-- 1 Gruntfile.js drwxr-xr-x 374 node_modules -rw-r--r-- 1 package-lock.json -rw-r--r-- 1 package.json drwxr-xr-x 6 src