📆 Don't miss our Sanity Showcase virtual meetup on March 20th!

SyntaxError: Cannot Use Import Statement Outside a Module

7 replies
Last updated: Apr 30, 2021
user A
trying to fire up your awesome translation gist - where should this file live in the sanity project ? Trying to run as a script in the root and getting errors https://gist.github.com/bjoerge/6c0b5bdcd316fb3631f48bdaefaf1456

import client from 'part:@sanity/base/client'
^^^^^^

SyntaxError: Cannot use import statement outside a module
Apr 21, 2021, 10:54 PM
Figured it out - need to run using
sanity exec
Apr 21, 2021, 11:26 PM
đź‘Ť
Apr 22, 2021, 10:53 AM
I think the code expects there to be a
document.field._type
field that indicates which type of field it is, but it's not included in the data returned from the
/export
endpoint: https://gist.github.com/bjoerge/6c0b5bdcd316fb3631f48bdaefaf1456#file-translate-js-L53
Can anyone from the sanity side speak to whether or not this assumption is correct or if I'm missing something?
Apr 28, 2021, 5:35 PM
user M
so I definitely am using field level translations (per that article) - at least I think I am : ) for whatever reason I'm not seeing any other fields beyond the value in the output (on the field level)
here's the schema of the document I'm trying to localize:

import { MdLocalMovies as icon } from 'react-icons/md'

export default {
  name: 'film',
  title: 'Film',
  type: 'document',
  icon,
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'localeString',
      validation: Rule => Rule.required()
    },
    {
      name: 'filmId',
      title: 'Film Id',
      type: 'string'
    },
    {
      name: 'latinTitle',
      title: 'Latin Title',
      type: 'string'
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'latinTitle',
        maxLength: 200 // will be ignored if slugify is set
      },
      validation: Rule => Rule.required()
    },
    {
      name: 'releaseDate',
      title: 'Release date',
      type: 'number'
    },
    {
      name: 'subject',
      title: 'Subject',
      type: 'localeString',
    },
    {
      name: 'productionId',
      title: 'Production ID',
      type: 'string'
    },
    {
      name: 'type',
      title: 'Type',
      type: 'localeString',
    },
    {
      name: 'abstract',
      title: 'Abstract',
      type: 'localeString',
    },
    {
      title: 'Keywords',
      name: 'keywords',
      type: 'localeArray'
    },
    {
      title: 'Latin Keywords',
      name: 'latinKeywords',
      type: 'array',
      of: [{ type: 'string' }]
    },
    {
      name: 'studio',
      title: 'Studio',
      type: 'localeString',
    },
    {
      name: 'director',
      title: 'Director',
      type: 'localeString'
    },
    {
      name: 'operators',
      title: 'Operators',
      type: 'localeString'
    },
    {
      name: 'otherCreators',
      title: 'Other Creators',
      type: 'localeString'
    },
    {
      name: 'sound',
      title: 'Sound',
      type: 'string'
    },
    {
      name: 'color',
      title: 'Color',
      type: 'string'
    },
    {
      name: 'numberOfParts',
      title: 'Number of Parts',
      type: 'number'
    },
    {
      name: 'meterage',
      title: 'Meterage',
      type: 'string'
    },
    {
      name: 'oldUrl',
      title: 'Old URL',
      type: 'url',
      readOnly: true
    }
  ]
}
and the localeString schema:

import {SUPPORTED_LANGUAGES} from './languages'

export default {
    type: 'object',
    name: 'localeString',
    fields: SUPPORTED_LANGUAGES.map((lang) => ({
      name: lang.id,
      type: 'string',
      title: lang.title,
    })),
  }
Apr 30, 2021, 7:22 PM
user M
so I definitely am using field level translations (per that article) - at least I think I am : ) for whatever reason I'm not seeing any other fields beyond the value in the output (on the field level)
here's the schema of the document I'm trying to localize:

import { MdLocalMovies as icon } from 'react-icons/md'

export default {
  name: 'film',
  title: 'Film',
  type: 'document',
  icon,
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'localeString',
      validation: Rule => Rule.required()
    },
    {
      name: 'filmId',
      title: 'Film Id',
      type: 'string'
    },
    {
      name: 'latinTitle',
      title: 'Latin Title',
      type: 'string'
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'latinTitle',
        maxLength: 200 // will be ignored if slugify is set
      },
      validation: Rule => Rule.required()
    },
    {
      name: 'releaseDate',
      title: 'Release date',
      type: 'number'
    },
    {
      name: 'subject',
      title: 'Subject',
      type: 'localeString',
    },
    {
      name: 'productionId',
      title: 'Production ID',
      type: 'string'
    },
    {
      name: 'type',
      title: 'Type',
      type: 'localeString',
    },
    {
      name: 'abstract',
      title: 'Abstract',
      type: 'localeString',
    },
    {
      title: 'Keywords',
      name: 'keywords',
      type: 'localeArray'
    },
    {
      title: 'Latin Keywords',
      name: 'latinKeywords',
      type: 'array',
      of: [{ type: 'string' }]
    },
    {
      name: 'studio',
      title: 'Studio',
      type: 'localeString',
    },
    {
      name: 'director',
      title: 'Director',
      type: 'localeString'
    },
    {
      name: 'operators',
      title: 'Operators',
      type: 'localeString'
    },
    {
      name: 'otherCreators',
      title: 'Other Creators',
      type: 'localeString'
    },
    {
      name: 'sound',
      title: 'Sound',
      type: 'string'
    },
    {
      name: 'color',
      title: 'Color',
      type: 'string'
    },
    {
      name: 'numberOfParts',
      title: 'Number of Parts',
      type: 'number'
    },
    {
      name: 'meterage',
      title: 'Meterage',
      type: 'string'
    },
    {
      name: 'oldUrl',
      title: 'Old URL',
      type: 'url',
      readOnly: true
    }
  ]
}
and the localeString schema:

import {SUPPORTED_LANGUAGES} from './languages'

export default {
    type: 'object',
    name: 'localeString',
    fields: SUPPORTED_LANGUAGES.map((lang) => ({
      name: lang.id,
      type: 'string',
      title: lang.title,
    })),
  }
Apr 30, 2021, 7:22 PM
Ended up giving up and writing my own script that's a bit more simple, but curious to know what I was doing wrong
Apr 30, 2021, 7:23 PM
Ended up giving up and writing my own script that's a bit more simple, but curious to know what I was doing wrong
Apr 30, 2021, 7:23 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?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
How to turn PortableText into plain text in Javascript?Dec 7, 2020
Rendering nested block contentJan 18, 2021
DOMParser is not defined at defaultParseHtmlDec 3, 2020
Next.js and Vercel - Error! No Output Directory named "dist" foundAug 8, 2022
List Arrays - Is it Possible to Fill a List Array Within a Schema Asynchronously?Mar 16, 2021
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
TypeError: Cannot Read Properties of Null (Reading 'jsonType')Sep 28, 2022
Error in custom validation rule for schema in Slack threadAug 18, 2023
How to populate a list of values in a Sanity schema using a separate fileSep 6, 2021
Hi all! Does anyone have problem on deoplloying on Vercel (Using Next. js as a frontend)? I have two test projects, andonce...Jan 25, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle