You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

解决Node.js运行时出现的Unexpected token语法错误

Fixing the "Unexpected token, expected ; (7:15)" Error in Node.js

Hey there! That Unexpected token, expected ; error at line 7, column 15 is a super common JavaScript syntax hiccup—your Node.js parser is choking on something it doesn’t recognize in that spot. Let’s walk through the most likely fixes:

Common Causes & Solutions

1. Arrow Function Returning an Object (Missing Parentheses)

If you’re using an arrow function to return an object literal directly, forgetting to wrap the object in parentheses will confuse the parser. It thinks the curly braces are the start of a function body instead of an object.

Wrong:

const getProfile = () => { name: 'Ray', age: 28 }; // Line 7 might look like this

Right:

const getProfile = () => ({ name: 'Ray', age: 28 }); // Wrap object in parentheses

2. Misplaced Code Blocks After Conditionals/Loops

If you have an if/for/while statement without curly braces, and the next line starts with something the parser doesn’t expect (like an object or variable declaration), it’ll throw this error.

Wrong:

if (isLoggedIn)
  console.log('Welcome back')
  { userRole: 'member' }; // Line 7 here is a rogue code block

Right:

if (isLoggedIn) {
  console.log('Welcome back');
  const userRole = 'member'; // Wrap code in curly braces
}

3. Failed Automatic Semicolon Insertion (ASI)

JavaScript tries to auto-add semicolons where it thinks they’re needed, but sometimes it gets it wrong—especially when a line starts with parentheses or brackets.

Wrong:

const total = 42
(function calculate() { console.log(total) })() // Parser sees total(...) instead of two separate lines

Right:

const total = 42;
(function calculate() { console.log(total) })() // Add the semicolon manually

4. Unclosed Quotes or Template Strings

A missing closing quote (single, double, or backtick) on line 7 or the line before will make the parser interpret everything after as part of a string, leading to unexpected tokens.

Wrong:

const greeting = 'Hello, // Missing closing single quote
console.log(greeting); // Line 7 is now inside an unclosed string

Right:

const greeting = 'Hello,';
console.log(greeting);

Next Step

If none of these fix it, share the code around line 7 (columns 10-20 or so) and we can pinpoint exactly what’s going on!

内容的提问来源于stack exchange,提问作者Raywengay

火山引擎 最新活动