Nyx

Chapter 17 · Reference

Finding and fixing mistakes

Everyone's code goes wrong, every day, including experts'. What matters is finding out what went wrong and where. In this chapter you'll learn to read the browser's console, understand Nyx's messages and JavaScript's own errors, and fix problems quickly.

Opening the console

Browsers have a hidden panel for developers called the console. It's where error messages appear, both from JavaScript and from Nyx. A visitor never sees it, so a page can be broken without anything showing on the screen. Get used to keeping it open while you work.

BrowserHow to open the console
Chrome, EdgeF12, or Ctrl+Shift+J. On a Mac: ⌘+Option+J.
FirefoxF12, or Ctrl+Shift+K. On a Mac: ⌘+Option+K.
SafariFirst turn on Settings → Advanced → Show features for web developers. Then ⌘+Option+C.
Any browserRight-click the page, choose Inspect, then the Console tab.

Messages come in three colours:

ColourMeaning
Red: errorSomething failed. Fix these first.
Yellow: warningProbably a mistake, but the page kept going.
Grey or blue: infoA tip or a note. Nothing broke.

Your own console.log(value) lines appear here too, which is the quickest way to check what a variable holds. On this website, print() does the same job inside the examples, as you learned in Getting started.

Reading a Nyx message

When Nyx spots a mistake, it writes a message in the console that answers three questions: what is wrong, where it happened, and how to fix it.

Nyx: Nothing matches "#sumbit". (app.js, line 4)

-> Did you mean "#submit"?

PartWhat it tells you
Nyx:This message comes from Nyx, not from JavaScript itself.
Nothing matches "#sumbit".What went wrong, in plain English.
(app.js, line 4)Where: the file and line in your own code. Open that file and go to that line.
-> Did you mean "#submit"?How to fix it. Often it spots the exact typo.

The file and line appear when your code is in its own .js file, or in a <script> on a page opened through a server like Live Server. Examples on this website don't have a file of their own, so their messages appear without a line.

Making mistakes on purpose

The best way to learn the messages is to cause them. Each line below has one mistake. Press Run and read what Nyx says about each one:

A card

Fix the lines one at a time and press Run again, until the Output is empty.

JavaScript's own errors

Some mistakes break the JavaScript language itself, not Nyx: a misspelled variable, a missing bracket. JavaScript reports these in red, in short, technical English. Nyx adds a "That error means..." note under the most common ones, with the file and line.

Score: 0

Click +1: the Output shows JavaScript's error and Nyx's explanation. Find the typo and fix it.

Here's every JavaScript error Nyx explains, and how to fix each one:

JavaScript saysIt means, and how to fix it
scroe is not definedThat name doesn't exist. Check the spelling (capitals count), create it with const or let first, or put quotes around text.
Cannot read properties of undefined (reading 'text')You used .text on something empty. Usually a typo in a name, a search that found nothing, or data that hasn't arrived (a missing await).
nyx.tost is not a functionThat isn't a function, so you can't call it with ( ). Check the spelling of the method.
await is only valid in async functionsAdd async to the function around the await.
Assignment to constant variableYou changed a const. Use let for values that change.
Cannot access 'score' before initializationYou used a name before the line that creates it. Move that const or let line up.
... is not iterableYou looped over something that isn't a list. Check it's an array, and that server data was awaited.
Unexpected end of inputA {, ( or [ is never closed. Count your brackets.
Unexpected token, missing ) after argument listJavaScript couldn't read the code: a missing or extra comma, quote or bracket near that line.

Firefox and Safari word some errors a little differently, like box is undefined. Nyx recognises those too.

Explaining errors on the page: nyx.explain

nyx.explain(error) gives you Nyx's plain-English version of an error, as { message, hint }, or null if it doesn't recognise it. Use it in a catch to show something friendlier than the technical text. Type name or city, then something that isn't there, like nmae:

  1. student[field]Reads the field whose name is in field. With "city" it's the same as student.city. A name that isn't there gives undefined.
  2. .toUpperCase()Turns text into capital letters. On undefined, JavaScript throws "Cannot read properties of undefined".
  3. nyx.explain(error)Turns that into "You used .toUpperCase on a value that is empty (undefined)."

Errors from Nyx's own requests (from Servers and AJAX) work too: you get their message and hint.

When nothing happens, and nothing is said

The hardest problems are silent: you click, nothing happens, and the console is empty. Go through this list from the top:

CheckHow
Is your script loaded at all?Open the Network tab and refresh. A red line or 404 means a wrong file name or folder. Or write console.log("loaded") at the top of your file and look for it.
Is Nyx loaded before your script?"nyx is not defined" means Nyx's <script> must come first, as in Getting started.
Are you seeing an old copy?Browsers keep files to load faster. Refresh with Ctrl+Shift+R (⌘+Shift+R on a Mac) to fetch fresh ones.
Does the code run before the element exists?Wrap it in nyx(() => { ... }), or put your script at the end of the page.
Is the listener on the right element?Add console.log("clicked") inside it. No message means the click never reached it.
Did it work, but you can't see it?Use Inspect on the element: CSS may hide it, or it may be off the screen.
Did you turn messages off?Check for nyx.quietMode = true left in your code.

Good debugging habits

  1. Read the first red message firstOne mistake often causes several messages after it. Fixing the first can make the rest disappear.
  2. Go to the line it namesThen look at the line just above too: a missing bracket is often noticed one line late.
  3. Check what values really areconsole.log(value) (or print() on this website) shows what a variable holds right now. Guessing is slower.
  4. Change one thing at a timeThen test. If you change five things at once and it works, you won't know which one fixed it.
  5. Make the problem smallDelete or comment out code until the mistake is obvious, then add it back.
  6. Explain your code out loudLine by line, to a friend, or to a rubber duck. You'll often hear the mistake as you say it.

Showing messages on the page: nyx.listen

nyx.listen() runs your function for every message Nyx gives, even when the console is closed. It's handy for a debug panel on a phone, where the console is hard to reach. This is how the Output in this website's examples shows Nyx's messages.

    Each message hasWhat it holds
    m.level"error", "warn" or "tip".
    m.messageWhat went wrong.
    m.hintHow to fix it, or empty text.
    m.where{ file, line } in your code, or null when it isn't known.
    m.timeWhen it happened, as a Date.

    Like the timers, nyx.listen() gives back something with .stop(): listener.stop() stops listening.

    Settings

    SettingWhat it does
    nyx.help()Prints a cheat sheet of Nyx's most useful tools in the console.
    nyx.explainMissing = falseStops the "Nothing matches" messages, for pages that check on purpose whether an element exists.
    nyx.explainErrors = falseStops the "That error means..." notes under JavaScript's own errors.
    nyx.quietMode = trueStops Nyx writing in the console at all. nyx.listen() still gets every message.

    Leave all of these as they are while you're learning. The messages only appear in the console, so visitors never see them.

    Every Nyx message

    Here's every message Nyx can give, grouped by chapter, with what to do. Messages are shortened: the real ones name your exact selector, field or value.

    Finding elements

    Nyx saysWhat to do
    Nothing matches "#sumbit". Did you mean "#submit"?Fix the spelling of the selector.
    No element has the id "card", but some have the class "card".Use .card instead of #card, or the other way round.
    "menu" has no # or . in front, so I looked for a <menu> tag.Add # for an id or . for a class.
    Nothing matches yet, because the page is still loading.Wrap your code in nyx(() => { ... }).
    "div >" is not a valid selector.Fix the CSS selector: something is missing or extra.
    I don't know how to find ...Give nyx() a selector in quotes, an element or a list of elements.

    Changing content, styles and events

    Nyx saysWhat to do
    A file box can't be filled in by code.Only the visitor can choose a file. .value("") clears it.
    .show() is instant, so it doesn't take a speed or a function.For an animation use .fadeIn() or .slideDown(), and .done() to run code after.
    .on("click") needs a function to run.Give it () => { ... }.
    "clik" is not an event I know. Did you mean "click"?Fix the event name.
    You added a "click" listener, but "#btn" matched nothing.The listener will never run. Check the selector.
    .picker() needs to know which children can be picked.Give it a selector: .picker("li").
    .appear() needs a function.Give it the code to run when the element scrolls into view.

    Animation

    Nyx saysWhat to do
    An animation stopped early.The browser couldn't finish it, often because the element was removed during the animation.

    Servers and AJAX

    Nyx saysWhat to do
    No URL was given.Give the address: nyx.get("/api/posts").
    Nothing found at ... (404).Check the address's spelling, and that the file or route exists.
    The server refused the request (403) / You need to be logged in (401).Log in, or check permissions. In WordPress, reload the page.
    ... doesn't accept this kind of request (405).Use nyx.get() instead of nyx.post(), or the other way round.
    The server couldn't use the data you sent (400, 422).Check the data's names and required fields. The server's own words follow.
    Too many requests, too fast (429).Send fewer requests, for example with nyx.debounce().
    The server had a problem (500).A bug on the server, not in your JavaScript.
    ... took longer than 30 seconds to answer.Try again, or allow more time: { timeout: "60 seconds" }.
    I couldn't reach ...Check the internet connection. Another website may not allow requests from yours (CORS).
    ... sent back a web page instead of data.The address is wrong, or the server hit an error.
    ... sent data that isn't valid JSON.The server code has a bug, or prints something extra.
    ... loaded, but nothing in it matches "#part".Check the selector after the address in .load().
    .formData() / .send() works on a <form> element.Select the form itself, not an input inside it.

    WordPress

    Nyx saysWhat to do
    WordPress has no handler for the action "like" (it answered 0).Add add_action( 'wp_ajax_like', ... ), plus wp_ajax_nopriv_like for visitors.
    WordPress rejected the security check (it answered -1).The page is old or cached. Reload it.
    WordPress sent a web page instead of data. It said: "Warning: ..."A PHP warning. Fix the line it names, or turn on WP_DEBUG.
    The answer has an extra 0 at the end.Use wp_send_json_success() instead of echo.
    WordPress said: ...Your PHP's own wp_send_json_error() message.
    rest_no_route, rest_cookie_invalid_nonce, rest_not_logged_in, rest_forbidden, rest_invalid_param ...Each is explained in plain English. See Using Nyx in WordPress.
    nyx.wp.ajax needs an action name.Give the action first: nyx.wp.ajax("like_post", data).

    Timers

    Nyx saysWhat to do
    I can't read the time "5 minuts".Fix the spelling: "5 minutes".
    nyx.after() takes the time first, then the function.Swap them: nyx.after("2 seconds", fn).
    nyx.after() needs a function to run later.Wrap the code in () => { ... }.
    nyx.after(5, ...) means 5 milliseconds.Write "5 seconds".
    nyx.countdown() can't read the date.Write year-month-day: "2027-02-15T10:00".

    Formatting

    Nyx saysWhat to do
    nyx.money() needs a number, but got "₹500".Give just the digits. From an input, use Number() and check it isn't empty.
    "IRN" is not a currency code I know. Did you mean "INR"?Use the three-letter code.
    Use the 3-letter code "INR" instead of "Rs".Write "INR".
    nyx.money()'s third value should be options in { }.Write { decimals: 0 }.
    "compact" is not a style nyx.number() knows.Use one of the styles it lists.
    nyx.date() can't read the date "29/11/2026".Write year-month-day: "2026-11-29".
    nyx.plural() needs the word to count.Write nyx.plural(3, "student").

    Saving data and smart state

    Nyx saysWhat to do
    nyx.remember() needs a name for the value, in quotes.Write nyx.remember("theme", "large").
    nyx.remember() can't save a function / a page element.Save data instead, like the element's text.
    I couldn't save: this browser's storage for the site is full.Forget values you no longer need.
    This browser isn't letting the page save anything.A private window or blocked site data. The page keeps working, but forgets on refresh.
    nyx.state() needs an object in { }.Write nyx.state({ count: 0 }).
    "reset" can't be a name in nyx.state().Choose another name: it's one of the state's tools.
    "remeber" is not a nyx.state() option.Write { remember: "name" }.
    .watch() needs a function.Give it the code that draws the page.

    Checking forms

    Nyx saysWhat to do
    The form has no field named "emial". Did you mean "email"?Use the field's name, spelled exactly.
    "requird" is not a rule I know. Did you mean "required"?Fix the rule's spelling.
    The rule "min" needs a number after it.Write "min 8".
    The form has no field named "newpasword" (same as).Write the other field's name exactly.
    The rules for "email" should be text in quotes.Write email: "required, email".
    nyx.check() works on a <form>.Select the <form>, not a <div>.
    nyx.check() needs the rules in { }.Give one line per field name.
    .submit() needs a function.Give it the code to run when the form is right.
    nyx.rule() needs a name and a function.Write nyx.rule("no spaces", (value) => ...).
    The rule "..." already exists. Your version replaces it.Choose another name, unless you meant to replace it.

    Searching lists

    Nyx saysWhat to do
    nyx.search() needs a list in [ ] to search.Give it an array.
    For elements on the page, use .search() on them.Write nyx("#list li").search(text).
    Put the field names in a list: ["name"].Add the square brackets.
    No item in the list has a field called "nmae".Use the real field name it suggests.
    nyx.closest() needs a list of choices in [ ].Give it an array.

    Everyday helpers

    Nyx saysWhat to do
    "sucess" is not a toast type. Did you mean "success"?Use info, success, warn or error.
    nyx.copy() copies text, not an element.Copy its text: nyx.copy(nyx("#code").text()).
    This browser can't read text aloud.Offer the text another way.
    "contrl" in "contrl+k" is not a key I know.Use ctrl, cmd, mod, shift or alt.
    nyx.shortcut() / nyx.online() / nyx.listen() needs a function.Give it the code to run.
    nyx.chart(): the value for "Maths" is "92", not a number.Use numbers: Number(...) for text from inputs.
    nyx.chart() needs the data as an object.Write { Maths: 92, Science: 85 }.

    Exercise

    Fill in the blanks, then press Check answers.

    1. Show a value in the console while you work.

    console.(score);

    2. JavaScript says scroe is not defined. Which word did you probably mean?

    3. await is only valid in async functions. Add the missing word.

    nyx("#load").on("click",  () => { await nyx.get(url); });

    4. Assignment to constant variable. Fix the first word.

     count = 0;
    count++;

    5. Get Nyx's plain-English version of an error.

    const help = nyx.(error);

    6. Run a function for every Nyx message.

    nyx.((m) => showOnPage(m.message));

    7. Print Nyx's cheat sheet in the console.

    nyx.();

    Remember

    Keep the console open while you work: F12, then the Console tab.
    Nyx's messages say what's wrong, where (file and line), and how to fix it.
    Under JavaScript's own errors, Nyx adds "That error means..." in plain English.
    Fix the first red message first, and change one thing at a time.
    Silent problems: check the Network tab, the script order, an old cached copy, and console.log() inside listeners.
    nyx.explain() translates an error; nyx.listen() shows every message on the page.