Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js SyntaxError: Invalid or unexpected token

I have downloaded and installed node.js from https://nodejs.org/en/. I want to run a js file:

console.log('hello');

I run node app.js

And get a syntax error:

SyntaxError: Invalid or unexpected token
[90m    at Module._compile (internal/modules/cjs/loader.js:895:18)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:815:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:727:14)[39m
[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)[39m
[90m    at internal/main/run_main_module.js:17:11[39m

My OS Info:

Version: 10.0.18362
BuildNumber: 18362
OSArchitecture: 32-bit

node.js v12.13.1

like image 421
Slava Logos Avatar asked Nov 27 '19 15:11

Slava Logos


People also ask

How to fix Unexpected token in Node js?

The "SyntaxError: Unexpected token import" occurs when we use the ES6 import syntax in a version of Node that doesn't support it. To solve the error, use the require syntax, e.g. const myPackage = require('my-package') or set the type attribute to module in your package. json file.

What is SyntaxError Unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.


2 Answers

Did you copy some unicode characters from word or something?

try retyping the console.log('hello') Something like this has happened to me before, when I copied some code from a word document.

like image 115
Patrick de Kievit Avatar answered Oct 19 '22 17:10

Patrick de Kievit


The problem was that I created the file by redirecting output from echo command in powershell. It had UTF-16 LE encoding, should be UTF-8

like image 8
Slava Logos Avatar answered Oct 19 '22 16:10

Slava Logos