在运行时确定节点模块位置 [英] Determining Node modules location at runtime
问题描述
我正在构建一个基本上是 node.js 模块的工具.这个工具可以全局安装(使用 -g
选项)我在模块中有几个静态文件来生成报告.如果模块在本地调用,我可以参考相对路径./node_modules/
的静态文件.但是当工具作为命令调用时,我如何引用静态文件?以及如何确定该工具是作为本地模块调用还是作为命令调用?
I am building a tool which is basically a node.js module. This tool can be installed globally (with -g
option) I have few static files in the module to generate a report. If the module is invoked locally, I can refer to the static files with relative path ./node_modules/<module>/static/filename
. But when the tool is invoked as a command, how do I refer to the static files? And how can I determine whether the tool is invoked as a local module or as a command?
推荐答案
使用魔术变量 __dirname.它指的是包含您的脚本文件的目录.
Use the magic variable __dirname. It refers to the directory containing your script file.
http://nodejs.org/api/globals.html#globals_dirname
这篇关于在运行时确定节点模块位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!