适用于 Windows 和 Mac 的 Node.js — 正斜杠、反斜杠校正

编程入门 行业动态 更新时间:2024-10-25 22:23:19
本文介绍了适用于 Windows 和 Mac 的 Node.js — 正斜杠、反斜杠校正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有一种方法可以纠正 node.js 从 Windows 到 Linux 和 Mac 关于反斜杠与正斜杠的差异?

Is there a method to rectify the discrepancy in node.js from Windows to Linux and Mac concerning the backslash versus forward slash?

Windows 在 git bash 中调用位置时需要反斜杠,而 Mac/Linux 需要正斜杠.我正在与 Mac 和 Windows 用户一起开发一个项目,因此我无法将代码中的所有正斜杠更改为反斜杠,因为当 Mac 用户拉动时,coffee 将无法为他们正确运行,反之亦然.

Windows requires backslashes when calling locations in git bash, while Mac/Linux requires forward slashes. I'm working on a project with both Mac and Windows users so I can't change all the forward slashes to backslashes in the code because when Mac users pull, coffee wont be able to properly run for them and vice versa.

有解决办法吗?

推荐答案

确保使用 path 方法而不是输入路径.path.normalize() 和 path.join() 在开发跨平台时特别有用:

Make sure to use path methods instead of typing out paths. path.normalize() and path.join() are particularly useful when developing cross platform:

在 Windows 上:

On Windows:

$ node > var p = require('path') undefined > p.normalize('/hey/there/you') '\hey\there\you' > p.join('/hey', 'there', '/you') '\hey\there\you'

在 Linux 上:

$ node > var p = require('path') undefined > p.normalize('/hey/there/you') '/hey/there/you' > p.join('/hey', 'there', '/you') '/hey/there/you'

希望这会有所帮助.

更多推荐

适用于 Windows 和 Mac 的 Node.js — 正斜杠、反斜杠校正

本文发布于:2023-11-28 19:46:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   适用于   Windows   Mac   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!