使用节点javascript的意外令牌ILLEGAL

Unexpected token ILLEGAL using node javascript

本文关键字:意外 令牌 ILLEGAL javascript 节点      更新时间:2023-09-26

我正试图使用节点JavaScript执行一个命令,但我的JavaScript中出现了"Uncaught SyntaxError:Unexpected token ILLEGAL"。它在1号线上。

var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var db;
var port = process.env.PORT || 8080;
MongoClient.connect(mongo_host, function(err, database) {
    if(err) throw err;
    db = database;
    app.listen(port, function () {
        console.log('listening port' + port);
    });
});
app.get('/', function (req, res) {
    res.json({ message: 'Bienvenue Azure!' });
});
app.get('/plante', function (req, res) {
    db.collection("plante").find().toArray(function(err, users) {
        res.send(users);
    });
});

注意:mongo_host是连接azure 的Git存储库url

然后显示以下错误:

SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:404:25)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:138:18)
    at node.js:974:3

您的分号不是标准分号。它们是'u037e(希腊语问号)。请尝试将它们切换回标准分号,然后运行代码。

有人一直在看推特。

正如@dvlsg所说,这是由等非法符号引起的语法错误。

有一个类似的SO线程对您有帮助,请参阅"没有明显的原因";意外的令牌"非法";。

我通过将Azure web应用程序服务中的节点版本更新到最新版本(在我的情况下为10.6.0)来解决此错误。它一直在犹豫包含ES6的线路。改变这种情况的一种方法是使用环境变量。您可以在应用程序门户的"应用程序设置"中更改它。更改WEBSITE_NODE_DEFAULT_VERSION