Reputation: 1606
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
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
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.
open file in vscode.
click vscode footer UTF-16 LE (Select Encoding) button.
reopen with Encoding
utf-8
wow,this's ok.
Upvotes: 1
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
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
Reputation: 451
copy content of first index.js file
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
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
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