Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url.pathToFileURL is not a function : how to convert a file path to an URL in node.js? #3846

Closed
raphael10-collab opened this issue Apr 28, 2022 · 4 comments
Labels

Comments

@raphael10-collab
Copy link

raphael10-collab commented Apr 28, 2022

I want to convert a file path to an URL.

I tried to follow the indications found in the official docs: https://nodejs.org/dist/latest-v16.x/docs/api/url.html#urlpathtofileurlpath

import { pathToFileURL } from 'url'

let filepathurl = pathToFileURL(file.path)

But I get this error:

Uncaught TypeError: (0 , _url.pathToFileURL) is not a function

Or :

import url from 'url'
let filepathurl = url.pathToFileURL(file.path)

Error message:

_url.default.pathToFileURL is not a function

Other info:

node: v16.13.0
O.S. : Ubuntu 20.04 Desktop

What am I doing wrongly? How to solve the problem?

At the moment, I solved the problem using https://www.npmjs.com/package/file-url combined with this helpful suggestion for webpack configuration: facebook/create-react-app#11764 (comment)

But I would like to know if and how to correctly use the features of the node.js library

@royadeveloper01
Copy link

I would advice to try this node package. I saw the solution on stackoverflow

https://www.npmjs.com/package/file-url

@nschonni nschonni transferred this issue from nodejs/nodejs.org Apr 28, 2022
@VoltrexKeyva
Copy link
Member

VoltrexKeyva commented Apr 29, 2022

I can't reproduce this, the code snippet looks correct. You're probably using an "incomplete" version of the Node.js binary which can cause the error although I'm unsure how you've got an incomplete version, and the errors don't seem like errors from Node.js itself since Node.js won't point out the name of the module you imported the function from, nor would it prepend a _ (_url) to the front of the module name.

I would recommend uninstalling Node.js and reinstalling it.

Copy link

There has been no activity on this issue for 11 months. The help repository works best when sustained engagement moves conversation forward. The issue will be closed in 1 month. If you are still experiencing this issue on the latest supported versions of Node.js, please leave a comment.

@github-actions github-actions bot added the stale label Apr 12, 2024
@RedYetiDev
Copy link
Member

(Closing stale issue)

@RedYetiDev RedYetiDev closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants