"Syntax error: Invalid or unexpected token" running a script with node.js
Asked Answered
E

2

5

i get the following error message from the terminal (powershell) when i try to run my echo.js file using node js

PS C:\Users\ASUS\Dropbox\Web Development\Backend\Intro To Node> node echo.js
C:\Users\ASUS\Dropbox\Web Development\Backend\Intro To Node\echo.js:1
(function (exports, require, module, __filename, __dirname) { ��
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
Evin answered 17/3, 2018 at 9:52 Comment(0)
S
7

Your echo.js file is most likely UTF-16LE-encoded, which is an encoding that node doesn't support. (The �� are the BOM (Unicode byte-order mark) characters, which node misinterprets as data).

To fix this problem, save echo.js using UTF-8 encoding. While you can choose UTF-8 without a BOM, it isn't necessary and better avoided.

Slam answered 22/3, 2018 at 17:3 Comment(0)
S
0

In Mac, use TextEdit. Go to FORMAT and select Make Plain Text. Then while saving file, choose plain text encoding as Customize Encoding List and select Unicode (UTF-8) and unselect everything else.

Also, you get this error when you copy paste code. In that case ensure you replace your single inverted commas as the format is different when you copy paste code. Replacing should make the error go away.

Somali answered 19/8, 2018 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.