12

I've just arted to learn node, and when executing a very simple app from a powershell terminal:

node app.js

I am getting the following exception:

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
PS C:\Users\kisco\Source\Repos\library> node app.js
C:\Users\kisco\Source\Repos\library\app.js:1
(function (exports, require, module, __filename, __dirname) { ��v
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
PS C:\Users\kisco\Source\Repos\library>

Here's my source for app.js:

var express = require('express');

var app = express();

app.get('/', function(req, res){
    res.send('Hello from my libvrary app')
})

app.listen(3000, function(){
    console.log('listening on port 3000');
});

What am I doing wrong?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

10 Answers10

9

It looks like there is some encoding issue with the file that you're trying to run with Node. Sometimes, files created with some command line utility does some encoding issues with the file. So, I would suggest not to use them to create file. Just create file with Right-Click > New File > app.js and replace your old file with this app.js. And try running node app.js.

Muhammad Usman
  • 10,039
  • 22
  • 39
4

It may happen due to mismatch of node versions. Please check the compatible version for your code repo.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 21 '21 at 10:17
  • In my case, I had two node version installed, for diff projects. And it was picking an older one on ng serve. Just remove one version or point to particular node version before hit start command. – Karan Raiyani Apr 25 '23 at 07:56
2

I solved this issue by updating my version of node on Ubuntu 16

sudo npm cache clean -f
sudo npm install -g n
sudo n stable
Barney Chambers
  • 2,720
  • 6
  • 42
  • 78
2

I solved it by deleting the entire node module and reinstalling.

Tim Kariuki
  • 633
  • 8
  • 17
  • it works, thanks! how to delete all node js, npm https://stackoverflow.com/questions/20711240/how-to-completely-remove-node-js-from-windows – yu yang Jian Aug 04 '22 at 07:43
1

I had a similar issue. My problem was that I copy-pasted code from a website and the '"' were wrongly formatted by my editor. So, I replaced all those quotations with " or ', whichever is applicable.

That solved my issue.

aiyu
  • 11
  • 5
1

I faced smilier issue.

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)

But in my case, i was using nvm and forgot to switch the node version to latest version. After switching to latest version it works and did not get this exception

1

For me I was using the wrong version of node/npm on which the node modules where installed.

printthis
  • 141
  • 3
  • 11
0

I got this same error when trying to run a program I compiled as an executable. Instead of just running ./program I was trying to run it with node. So I did node ./program.

Trying to run the executable (compiled with pkg for linux) with node gave me pretty much the exact same error you got here.

  • Whether you were able to find solution for this? I have created sample application and tried to package with pkg . --debug packaging was successful , but when I tried to run with node testapp it gave me following error (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.... – Chirag Purohit Nov 27 '18 at 06:24
  • 1
    Because it's compiled as an executable, you need to run it as one. So instead of `node testapp` you need to run `./testapp` – Hudson Peden Nov 28 '18 at 14:08
0

I also had the same issue. And indeed it was an encoding problem.
Our js file was parsing another file which had been copied/pasted from a pdf. Turned out there was a corrupted space character in the middle of the file.

Removing it solved my issue.

Sofien
  • 1,302
  • 11
  • 21
0

In my case, all I had to do was unistall webstorm (in your case it might be whatever text editor it is you are using for your Repository to work on your project), delete my project folder and install webstorm afresh then running the following after getting my Repository from github

npm init 
npm install mysql
npm install colors

and then node index.js and I got my server running up again.

Note that I had to update my NodeJS to the latest version.

Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54