如何知道需要的文件的确切path()

node控制台中,我执行: require('path')require('assert') =>如何准确找出哪个文件被命令加载(文件的绝对path)

我无法在任何地方find决定性的答案,我自己也无法得到答案……我认为这会比看起来更容易…

我不认为这是如你所希望的那么简单,但使用require对象,你可以这样做:

 // Load up some modules var _ = require('lodash'); var natural = require('natural'); // These are where Node will go looking for the modules above console.log('Paths:'); console.log(require.main.paths); // You can print out the id of each module, which is the path to its location console.log('Module IDs:'); require.main.children.forEach((module) => { console.log(module.id); }); 

输出:

 $ node index.js Paths: [ '/Users/tyler/Desktop/js_test/node_modules', '/Users/tyler/Desktop/node_modules', '/Users/tyler/node_modules', '/Users/node_modules', '/node_modules' ] Module IDs: /Users/tyler/Desktop/js_test/node_modules/lodash/lodash.js /Users/tyler/Desktop/js_test/node_modules/natural/lib/natural/index.js 

据我可以告诉模块ID将按照您require的顺序,所以您应该能够使用其索引或dynamicsearch模块ID来寻找你想要的东西。