在运行时确定节点模块位置 [英] Determining Node modules location at runtime

查看:53
本文介绍了在运行时确定节点模块位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基本上是 node.js 模块的工具.这个工具可以全局安装(使用 -g 选项)我在模块中有几个静态文件来生成报告.如果模块在本地调用,我可以参考相对路径./node_modules//static/filename的静态文件.但是当工具作为命令调用时,我如何引用静态文件?以及如何确定该工具是作为本地模块调用还是作为命令调用?

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屋!

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