How to use the pathToFileURL function from url

Find comprehensive JavaScript url.pathToFileURL code examples handpicked from public code repositorys.

url.pathToFileURL is a Node.js built-in module function that converts a file path to a URL object.

175
176
177
178
179
180
181
182
183
184
let parentURL;
try {
  const { pathToFileURL } = require('url');
  // Adding `/repl` prevents dynamic imports from loading relative
  // to the parent of `process.cwd()`.
  parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href;
} catch {
}

// Remove all "await"s and attempt running the script
fork icon1
star icon1
watch icon1

+ 3 other calls in file

321
322
323
324
325
326
327
328
329
330
  return cb(null);

let pwd;
try {
  const { pathToFileURL } = require('url');
  pwd = pathToFileURL(process.cwd()).href;
} catch {
}
while (true) {
  try {
fork icon0
star icon1
watch icon2

+ 3 other calls in file

How does url.pathToFileURL work?

The url.pathToFileURL method is used to create a file: URL object from a file path in a platform-independent way, by converting the given path to a file URL string and returning it as a URL object.

The method takes a single argument which is the file path string to be converted to a file: URL.

Note that this method is available in Node.js version 10.12.0 or later, and in modern web browsers.

234
235
236
237
238
239
240
241
242
243
sourcePath(node) {
  if (this.mapOpts.from) {
    return this.toUrl(this.mapOpts.from)
  } else if (this.mapOpts.absolute) {
    if (pathToFileURL) {
      return pathToFileURL(node.source.input.from).toString()
    } else {
      throw new Error(
        '`map.absolute` option is not available in this PostCSS build'
      )
fork icon0
star icon0
watch icon1

182
183
184
185
186
187
188
189
190
191
if (isAbsolute(from.source)) {
  fromUrl = pathToFileURL(from.source)
} else {
  fromUrl = new URL(
    from.source,
    this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
  )
}

let result = {
fork icon0
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
const { pathToFileURL } = require("url");

const filePath = "/path/to/my/file.txt";
const fileUrl = pathToFileURL(filePath);

console.log(fileUrl.href); // output: "file:///path/to/my/file.txt"

In this example, the pathToFileURL() method from the url module is used to convert the filePath to a file URL. The resulting fileUrl variable contains the file URL, which is then printed to the console using the href property.

232
233
234
235
236
237
238
239
240
241
  return encodeURI(path).replace(/[#?]/g, encodeURIComponent)
}

toFileUrl(path) {
  if (pathToFileURL) {
    return pathToFileURL(path).toString()
  } else {
    throw new Error(
      '`map.absolute` option is not available in this PostCSS build'
    )
fork icon0
star icon0
watch icon1

+ 3 other calls in file

253
254
255
256
257
258
259
260
261
262
let response = { status: 599, ok: false, headers: {} };
let s;
let ok;

if (argv.cached) {
  u = url.pathToFileURL(argv.cached).toString();
}

if (argv.provider && argv.provider.data && argv.provider.data.length) {
  ng.logger.prepend('S');
fork icon0
star icon0
watch icon1

55
56
57
58
59
60
61
62
63
64
    throw new Error('Cannot assign to a client module from a server module.');
  },
};

(require: any).extensions['.client.js'] = function(module, path) {
  const moduleId = url.pathToFileURL(path).href;
  const moduleReference: {[string]: any} = {
    $$typeof: MODULE_REFERENCE,
    filepath: moduleId,
    name: '*', // Represents the whole object instead of a particular import.
fork icon0
star icon0
watch icon1