【问题标题】:How can you make file paths 'OS agnostic' in nodejs?如何在 nodejs 中使文件路径“与操作系统无关”?
【发布时间】:2020-04-10 05:54:33
【问题描述】:

我现在正在处理一些代码,这些代码在我们的集成测试中搞砸了,因为我们在 Windows 机器的 EC2 实例上运行测试作业。在解决了文件名太长的问题后,许多路径仍然无法正确解析,因为其中许多路径是针对 UNIX 进行硬编码的。

我正在研究 nodejs 的 Path 模块,但我不确定需要更改哪些内容才能使路径名在两个操作系统上都能正常工作(当然反斜杠除外)。我是否需要为这些 Windows 路径名添加前缀?

这是一些失败的代码:

function getDirectories(srcpath) {
//srcPath looks like graph-validator/tests/data/graph-examples/
  return fs.readdirSync(srcpath).filter(function(file) {
    return (
      file !== '.git' && fs.statSync(path.join(srcpath, file)).isDirectory()
    )
  })
}

const dataDirectory = 'graph-validator/tests/data/'

// Generate valid input for included minimal tests
function createDatasetFileList(path) {

  const testDatasetPath = `${dataDirectory}${path}`
 //path is often 'graph-examples/ds001/' 
  if (!isNode) {

    return createFileList(testDatasetPath)
  } else {
    return testDatasetPath
    // testDatasetPath = graph-validator/tests/data/graph-examples/ds001/

  }
}

我试过path.join(),这似乎部分解决了这个问题,至少是斜线。如何使这些路径名在操作系统中完全不可知?

【问题讨论】:

    标签: javascript node.js path filesystems filenames


    【解决方案1】:

    我认为您正在寻找的是 path.sephttps://nodejs.org/api/path.html#path_path_sep

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2015-07-16
      • 2015-01-15
      • 1970-01-01
      • 2012-06-06
      • 2011-07-10
      相关资源
      最近更新 更多