I was taking screenshots of a website with a small Playwright script, written as an ES module, and ran it through npx. One of the commands I tried was this:
npx -p playwright node script.mjs It ended in:
ERR_MODULE_NOT_FOUND The reasoning looks airtight. -p playwright tells npx to make Playwright available, then run node script.mjs. The script imports Playwright. Playwright is available. Done.
It wasn't. After the second failure I stopped trying variations of the command and changed the approach entirely. This post is about why npx -p can't help an ES module find a package, what I did instead, why that kind of fix later broke on its own, and what I would do now.
The short version: npx puts things on your PATH. import never looks at your PATH. Those are two different lookup systems, and I was feeding the wrong one.
Failure, then failure again
I don't have a record of the exact first command I tried or its exact error, so I won't pretend I do. What I do have recorded is that npx -p playwright node script.mjs still ended in ERR_MODULE_NOT_FOUND, and that I changed approach after the second failure.
That second failure is where I stopped, and I want to explain why, because it's the part of this story I'd most like to keep.
When a command fails, the natural move is to try a slightly different command. Different flag, different order, quote something, clear a cache, try again. Each attempt is cheap, so it feels fine to keep going. But if the error is identical and the cause is identical, a third variation of the same shape is not a new experiment. It's the same experiment with a different spelling.
The rule I use now is simple: if the same operation fails twice for the same reason, don't run a third version of it. Find out why it fails, and switch to a different method. Retries for flaky things (timeouts, rate limits, a locked file) don't count. Two identical, deterministic failures do.
An honest note on sources: what I wrote down at the time was one line, "ESM can't resolve a package that npx provides," plus the workaround below. The fuller explanation in the next two sections comes from reading the npx and Node docs while writing this post.
What npx actually gives you
From the npm docs for npx, about --package:
"will be provided in the
PATHof the executed command"
And about where it comes from, when the package isn't already in your project:
"installed to a folder in the npm cache"
[src:official:https://docs.npmjs.com/cli/v11/commands/npx]
That's the whole contract. npx makes sure the package exists somewhere in the npm cache, and it puts that package's executables on PATH for the process it starts. That's what lets npx some-cli --flag work: your shell (or npx itself) looks up some-cli on PATH, finds the binary in the cache, and runs it.
Notice what's not in that contract: anything about where Node's module loader looks when your code says import 'playwright'. PATH is for finding executables. It's how a shell turns node into a file on disk. It has no role in resolving a bare specifier inside JavaScript.
So npx -p playwright node script.mjs did exactly what it promised. It put Playwright's CLI on PATH, then started node. My script never asked for the CLI. It asked the module loader for a package, and the module loader looked somewhere else.
Where import 'playwright' actually looks
Node's ESM documentation spells out the resolution algorithm. A bare specifier is a name like 'playwright' or 'some-package/sub', as opposed to a relative path or a URL. Bare specifiers go through a package lookup that, paraphrasing the spec's PACKAGE_RESOLVE step, does this:
- Start at the folder of the module doing the import (the spec calls it
parentURL). - Look for
node_modules/playwrightthere. - If it's not there, move to the parent folder and look again.
- Keep going until the filesystem root. If nothing is found, throw Module Not Found.
[src:official:https://nodejs.org/api/esm.html]
The anchor is the importing file's location. Not your current working directory, not PATH, not wherever npx happened to install something. If no folder above the script has node_modules/playwright, the walk reaches the root and fails. That's ERR_MODULE_NOT_FOUND, and it's the correct answer to the question an ES module asks when it imports a package by name.
"But NODE_PATH..."
If you've been writing Node for a while, you might remember NODE_PATH: an environment variable listing extra folders to search for modules. CommonJS still honors it. The modules docs say that if NODE_PATH is set, Node "will search those paths for modules if they are not found elsewhere" [src:official:https://nodejs.org/api/modules.html].
ES modules don't. The ESM docs have a short section titled "No NODE_PATH":
"
NODE_PATHis not part of resolvingimportspecifiers. Please use symlinks if this behavior is desired."
[src:official:https://nodejs.org/api/esm.html]
So even an approach that points NODE_PATH at the npx cache would not help an .mjs file. I didn't check whether npx sets NODE_PATH at all; for an ES module it wouldn't matter either way.
This is the gap in one sentence: npx extends the executable lookup, ESM uses the folder-walk lookup, and the only global escape hatch that used to bridge them (NODE_PATH) isn't part of ESM resolution.
What I did instead
Knowing what I know now, there are two directions: put Playwright somewhere on that walk, or stop using a bare specifier so there's no walk at all.
What I did at the time was the second one. ESM accepts absolute specifiers. The docs' example is 'file:///opt/nodejs/config.js', described as referring "directly and explicitly to a full path" [src:official:https://nodejs.org/api/esm.html]. An absolute specifier skips package resolution entirely. Node just loads that file.
Playwright was already on disk. npx had installed it into the npm cache at some point. I just needed to know where. I located the actual playwright package folder inside the npx part of the cache with find. I didn't save the exact invocation. The shape is a name search under the cache's _npx folder, something like this (illustrative, not the logged command):
find "<npm cache>/_npx" -type d -name playwright -path "*node_modules/playwright" Then I imported Playwright by an absolute path into that folder, and it worked. No npx needed to make the package visible, which is what the title means: the fix was to stop relying on npx and point at the file directly.
I don't have that screenshot script's exact code in my notes. But the same day I wrote another small script, the one that renders cover images for these posts, and it uses the same pattern. This is its import line as it sits today, with my user folder and the cache folder's hash replaced:
import { chromium } from 'file:///C:/Users/<you>/AppData/Local/npm-cache/_npx/<hash>/node_modules/playwright/index.mjs' A detail worth knowing if you're on Windows: I used the file:///C:/... URL form rather than a bare C:\... path. The Node docs recommend converting paths with url.pathToFileURL when you import a path, because URL and path rules differ in places like percent-encoding [src:official:https://nodejs.org/api/esm.html]. The line above is a hand-written URL. Building it with pathToFileURL is what the docs suggest; I haven't tried that version.
Then the fix broke on its own
Some weeks later I went to make a cover image and the Playwright copy in the npx cache was gone. The cover script's import pointed at a path that no longer existed. I didn't fix it. I rendered the same HTML with headless Chrome another way and moved on. That's how this post's cover was made, too.
In hindsight this was predictable, and I should have written it down at the time I made the switch:
- The npm cache is a cache. The npx docs describe it as the install location and don't promise anything about how long things stay there. Anything that clears or prunes the cache can remove the folder my script depended on.
- The folder name was not chosen by me. The
<hash>segment is something npm picks. I didn't look into what it's derived from, so I can't tell you when it would change. I only know I was hardcoding a name I didn't control. - The dependency was invisible. The repository the script lives in has a
package.jsonat its root, but Playwright isn't listed in it today. Nothing declares "this script needs Playwright." The only record is a long string inside an import statement.
So the fix was real, and it worked, but it swapped one problem (Node can't find the package) for a quieter one (Node can find exactly one copy of the package, in a place that can disappear without warning). I'd call it a good diagnostic step and a poor permanent solution.
There's a smaller safety point too. A hardcoded import into a cache folder runs whatever is in that folder. And per the same docs, a missing package gets "installed to a folder in the npm cache" when npx runs, so that folder is filled by npx, not by anything I reviewed or pinned. For a screenshot script on my own machine, that risk is small. For anything that runs with real credentials in its environment, I wouldn't want to load code from a folder nothing in the project pins.
What I would try now (all untested)
Everything in this section is what I'd reach for next time. I have not tried any of these for this script, so treat them as options to evaluate, not results.
1. Make it a tiny project with a real dependency (untested here).
Install Playwright as a dev dependency in the folder's package.json. Then node_modules/playwright sits directly on the folder walk that the ESM resolver performs, a bare import 'playwright' works, and the dependency is written down in a lockfile. This is the boring answer, and it fixes both problems: resolution and invisibility. The cost is one more dependency to install and keep updated. Given what happened, that cost looks small.
2. createRequire anchored somewhere else (untested).
module.createRequire(filename) builds a CommonJS-style require whose lookup starts from the path you give it. The docs say the argument "Must be a file URL object, file URL string, or absolute path string" [src:official:https://nodejs.org/api/module.html]. In principle you could anchor it at a folder that does have Playwright in its node_modules. But that's still a hardcoded location; if the location is the npx cache, I'd have the same disappearing-folder problem with extra steps. I'd only consider it if the anchor were a stable, deliberately installed location.
3. A symlink, as the Node docs suggest (untested).
The "No NODE_PATH" section says to use symlinks if you want that behavior. A node_modules/playwright link next to the script, pointing at a real install, would put the package on the walk without copying it. Pointing it at the npx cache would inherit the same fragility, so this is only as good as the target.
4. Use the resolver itself as a diagnostic (untested).
Before changing anything, I'd like to have asked Node directly where it thinks 'playwright' resolves from the script's location. I haven't looked up or tried the API for doing that, so I won't name one or describe its output. The point is to get the answer from the resolver instead of from my mental model of the resolver, which is the thing that was wrong.
5. NODE_PATH: no.
Per the ESM docs quoted above, it isn't part of import resolution, so it isn't an option for an .mjs file at all.
The part I actually took away
The technical lesson is narrow and worth having: PATH resolves executables, the ESM loader resolves packages by walking up from the importing file, and npx only touches the first. If you remember that, npx -p some-lib node script.mjs will look wrong to you before you run it.
The process lesson is the one I use more often.
Stop at the second identical failure. I didn't keep trying variations; I stopped at the second failure and switched approach. The one-line note I kept was right, but it was a conclusion, not an explanation. Writing down which lookup was failing would have made the later breakage easier to see coming.
When you switch approaches, write down what the new approach depends on. This is the half I skipped. "Imports Playwright from the npx cache at this exact path" is a dependency with an expiry date. One line next to the import would have explained the later breakage immediately.
A workaround that works is still a workaround. "It runs now" is a fine place to stop only if you also write down what would make it stop running.
Checklist for ERR_MODULE_NOT_FOUND with npx
- Is your code importing the package, or running its CLI? npx helps with the CLI. It doesn't change where
importlooks. - Where is the importing file? ESM looks for
node_modules/<name>starting from that file's folder and walking up. Your working directory andPATHdon't matter. - Is the file ESM?
.mjs, or"type": "module"in the nearestpackage.json, meansNODE_PATHis ignored forimport. - Did the same error come back twice? Stop varying the command. Read what each tool actually promises.
- If you hardcode a path to make it work, write down why and what it depends on. Especially if that path is inside a cache.
- Prefer a declared dependency over a discovered one. A
package.jsonentry survives cache cleanups. A path you found withfindmight not.
I'm building an AI code reviewer (getdevreview.com) that tries to point out the issues that matter instead of everything it can find. There's a free tier if you want to try it on a pull request. If you've hit the npx-versus-import gap and solved it in a cleaner way, I'd like to hear how.
