Utkarsh Bhatt
Utkarsh Bhatt

Reputation: 1606

Nodejs SyntaxError: Invalid or unexpected

In my index.js file, I have this error

C:\Users\Utkarsh\Desktop\LearningNode\notable\app\routes\index.js:1
(function (exports, require, module, __filename, __dirname) { ��c
                                                              ^

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 Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Utkarsh\Desktop\LearningNode\notable\server.js:11:1)

The actual contents of index.js is this:

const noteRoutes = require("./note_routes");

module.exports = function(app, db) {
    noteRoutes(app, db);
}

I have checked for weird quotes and directory names. Nothing is working. It seems that the problem is residing in this line.

const noteRoutes = require("./note_routes");

Edit:

note_routes.js

module.exports = function(app, db) {
    app.post("./notes", (req, res) => {
        // We'll create the note here
        res.send("hello");
    })
}

Node version is v8.9.4

I'm using sublime text 3.

Upvotes: 4

Views: 22644

Answers (7)

vivek kumar
vivek kumar

Reputation: 1

If you have create your file using CLI . then it will give this error as 'Invalid or unexpected' . try creating file directly using GUI.It will help.

Upvotes: 0

Tom Pang
Tom Pang

Reputation: 707

I meet same proplem.I use powershell command(echo '' > index.js) new create index.js file.

If you use vscode, you can use the following method.

  1. open file in vscode.

  2. click vscode footer UTF-16 LE (Select Encoding) button.

enter image description here

  1. select reopen with Encoding

enter image description here

  1. select utf-8

enter image description here

  1. delete error string.

enter image description here

wow,this's ok.

Upvotes: 1

Nana Nagane
Nana Nagane

Reputation: 1

It doesn't work with just files we need to use Notepad++(using it just run the file and remove the spaces wherever the error occurs) and save the file in a new folder wherein we again need to install node and dependent libraries using CLI and then it works perfectly fine.

Upvotes: 0

usersam
usersam

Reputation: 1235

I also faced the similar problem. I just renamed "node_modules" to "old_node_modules" and executed "npm install" command. This took some time but problem got resolved and i got able to start the project. Later i removed the "old_node_modules" folder.

Upvotes: 0

Akash Anaghan
Akash Anaghan

Reputation: 451

  1. copy content of first index.js file

  2. just create new index.js file manually with right click context menu and paste same content in new index.js file

because it is problem of different encoding file created with shell.

Upvotes: 4

Michael Rust
Michael Rust

Reputation: 1

I had the same issue, different solution.

The index.js file, which was created by PowerShell, is encoded with UCS-2 LE BOM.

To change the file so that node can run it (rather than deleting and making a new file) I changed the encoding to UTF-8 using NotePad++

Upvotes: 0

Utkarsh Bhatt
Utkarsh Bhatt

Reputation: 1606

OK. I found the solution to this issue.

The thing is that I created the index.js file in my project using PowerShell's Out-File index.js command. That might have caused some encoding issue.

On creating the file simply by right-click > New file > index.js, I get no such errors on running the code.

Upvotes: 34

Related Questions