---
type: errors
language: javascript
slug: is-not-defined
title: "ReferenceError: x is not defined"
title_tag: "ReferenceError: x is not defined の原因と直し方"
summary: >
  宣言していない名前を読もうとしたときに出ます。綴り違い、読み込み順、スコープの外、
  Node とブラウザで存在しないもの。原因を多い順に並べ、それぞれ直したコードを載せています。
description: >
  宣言が無い名前を読むと出ます。似ている「Cannot access before initialization」との違い、
  typeof なら落ちない理由、window や require が無い環境での対処まで実行して確かめます。
status: published
difficulty: 1
minutes: 8

versions:
  verified: "Node 22.22.3"
  since: "ES1"
  deprecated: null
  removed: null

sources:
  - title: "ResolveBinding — ECMAScript® 2026 Language Specification"
    url: "https://tc39.es/ecma262/#sec-resolvebinding"
  - title: "The typeof Operator — ECMAScript® 2026 Language Specification"
    url: "https://tc39.es/ecma262/#sec-typeof-operator"
  - title: "ReferenceError: \"x\" is not defined — MDN"
    url: "https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Errors/Not_defined"

terms: [宣言, スコープ, 巻き上げ, グローバルオブジェクト]

links:
  related:
    - javascript/errors/cannot-access-before-initialization
    - javascript/errors/cannot-read-properties-of-undefined
    - javascript/errors/is-not-a-function

content_updated_at: 2026-09-08
published_at: 2026-09-08
---

**どこにも宣言が無い名前**を読もうとしたときに出ます。

```js bad
console.log(notDeclared);
```
```output
ReferenceError: notDeclared is not defined
```

まず**綴りを確かめてください。** 大文字小文字も区別されます。

```js run
const userName = 'あ';

console.log(userName);
```
```output
あ
```

## 似ているエラーとの違い

**ここを取り違えると、探す場所を間違えます。**

| 文言 | 意味 | 探す場所 |
|---|---|---|
| `x is not defined` | **宣言が無い** | 綴り・読み込み・スコープ |
| `Cannot access 'x' before initialization` | 宣言はある。まだ使えない | [宣言の位置](/ja/javascript/errors/cannot-access-before-initialization/) |
| `Cannot read properties of undefined` | 変数はある。**中身が `undefined`** | [値の出どころ](/ja/javascript/errors/cannot-read-properties-of-undefined/) |
| `x is not a function` | 変数はある。関数ではない | [呼び出し](/ja/javascript/errors/is-not-a-function/) |

`x is not defined` だけが「**名前そのものが見つからない**」です。

## `typeof` なら落ちない

```js run
console.log(typeof notDeclared);
```
```output
undefined
```

`typeof` は、宣言が無くても[em:例外にならない]数少ない書き方です。
**あるかどうかを調べたいときに使えます。**

```js run
if (typeof someGlobal === 'undefined') {
  console.log('この環境には無い');
}
```
```output
この環境には無い
```

[bad:ただし `let` / `const` の宣言より前では、`typeof` でも落ちます。]
そちらは別のエラーです。
→ [Cannot access before initialization](/ja/javascript/errors/cannot-access-before-initialization/)

## 原因1: スコープの外から読んでいる

```js bad
function setUp() {
  const config = { retry: 1 };
}

setUp();

console.log(config);
```
```output
ReferenceError: config is not defined
```

`config` は関数の中だけの名前です。**外からは見えません。**

外で使いたいなら、[fn:返して受け取ります]。

```js run
function setUp() {
  return { retry: 1 };
}

const config = setUp();

console.log(config);
```
```output
{ retry: 1 }
```

`{}` のブロックでも同じです。

```js bad
if (true) {
  const inside = 1;
}

console.log(inside);
```
```output
ReferenceError: inside is not defined
```

`let` と `const` は[key:ブロックの中]だけで生きています。

## 原因2: 環境に無いものを使っている

**同じコードでも、動く場所によって存在しないものがあります。**

```js run
console.log(typeof window);
console.log(typeof document);
console.log(typeof globalThis);
```
```output
undefined
undefined
object
```

[dim:上はこのページの実行環境（ブラウザのワーカー）での結果です。画面を持たないので `window` も `document` もありません。]

| 名前 | ブラウザの画面 | Node | ワーカー |
|---|---|---|---|
| `window` `document` | ある | [bad:無い] | [bad:無い] |
| `require` | [bad:無い] | CommonJS のときだけ | [bad:無い] |
| `process` | [bad:無い] | ある | [bad:無い] |
| `globalThis` | ある | ある | ある |

**どこでも使えるのは `globalThis` だけ**です。

`require is not defined` が出たときは、そのファイルが
[key:モジュールとして読まれている]（`import` を使う）ことを疑ってください。

## 原因3: 読み込み順

`<script>` を並べる書き方では、**後ろのファイルの中身は前のファイルから見えません。**

```js bad
useHelper();

// helper.js は、このあとで読み込まれる
```

`import` で書けば、順番は処理系が解決します。
古い書き方のまま並べているなら、**使う側を後ろに**置いてください。

## 原因4: 代入したつもりで宣言していない

```js run
'use strict';

try {
  undeclared = 1;
} catch (error) {
  console.log(error.name + ': ' + error.message);
}
```
```output
ReferenceError: undeclared is not defined
```

宣言せずに代入すると、strict モードでは**代入の時点で**落ちます。

[bad:strict でないと、代入は通ってしまいます。] そしてグローバルに名前が増えます。

```js run
undeclared = 1;

console.log(undeclared);
console.log(globalThis.undeclared);
```
```output
1
1
```

**気づかないうちにグローバルを汚しています。**
`import` / `export` のあるファイルは自動的に strict なので、実務では落ちるほうが普通です。

必ず `const` か `let` を書いてください。

## 探しかたの順序

1. エラーの文に出ている**名前をそのまま検索**する。宣言が1つも無ければ綴り違い
2. 宣言があるなら、**それが同じスコープにあるか**見る
   - 関数やブロックの中なら → 返して受け取る
3. `window` `document` `require` `process` のどれかなら → **環境の違い**
4. どれでもなければ、**宣言より前で使っていないか**見る
   - その場合、文言は `Cannot access ... before initialization` になっているはず

[dim:このページの実行結果は Node 22.22.3 のものです。文言は処理系と版によって変わります。自分の環境で出た文言で検索してください。]

## まとめ

- **名前そのものが見つからない**とき。まず綴りを疑う
- `typeof` なら宣言が無くても落ちない。**存在確認に使える**
- `let` / `const` は[key:ブロックの中]だけ。外からは見えない
- `window` `document` `require` `process` は**環境によって無い**
- 宣言せずに代入すると、strict では落ち、そうでないと[bad:黙ってグローバルが増える]

変数はあるのに中身が `undefined` だった、という場合はこちらです。
→ [Cannot read properties of undefined](/ja/javascript/errors/cannot-read-properties-of-undefined/)
