Adapting a beloved children’s book for the screen is a difficult task. Adapting a beloved picture book, famous as much for its vivid illustrations as for its spartan plot, is considerably more challenging. Doing it while answering to an increasingly skeptical studio, whose moneymen are convinced that you’re squandering their investment on an expensive art film that’s too childish for adults and too bizarre for children — well, that’s a real test of a director’s mettle.

It’s a test that Spike Jonze has passed. His version of Maurice Sendak’s Where the Wild Things Are, five difficult years in the making, is gorgeous, haunting, and completely unlike any children’s story you’ve ever seen committed to the screen. Yes, it’s also messy, weird, and occasionally tedious. But considering what it could have been — either an overindulged auteur’s spectacular misfire, or a panicked studio’s compromised botch — it’s a miracle that the finished product is this good.

In Sendak’s slender story, a rambunctious boy named Max is sent to bed without his supper, dreams his way into a world of gallumphing monsters who acclaim him as their king — and then, homesick, dreams his way back out again.

In Jonze’s thicker, darker film, Max is a child of divorce, and his rampages are at once a cry for attention — from his older sister and her crowd of friends, and from his weary mother — and an inchoate protest against a home life that’s gone badly out of joint.

One of these protests ends with him racing out of the house into the night, where a suburban subdivision abruptly gives way to a dark wood and a ragged coastline. There, a small sailboat stands ready for his journey across a storm-tossed ocean to the forested, flame-lit island of the Wild Things, where Max finds a society of monsters that’s as disrupted and unhappy as the one he’s fled.

Most of the initial disruption comes from Carol (the rumbling voice of James Gandolfini), who’s tearing down the Wild Things’ strange houses — they’re like enclosed bird’s nests, or hollow balls of yarn — in a fury over the apparent defection of his red-haired crush, KW (Lauren Ambrose), to a different gang of friends. But there’s plenty of unhappiness to go around. The whole tribe — which includes the eagle-headed Douglas (Chris Cooper), the chattery, resentful Judith (Catherine O’Hara) and her ponderous husband Ira (Forest Whitaker), the goatish Alexander (Paul Dano), and a silent, nameless minotaur — seems to be in mourning for some lost Arcadia, a paradise in which (as Carol puts it) “only the things you want to happen would happen.”

They’re ripe for a messiah, in other words, and so they crown Max their king, after he claims extraordinary powers and promises to “keep out all the sadness.” His reign is a child’s idea of paradise, all wild rumpuses, ambitious fort-building, earth-clod fights, and furry group hugs and slumber parties. But paradise is fragile, and slowly but surely, the sadness leaks back in — first in hurt feelings and petty resentments, and then in the kind of wild anger that prompted Max to run away in the first place.

The whole narrative is basically a dream sequence, one that’s as psychologically ambitious, in its way, as any of the Freudscapes that Gandolfini famously wandered through on The Sopranos. As with a true dream, no single interpretation fits exactly, but multiple readings resonate. The Wild Things are at once aspects of Max’s personality brought to shambling, self-pitying, horn-crowned life, a child’s-eye reimagining of his fissiparous domestic sphere, and a recapitulation, on a monstrous scale, of the social dynamics of a kindergarten.

When this works, it really works. The monsters themselves are an astounding technical achievement, part CGI and part gloriously tactile puppetry, with faces more expressive than those of many flesh-and-blood actors. Their island — really the coastline of Australia — has the barren, sadness-haunted beauty that you’d expect to find just east of Eden. And Max Records, the child cast as Max, holds the screen like a seasoned movie star, projecting exactly the right mix of yearning, woundedness, and unexpected joy.

But it’s hard to sustain a trip through the unconscious for the length of an entire film, and I found myself growing frustrated, in stretches, by the movie’s plotlessness and lack of narrative momentum. The monsters’ feuding and pouting can feel annoying as well as captivating, like a perpetual recess crossed with a Park Slope group-therapy session. And although their initial, night-shrouded appearance is suitably terrifying, the Wild Things could stand to be somewhat scarier. Even their famous threat (“We’ll eat you up, we love you so!”) is drained of the menace that Sendak’s book imparts to it, and without that hint of mortal danger, Max’s sojourn on the island — and with it, the movie — limps rather than rushes to a close.

But these may be adult complaints; any child with a healthy sense of wonder, I suspect, would have no trouble giving himself over unreservedly to the movie’s dream logic and ignoring its lapses and longueurs. “I didn’t set out to make a children’s movie,” Jonze has said. “I set out to make a movie about childhood.” But what he’s done blurs those categories: This is a film about childhood that can be appreciated by pretentious, nitpicky grown-ups, but my bet is that it will find its fiercest fans among the Maxes of the world.