简体   繁体   中英

node.js SyntaxError: Invalid or unexpected token

I have downloaded and installed node.js from https://nodejs.org/en/ . I want to run a js file:

console.log('hello');

I run node app.js

And get a syntax error:

SyntaxError: Invalid or unexpected token
[90m    at Module._compile (internal/modules/cjs/loader.js:895:18)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:815:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:727:14)[39m
[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)[39m
[90m    at internal/main/run_main_module.js:17:11[39m

My OS Info:

Version: 10.0.18362
BuildNumber: 18362
OSArchitecture: 32-bit

node.js v12.13.1

Did you copy some unicode characters from word or something?

try retyping the console.log('hello') Something like this has happened to me before, when I copied some code from a word document.

The problem was that I created the file by redirecting output from echo command in powershell. It had UTF-16 LE encoding, should be UTF-8

Had the same issue for hours, echo const http=require('http') >> index.js . Solved

Still, nothing! It wont work!

Code:
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const config = require('./config.json')

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
console.log ("My new first bot is ready and is launched! Yay!")
});

// Login to Discord with your client's token
client.login(config.token)

// CREATE COMMANDS!!!
client.on("messageCreate", async (message) => {
    let prefix = "$"
    const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`);
            if(prefixRegex.test(message.content)) {
                const [, matchedPrefix] = message.content.match(prefixRegex);
    let args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
                    let cmd = args.shift()?.toLowerCase(); 
    
    if(cmd == "test") {
    return message.channel.send("I am Working!")
    })


Error:
SyntaxError: Unexpected token ')'
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1055:15)
    at Module._compile (node:internal/modules/cjs/loader:1090:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Idk what I did wrong...

image

if you take a loot at the picture. you can see th error is on R21. You are trying to close the if from r14 with a ) instead of a }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM