运行节点bin脚本时确定命令行工作目录 [英] Determine command line working directory when running node bin script

查看:126
本文介绍了运行节点bin脚本时确定命令行工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个节点命令行界面。它是全局安装的,并使用bin文件来执行。



我打算在我正在工作的文件的根目录下打开一个命令窗口,然后运行该命令,但是我一直无法确定当前工作目录为 process.cwd()正在返回节点包的目录。我最初假设,因为代码是使用批处理文件作为包装器(这就是如何bin文件可以执行没有节点在开始),那么这是不可能的,但是咖啡脚本设法做到。我看了一下咖啡脚本源代码,但不能跟随它(没有足够的经验)。



要测试它自己创建一个包package.json file:

  {
name:test-package,
version: 1.0.0,
bin:{
test-package:./bin/test-package
},
main:/ lib / test
}

此测试包文件在bin:

 #!/ usr / bin / env node 

var path = require('path');
var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__ filename)),'../lib');

require(lib +'/ test');

任何人都可以为此留出一些光。



,然后尝试在lib / test中获取命令行目录。

解决方案


  • process.cwd()如果应用程序中没有被'process.chdir'更改,则返回已执行命令的目录(不是节点程序包的目录)。

  • __ filename 返回文件的绝对路径。

  • __ dirname 返回 __ filename 目录的绝对路径。



如果您需要从您的模块目录加载文件,您需要使用相对路径。

  require('../ lib /测试'); 

而不是

 code> var lib = path.join(path.dirname(fs.realpathSync(__ filename)),'../lib'); 

require(lib +'/ test');

它总是相对于文件调用,而不依赖于当前工作目录。 p>

I am creating a node command line interface. It is installed globally and uses a bin file to execute.

I plan to have a command window open at the root directory of the files I am working on and then just run the command however I have been unable to determine the current working directory as process.cwd() is returning the directory of the node package. I initially assumed that since the code is being executed using a batch file as a wrapper (that is how bin files can execute without node at the beginning) then it is impossible but coffee-script manages to do it. I took a look at the coffee-script source but couldn't follow it (not experienced enough).

To test it for yourself create a package with this package.json file:

{
  "name": "test-package",
  "version": "1.0.0",
  "bin": {
    "test-package":  "./bin/test-package"
  },
  "main": "/lib/test"
}

this test-package file in bin:

#!/usr/bin/env node

var path = require('path');
var fs   = require('fs');
var lib  = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');

require(lib + '/test');

Could anyone shed some light onto this.

and then try and get the command line directory inside lib/test.

解决方案

  • process.cwd() returns directory where command has been executed (not directory of the node package) if it's has not been changed by 'process.chdir' inside of application.
  • __filename returns absolute path to file where it is placed.
  • __dirname returns absolute path to directory of __filename.

If you need to load files from your module directory you need to use relative paths.

require('../lib/test');

instead of

var lib  = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');

require(lib + '/test');

It's always relative to file where it called from and don't depend on current work dir.

这篇关于运行节点bin脚本时确定命令行工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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