Nodejs SyntaxError:无效或意外

Utk*_*rsh 0 javascript node.js express

在我的index.js文件中,出现此错误

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)
Run Code Online (Sandbox Code Playgroud)

index.js的实际内容是这样的:

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

module.exports = function(app, db) {
    noteRoutes(app, db);
}
Run Code Online (Sandbox Code Playgroud)

我检查了奇怪的引号和目录名称。什么都没用。看来问题出在这一行。

const noteRoutes = require("./note_routes");
Run Code Online (Sandbox Code Playgroud)

编辑:

note_routes.js

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

节点版本为v8.9.4

我正在使用崇高的文字3。

Utk*_*rsh 5

好。我找到了解决此问题的方法。

问题是index.js我使用PowerShell的Out-File index.js命令在项目中创建了文件。那可能导致了一些编码问题。

在仅通过创建文件时right-click > New file > index.js,在运行代码时没有出现此类错误。