“Syntax error: Invalid or unexpected token” running a script with node.js

谁都会走 提交于 2020-01-13 17:06:06

问题


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

回答1:


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 with a BOM, it isn't necessary and better avoided.




回答2:


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.



来源:https://stackoverflow.com/questions/49334643/syntax-error-invalid-or-unexpected-token-running-a-script-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!