Arduino build error

error: 'Servo' does not name a type

The compiler expected a type, such as int, String or Servo, and the word it found is not one it knows.

  • Build error
  • 4 common causes
  • Seen 725 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message

What the Arduino IDE prints

Output window
sketch.ino:1:1: error: 'Servo' does not name a type
    1 | Servo gate;
      | ^~~~~
exit status 1
Compilation error: exit status 1

It happens when a library is not included, when a statement sits outside every function, when a type is spelled with the wrong capital, and when a type is used above the line that defines it.

Mokxi's compiler, clang, words it as unknown type name 'Servo'.

Causes

The 4 usual causes with the fix

1A library class without its #include

Servo is a class that the Servo library declares. The sketch has to include the library before the first line that makes a Servo.

Broken
Servo gate;

void setup() {
  Serial.begin(9600);
  gate.attach(9);
  Serial.println("gate ready");
}

void loop() {
}
Fixed
#include <Servo.h>

Servo gate;

void setup() {
  Serial.begin(9600);
  gate.attach(9);
  Serial.println("gate ready");
}

void loop() {
}

Mokxi’s build tab says: Servo comes from a library the sketch does not include. Add #include <Servo.h> at the top of the sketch.

Try this fix in Mokxi

2A statement outside every function

Between functions you can declare variables and objects. You cannot run a call. A line such as Serial.begin(9600); at the top of the file reads to the compiler like the start of a declaration, so it asks what type Serial is.

Broken
int count = 0;
Serial.begin(9600);

void setup() {
  count = 3;
  Serial.println(count);
}

void loop() {
}
Fixed
int count = 0;

void setup() {
  Serial.begin(9600);
  count = 3;
  Serial.println(count);
}

void loop() {
}

Mokxi’s build tab says: Serial is used as a type, but nothing declares it.

Try this fix in Mokxi

3A type spelled with the wrong capital

Arduino's text type is String with a capital S. The lowercase string is not a type in an Arduino sketch.

Broken
string name = "Mokxi";

void setup() {
  Serial.begin(9600);
  Serial.println("Hello " + name);
}

void loop() {
}
Fixed
String name = "Mokxi";

void setup() {
  Serial.begin(9600);
  Serial.println("Hello " + name);
}

void loop() {
}

Mokxi’s build tab says: Arduino spells it String, not string. Names in C++ must match exactly, capital letters included.

Try this fix in Mokxi

4A struct used above the line that defines it

The compiler has to see struct Point before it can read a line that makes a Point. Move the definition to the top, above everything that uses it.

Broken
Point spot = {3, 4};

struct Point {
  int x;
  int y;
};

void setup() {
  Serial.begin(9600);
  Serial.println(spot.x);
}

void loop() {
}
Fixed
struct Point {
  int x;
  int y;
};

Point spot = {3, 4};

void setup() {
  Serial.begin(9600);
  Serial.println(spot.x);
}

void loop() {
}

Try this fix in Mokxi

FAQ

Questions and answers

What does 'does not name a type' mean in Arduino?

The compiler expected a type name and found a word it has no type for. Usually the library include is missing or a call was written outside a function.

Why does a missing #include give this error and not "file not found"?

Nothing is wrong with the include line you have. The include line you need is not there at all, so the compiler never learns the type exists.

Why does 'Serial' not name a type?

The line Serial.begin(9600); sits outside setup() and loop(). Move it into setup().