简体   繁体   English

使用Node.js process.cwd()向上移动目录

[英]Moving up a directory using Node.js process.cwd()

Not sure if I have ever figured out a good workaround for this. 不知道我是否曾经为此找到一个好的解决方法。

I am in a directory, I want to use process.cwd() to find the currently working directory but want to move up on directory, something like so: 我在目录中,我想使用process.cwd()查找当前正在工作的目录,但想向上移动目录,如下所示:

var serverPath = path.resolve(process.cwd() + '../bin/www');

however, this obviously won't work, and will give me an error along these lines: 但是,这显然行不通,并且会在以下方面给我一个错误:

Error: Cannot find module '/Users/amills001c/WebstormProjects/ORESoftware/suman../bin/www'

so what is the best way to use process.cwd() and move up a directory? 那么使用process.cwd()并向上移动目录的最佳方法是什么?

您只需要在..之前加一个/

var serverPath = path.resolve(process.cwd() + '/../bin/www');

You're missing a \\ . 您缺少\\

You should consider calling path.join(process.cwd(), '../bin/www'); 您应该考虑调用path.join(process.cwd(), '../bin/www'); before you pass it to path.resolve . 在将其传递给path.resolve This will help get the slashes correct. 这将有助于使斜杠正确。 Concatenation of file paths is generally considered risky business. 文件路径的串联通常被认为是有风险的业务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM