Migration API createAsset "ReferenceError: File is not defined"

Describe your question/issue in detail

Using the "@prismicio/migrate": "^0.0.2", package, I'm unable to create Assets. I'm assuming it's something I'm doing wrong but I feel like I've followed the documentation and tutorials.

Impacted feature

Migration API unable to create assets.

What steps have you taken to resolve this issue already?

Tried hardcoding a URL and using the async method detailed here: Migrating to Prismic - Documentation - Prismic.

Errors

As soon as the function is fired, I get "ReferenceError: File is not defined at WriteClient.fetchForeignAsset".

Using Node v18.18.0.

Code

import Posts from './posts.json' assert { type: 'json' };

const writeClient = prismic.createWriteClient(repo, { writeToken: token});
const migration = prismic.createMigration();

const pageDocument = page => {
	if (!page.title) return;

	const featuredImage = page.images.split('|')[0];

	const post = {
		title: page.title,
		uid: page.slug,
		data: {
			heading: page.title,
			description: page.description,			
			image: migration.createAsset(
				new URL(featuredImage),
				featuredImage.split('/').pop(),
				{
					alt: page.image_alt ? page.image_alt.split('|')[0] : '',
				}
			),
		},
		type: 'post',
		lang: 'en-gb',
	};

	migration.createDocument(post, page.title);
};

for (const page of Posts) {
	pageDocument(page);
}

// Execute the prepared migration at the very end of the script
await writeClient.migrate(migration, {
	reporter: event => console.log(event),
});

Hi @dev52, the migration APIs support Node.js 20+, which includes File and a few other necessary globals, like Blob and FormData.

Could you upgrade to Node.js 20 or later and let us know if that fixes the issue?

1 Like