运行节点bin脚本时确定命令行工作目录

时间:2020-12-13 00:12:31

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

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

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).

我计划在我正在处理的文件的根目录上打开一个命令窗口,然后运行这个命令,但是我无法确定当前的工作目录,因为process.cwd()返回节点包的目录。我最初认为,由于代码是使用批处理文件作为包装器执行的(这就是bin文件在开始时可以在没有节点的情况下执行的方式),所以这是不可能的,但是coffee-script设法做到了这一点。我看了看咖啡脚本的来源,但没能跟上(经验还不够丰富)。

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

要为您自己测试它,请使用这个包创建一个包。json文件:

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

this test-package file in bin:

这个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.

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

3 个解决方案

#1


144  

  • 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.
  • cwd()返回命令已执行的目录(而不是节点包的目录),如果'process '没有更改该目录。目录里面的应用程序。
  • __filename returns absolute path to file where it is placed.
  • __filename返回放置它的文件的绝对路径。
  • __dirname returns absolute path to directory of __filename.
  • __dirname返回到__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.

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

#2


6  

Current Working Directory

To get the current working directory, you can use:

要获取当前的工作目录,可以使用:

process.cwd()

However, be aware that some scripts, notably gulp, will change the current working directory with process.chdir().

但是,请注意,有些脚本(特别是gulp)将使用process.chdir()更改当前工作目录。

Node Module Path

You can get the path of the current module with:

您可以得到当前模块的路径:

  • __filename
  • __filename
  • __dirname
  • __dirname

Original Directory (where the command was initiated)

If you are running a script from the command line, and you want the original directory, from which the script was run, regardless of what directory the script is currently operating in, you can use:

如果您正在从命令行运行一个脚本,并且您想要运行脚本的原始目录,无论脚本当前在哪个目录中操作,您都可以使用:

process.env.INIT_CWD

Original directory, when working with NPM scripts

It's sometimes desirable to run an NPM script in the current directory, rather than the root of the project.

有时希望在当前目录中运行NPM脚本,而不是项目的根目录。

This variable is available inside npm package scripts as:

该变量在npm包脚本中可用,如:

$INIT_CWD.

You must be running a recent version of NPM. If this variable is not available, make sure NPM is up to date.

您必须运行最新版本的NPM。如果这个变量不可用,请确保NPM是最新的。

This will allow you access the current parth in your package.json, e.g.:

这将允许您访问包中的当前parth。json,例如:

scripts: {
  "customScript": "gulp customScript --path $INIT_CWD"
}

#3


4  

Alternatively, if you want to solely obtain the current directory of the current NodeJS script, you could try something simple like this. Note that this will not work in the Node CLI itself:

或者,如果您想单独获取当前NodeJS脚本的当前目录,您可以尝试以下简单的方法。注意,这不会在节点CLI中工作:

var fs = require('fs'),
    path = require('path');

var dirString = path.dirname(fs.realpathSync(__filename));

// output example: "/Users/jb/workspace/abtest"
console.log('directory to start walking...', dirString);

#1


144  

  • 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.
  • cwd()返回命令已执行的目录(而不是节点包的目录),如果'process '没有更改该目录。目录里面的应用程序。
  • __filename returns absolute path to file where it is placed.
  • __filename返回放置它的文件的绝对路径。
  • __dirname returns absolute path to directory of __filename.
  • __dirname返回到__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.

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

#2


6  

Current Working Directory

To get the current working directory, you can use:

要获取当前的工作目录,可以使用:

process.cwd()

However, be aware that some scripts, notably gulp, will change the current working directory with process.chdir().

但是,请注意,有些脚本(特别是gulp)将使用process.chdir()更改当前工作目录。

Node Module Path

You can get the path of the current module with:

您可以得到当前模块的路径:

  • __filename
  • __filename
  • __dirname
  • __dirname

Original Directory (where the command was initiated)

If you are running a script from the command line, and you want the original directory, from which the script was run, regardless of what directory the script is currently operating in, you can use:

如果您正在从命令行运行一个脚本,并且您想要运行脚本的原始目录,无论脚本当前在哪个目录中操作,您都可以使用:

process.env.INIT_CWD

Original directory, when working with NPM scripts

It's sometimes desirable to run an NPM script in the current directory, rather than the root of the project.

有时希望在当前目录中运行NPM脚本,而不是项目的根目录。

This variable is available inside npm package scripts as:

该变量在npm包脚本中可用,如:

$INIT_CWD.

You must be running a recent version of NPM. If this variable is not available, make sure NPM is up to date.

您必须运行最新版本的NPM。如果这个变量不可用,请确保NPM是最新的。

This will allow you access the current parth in your package.json, e.g.:

这将允许您访问包中的当前parth。json,例如:

scripts: {
  "customScript": "gulp customScript --path $INIT_CWD"
}

#3


4  

Alternatively, if you want to solely obtain the current directory of the current NodeJS script, you could try something simple like this. Note that this will not work in the Node CLI itself:

或者,如果您想单独获取当前NodeJS脚本的当前目录,您可以尝试以下简单的方法。注意,这不会在节点CLI中工作:

var fs = require('fs'),
    path = require('path');

var dirString = path.dirname(fs.realpathSync(__filename));

// output example: "/Users/jb/workspace/abtest"
console.log('directory to start walking...', dirString);