适用于Windows和Mac的Node.js —正斜杠,反斜杠纠正

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

是否存在纠正从Windows到Linux和Mac的node.js中关于反斜杠与正斜杠的差异的方法?

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

Windows在调用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.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上:

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

发布评论

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

>www.elefans.com

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