Sanity CLI import fails with relative file paths for assets

14 replies
Last updated: Sep 6, 2021
I’m still (reported this a few months ago) having issues with export/import using the CLI. All imports fail with the same error
Error: Error while fetching asset from "file://./images/<image-name>.<ext>":
File does not exist at the specified endpoint
If I change that path to be absolutely defined from the root of my computer (
/Users/<username>/path/to/sanity/project/export/images/<image-name>.ext
) it works.
Seems strange that I have to go and do that for all assets (not only images, files are affected in the same way). Does this only happen to me?
AI Update

You're definitely not alone with this issue! This is a known behavior with the Sanity CLI's dataset import command when handling asset file paths. Here's what's happening and how to work around it:

The Problem

When you export a dataset using sanity dataset export, the resulting archive contains asset references with relative paths like file://./images/<image-name>.<ext>. However, when importing, the CLI sometimes fails to resolve these relative paths correctly, leading to the "File does not exist at the specified endpoint" error you're seeing.

According to the official Sanity documentation on this error, local file paths must be absolute, not relative.

Solutions

1. Use Absolute Paths (Your Current Workaround)

As you've discovered, using absolute paths is currently the most reliable solution. The documentation confirms this:

Correct: image@file:///Users/username/path/to/sanity/project/export/images/image-name.ext
Wrong: image@file://./images/image-name.ext

While it's frustrating to manually convert paths, this is the approach that consistently works.

2. Import the Tarball Instead of NDJSON

The importing data documentation specifically mentions this issue:

If you're getting an import error like Error: Error while fetching asset from "file://./images/<image-name>.<ext>": File does not exist at the specified endpoint, you can either (1) make the filenames absolute or (2) import a tarball (including assets) rather than an ndjson file.

So instead of extracting and importing just the .ndjson file, try importing the compressed tarball directly:

sanity dataset import production.tar.gz your-dataset

This keeps the assets bundled with the document data and should handle paths more reliably.

3. Run Import from the Export Directory

Make sure you're running the sanity dataset import command from the same directory where the export files are located. The relative paths are resolved relative to your current working directory.

4. Use the --allow-failing-assets Flag

If some missing assets are acceptable for your use case, you can use:

sanity dataset import your-export.tar.gz your-dataset --allow-failing-assets

This allows the import to continue even when some assets can't be found.

5. Automate Path Conversion

If you're doing frequent exports/imports, consider writing a script to automatically convert relative paths to absolute ones in your export files. You could extract the tarball, modify the .ndjson file with a find-and-replace script, and re-import.

Why This Happens

The CLI's handling of relative file paths in asset references has been inconsistent across different operating systems and execution contexts. The path resolution logic doesn't always correctly interpret ./ relative to the archive location or the current working directory.

Bottom Line

This is a known limitation of the import tool, not something you're doing wrong. Your workaround of using absolute paths is actually the officially recommended approach. For the most reliable imports, stick with importing the full tarball rather than extracted NDJSON files, or use absolute paths when you need more control.

Show original thread
14 replies
@sanity/cli version 2.16.0
Have you tried importing the .tar.gz file?
Or are you unzipping, and then importing the .ndjson file?
unzipping, the docs say to import the
data.ndjson
file.
Actually, this page says to import
data.ndjson
https://www.sanity.io/docs/importing-data and this one just says to import
[FILE | FOLDER | URL]
https://www.sanity.io/docs/migrating-data
Sorry this has caused issues for a few months, User. My understanding of export/import is that pulling in the
.ndjson
file directly would be useful if making a batch change to content, for example, but that in most use cases one would want to pull in everything. On the Migrating Data page, there’s a line at the end of the Gotcha—“Make sure though to bring your assets along for the ride if this is the workflow you have chosen”—that needs to be much more prominent.
I’d be curious to know if User’s suggestion to import the whole tarball works for you. I’d like to make these two pages clearer and include a more complete provision of use cases.
Ah User, don’t sweat it! It’s just been months since I last tried so it’s not like I’ve suffered for months 😂
Hmm, that doesn’t really say anything at all that would help
😅 There’s no definition of how I’d bring along the data for the ride. Also, I haven’t even seen anywhere that you can import the tarball directly so not sure what I’ve missed here but I didn’t even know it was possible until today.
I make batch changes every now and then, but with enough time in between for me to forget how
😄 (today was one of those days), like if I change a type for example which would be relatively common I imagine.
Hmm, that doesn’t really say anything at all that would help  There’s no definition of how I’d bring along the data for the ride.
Sorry, prominent was the wrong word. You’re absolutely right that they need to be elaborated on, which I’ll do this week. I’ll ping you once I’m done as I’d like to invite your feedback, if that works for you.
Of course mate! Love to help
How did you go with the
.tar.gz
import? I used to run into this myself until I found out you could import the export bundle itself.
I didn't get a chance to try it, was pretty late over here. I already got the import working by changing all the file paths to start from the root. I've got another project that needs a migration between datasets this week as well so I'll try that then and let you know
Both https://www.sanity.io/docs/migrating-data and https://www.sanity.io/docs/importing-data have been updated—hopefully to clarify what is possible and improve the experience. User or anyone else, I’d welcome any feedback.
Thanks User, I'll check it out tomorrow
Awesome
user A
, that will be very helpful! Looks like it covers everything now. 🙏
Thanks for reviewing those, User. I appreciate that you pointed these out and hope you'll continue to do so. 🙌

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?