Reference File relative to installed location

时间:2016-04-04 18:20:09

标签: node.js

I have a node utility I am writing where I have index.js and results.hbs in the root of the project. I installed this utility globally using npm link but when I run it from anywhere other than the project folder it can't find results.hbs. How do I reference results.hbs relative to the installed location rather than cwd?

1 个答案:

答案 0 :(得分:1)

You can reference any file relative to a library. So, if your library is called mylib you can find it's path like this

var mylibPath = require.resolve('mylib')

Then you can use that to reference any file in the library Or if you need to just require something relative to a library you can do it like this

require('mylib/myfile')