question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: url.parse is not a function

See original GitHub issue

Hi there,

I have been stuck on this thing for a week now. I am trying to fetch data within a Vue (Vite) project, doing it exactly as this guide is showing: https://www.sanity.io/guides/create-a-single-page-application-with-vuejs-and-sanity

But I am getting an error called TypeError: url.parse is not a function while trying to fetch from my dataset.

Using:

This is what I am trying:

projects.vue

<script>
import { onMounted, ref } from 'vue'

// sanity
import imageUrlBuilder from '@sanity/image-url'
import sanityConfig from '../../sanity-config'

const imageBuilder = imageUrlBuilder(sanityConfig)

export default {
  setup() {
    onMounted(() => {
      fetchProjects()
    })

    const groqQuery = `*[_type=="projects"]{
                        _id,
                        title
                      }[0...50]`

    const projects = ref([])
    let error = ref()

    const imageUrlFor = (source) => {
      return imageBuilder.image(source)
    }

    function fetchProjects() {
      console.log(sanityConfig)
      sanityConfig.fetch(groqQuery).then(
        (projectList) => {
          projects.value = projectList
          console.log(projects.value)
        },
        (errorList) => {
          error = errorList
          console.log(error)
        },
      )
    }

    return {
      projects,
      imageUrlFor,
    }
  },

}
</script>

FYI, console.log(sanityConfig) returns: image

sanity-config.js

import sanityClient from '@sanity/client'

export default sanityClient({
  projectId: 'abCDef', // masked projectId
  dataset: 'production',
  apiVersion: '2021-06-07',
  useCdn: true,
  // useCdn: process.env.NODE_ENV === 'production',
})

The following error occurs in my console:

TypeError: url.parse is not a function
    at module.exports (index.js:10)
    at module.exports (browser-request.js:20)
    at index.js:57
    at Object.publish (index.js:18)
    at SanityObservableMinimal._subscribe (observable.js:22)
    at SanityObservableMinimal.Observable2._trySubscribe (Observable.ts:238)
    at SanityObservableMinimal.Observable2.subscribe (Observable.ts:219)
    at FilterOperator2.call (filter.ts:71)
    at SanityObservableMinimal.Observable2.subscribe (Observable.ts:214)
    at MapOperator2.call (map.ts:59)

index.js:10 is within this file: /node_modules/.pnpm/same-origin@0.1.1/node_modules/same-origin/index.js - which contains:

'use strict';

var url = require('url');

module.exports = function(uri1, uri2, ieMode) {
    if (uri1 === uri2) {
        return true;
    }

    var url1 = url.parse(uri1, false, true);
    var url2 = url.parse(uri2, false, true);

    var url1Port = url1.port|0 || (url1.protocol === 'https' ? 443 : 80);
    var url2Port = url2.port|0 || (url2.protocol === 'https' ? 443 : 80);

    var match = {
        proto: url1.protocol === url2.protocol,
        hostname: url1.hostname === url2.hostname,
        port: url1Port === url2Port
    };

    return ((match.proto && match.hostname) && (match.port || ieMode));
};

What am I doing wrong… I updated everything and followed several guides, amongst the one mentioned above.

Thank you for your time!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:12

github_iconTop GitHub Comments

39reactions
jmrooncommented, Jan 24, 2022

This appears to happen because of one of Sanity’s dependencies (same-origin) requires a node built-in: url.

Easiest way to get around it: add url to your dependencies.

npm install url or yarn add url

Another option is to add a polyfill to both esbuild and rollup, but that’s more involved and I’m not sure if there’s much benefit: https://medium.com/@ftaioli/using-node-js-builtin-modules-with-vite-6194737c2cd2

Ideally, sanity can remove the dependency on same-origin, as it’s a rather small package to rewrite without a node built-in. Alternatively, if they provided url as a required dependency, that would be a quick patch in the meantime.

5reactions
Husain01commented, Sep 6, 2022

This appears to happen because of one of Sanity’s dependencies (same-origin) requires a node built-in: url. Easiest way to get around it: add url to your dependencies. npm install url or yarn add url Another option is to add a polyfill to both esbuild and rollup, but that’s more involved and I’m not sure if there’s much benefit: https://medium.com/@ftaioli/using-node-js-builtin-modules-with-vite-6194737c2cd2 Ideally, sanity can remove the dependency on same-origin, as it’s a rather small package to rewrite without a node built-in. Alternatively, if they provided url as a required dependency, that would be a quick patch in the meantime.

Hi Brother, I am using React with Vite and Sanity, still facing the same problem with Sanity, nothing is working, I have installed url with npm still same problem persists. Please help me.

Yes it doesn’t seem to be working with React in Vite

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use URL module on Node.js, cannot call method 'parse ...
I found the problem, I had the code like this: var http = require("http"); var url = require('url'); http.createServer(function(request ...
Read more >
url-parse - npm
Small footprint URL parser that works seamlessly across Node.js and browser environments. Latest version: 1.5.10, last published: 10 months ...
Read more >
URL | Node.js v19.3.0 Documentation
The node:url module provides utilities for URL resolution and parsing. ... A TypeError will be thrown if the input or base are not...
Read more >
TypeError: "x" is not a function - JavaScript - MDN Web Docs
Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function?...
Read more >
url.parse JavaScript and Node.js code examples - Tabnine
http.createServer(function (req, res) { var url = URL.parse(req.url);
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found