✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Troubleshooting importing HTML content into Sanity using @sanity/block-tools

3 replies
Last updated: Jul 23, 2021
Hi all, I’m new to sanity. Trying to import some existing blog content (in HTML) into sanity using
@sanity/block-tools
. I have found a great blog post about how you can use the CLI for this.I’m trying to reference the
portableText
type to convert my HTML to in my script. But I just cannot figure out what’s going wrong when running the tooling. Just this simple script:
// script/blog-migration.js

import mySchema from "../../schemas/schema";

const article = { contentHtml: "<p>test</p>" }

const blockContentType = defaultSchema.get("portableText");
const blocks = blockTools.htmlToBlocks(
  article.contentHtml,
  blockContentType
);
Fails with the following error when running `npx sanity exec script/blog-migration.js`:


/Users/tolgapaksoy/Projects/sanity-project-1/studio/node_modules/@sanity/bifur-client/dist/cjs/index.js:97
        : id, operators_2.shareReplay({ refCount: true }), operators_2.takeUntil(rxjs_1.fromEvent(window, 'beforeunload'))));
                                                                                                  ^

ReferenceError: window is not defined
    at fromUrl (/Users/tolgapaksoy/Projects/sanity-project-1/studio/node_modules/@sanity/bifur-client/dist/cjs/index.js:97:99)
    at fromSanityClient (/Users/tolgapaksoy/Projects/sanity-project-1/studio/node_modules/@sanity/bifur-client/dist/cjs/index.js:102:12)
    at Object.<anonymous> (/Users/tolgapaksoy/Projects/sanity-project-1/studio/node_modules/@sanity/base/lib/client/bifur.js:95:47)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Module._compile (/Users/tolgapaksoy/Projects/sanity-project-1/studio/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Object.newLoader [as .js] (/Users/tolgapaksoy/Projects/sanity-project-1/studio/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
How do I reference my existing schema, without running into this issue of some part of Sanity requiring “window” to be set?
Jul 23, 2021, 3:18 PM
You can try the
--mock-browser-env
arg to sanity exec. I hit this issue at some point and that arg was added.

https://github.com/sanity-io/sanity/pull/2319
Jul 23, 2021, 3:22 PM
Well… that explains a lot. Apparently we’ve scaffolded our project with Sanity v2.2.3 and that command was missing
Jul 23, 2021, 3:25 PM
Thank you very much Matt!
Jul 23, 2021, 3:25 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?