简体   繁体   English

如何在 npm 模块可执行文件中使用静态资产?

[英]How to use static assets with npm module executable?

Is there any way to have executable in Node.js NPM module (exectuable is located in completely different place than the module), to have additional file (or directory with files) that the exectutable can access?有没有办法在 Node.js NPM 模块中拥有可执行文件(可执行文件位于与模块完全不同的位置),以便拥有可执行文件可以访问的附加文件(或包含文件的目录)?

This is same problem as in Open file from npm module from executable but phrased differently, I have REPL and I want to load files that are in the module from which executable came from.这与Open file from npm module from executable 中的问题相同,但措辞不同,我有 REPL,我想加载可执行文件来自的模块中的文件。

This should be simple and common task but I'm not able to find solution, maybe you know some package that have binary with assets, looking at the source I would be able to figure this out.这应该是简单而常见的任务,但我无法找到解决方案,也许您知道一些带有资产的二进制文件的包,查看源代码我将能够弄清楚这一点。

Found how to do that when needed this again.找到了再次需要时如何做到这一点。 Looking at docs for module 查看模块的文档

I've found function:我找到了功能:

require.resolve(request[, options]) require.resolve(request[, options])

Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.使用内部 require() 机制查找模块的位置,但不加载模块,只需返回解析的文件名。

so to load static assets you only need this:所以要加载静态资产,你只需要这个:

function boostrap() {
    var path;
    try {
        path = require.resolve('./examples/helpers.lips');
    } catch (e) {
        path = require.resolve('@jcubic/lips/examples/helpers.lips');
    }
    var data = fs.readFileSync(path);
    return run(data, env);
}

I need to test two cases and if I can that funciton with module name and I have the source code it will throw the same as if I call as local file and I'm not in source.我需要测试两种情况,如果我可以使用模块名称的函数并且我有源代码,它会抛出相同的情况,就像我作为本地文件调用而我不在源代码中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM