适用于 Windows 和 Mac 的 Node.js — 正斜杠、反斜杠校正 [英] Node.js for Windows and Macs — forward slash, backslash rectification

查看:59
本文介绍了适用于 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 — 正斜杠、反斜杠校正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆