You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resolver.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. var path = require('path');
  2. var fs = require('fs');
  3. var test = require('tape');
  4. var resolve = require('../');
  5. var async = require('../async');
  6. test('`./async` entry point', function (t) {
  7. t.equal(resolve, async, '`./async` entry point is the same as `main`');
  8. t.end();
  9. });
  10. test('async foo', function (t) {
  11. t.plan(12);
  12. var dir = path.join(__dirname, 'resolver');
  13. resolve('./foo', { basedir: dir }, function (err, res, pkg) {
  14. if (err) t.fail(err);
  15. t.equal(res, path.join(dir, 'foo.js'));
  16. t.equal(pkg && pkg.name, 'resolve');
  17. });
  18. resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
  19. if (err) t.fail(err);
  20. t.equal(res, path.join(dir, 'foo.js'));
  21. t.equal(pkg && pkg.name, 'resolve');
  22. });
  23. resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  24. if (err) t.fail(err);
  25. t.equal(res, path.join(dir, 'foo.js'));
  26. t.equal(pkg && pkg.main, 'resolver');
  27. });
  28. resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  29. if (err) t.fail(err);
  30. t.equal(res, path.join(dir, 'foo.js'));
  31. t.equal(pkg.main, 'resolver');
  32. });
  33. resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
  34. if (err) t.fail(err);
  35. t.equal(res, path.join(dir, 'foo.js'));
  36. });
  37. resolve('foo', { basedir: dir }, function (err) {
  38. t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
  39. t.equal(err.code, 'MODULE_NOT_FOUND');
  40. });
  41. // Test that filename is reported as the "from" value when passed.
  42. resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
  43. t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
  44. });
  45. });
  46. test('bar', function (t) {
  47. t.plan(6);
  48. var dir = path.join(__dirname, 'resolver');
  49. resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
  50. if (err) t.fail(err);
  51. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  52. t.equal(pkg, undefined);
  53. });
  54. resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
  55. if (err) t.fail(err);
  56. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  57. t.equal(pkg, undefined);
  58. });
  59. resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
  60. if (err) t.fail(err);
  61. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  62. t.equal(pkg.main, 'bar');
  63. });
  64. });
  65. test('baz', function (t) {
  66. t.plan(4);
  67. var dir = path.join(__dirname, 'resolver');
  68. resolve('./baz', { basedir: dir }, function (err, res, pkg) {
  69. if (err) t.fail(err);
  70. t.equal(res, path.join(dir, 'baz/quux.js'));
  71. t.equal(pkg.main, 'quux.js');
  72. });
  73. resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  74. if (err) t.fail(err);
  75. t.equal(res, path.join(dir, 'baz/quux.js'));
  76. t.equal(pkg.main, 'quux.js');
  77. });
  78. });
  79. test('biz', function (t) {
  80. t.plan(24);
  81. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  82. resolve('./grux', { basedir: dir }, function (err, res, pkg) {
  83. if (err) t.fail(err);
  84. t.equal(res, path.join(dir, 'grux/index.js'));
  85. t.equal(pkg, undefined);
  86. });
  87. resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  88. if (err) t.fail(err);
  89. t.equal(res, path.join(dir, 'grux/index.js'));
  90. t.equal(pkg.main, 'biz');
  91. });
  92. resolve('./garply', { basedir: dir }, function (err, res, pkg) {
  93. if (err) t.fail(err);
  94. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  95. t.equal(pkg.main, './lib');
  96. });
  97. resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  98. if (err) t.fail(err);
  99. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  100. t.equal(pkg.main, './lib');
  101. });
  102. resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
  103. if (err) t.fail(err);
  104. t.equal(res, path.join(dir, 'tiv/index.js'));
  105. t.equal(pkg, undefined);
  106. });
  107. resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
  108. if (err) t.fail(err);
  109. t.equal(res, path.join(dir, 'tiv/index.js'));
  110. t.equal(pkg.main, 'grux');
  111. });
  112. resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
  113. if (err) t.fail(err);
  114. t.equal(res, path.join(dir, 'tiv/index.js'));
  115. t.equal(pkg, undefined);
  116. });
  117. resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
  118. if (err) t.fail(err);
  119. t.equal(res, path.join(dir, 'tiv/index.js'));
  120. t.equal(pkg.main, './lib');
  121. });
  122. resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
  123. if (err) t.fail(err);
  124. t.equal(res, path.join(dir, 'grux/index.js'));
  125. t.equal(pkg, undefined);
  126. });
  127. resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  128. if (err) t.fail(err);
  129. t.equal(res, path.join(dir, 'grux/index.js'));
  130. t.equal(pkg.main, 'tiv');
  131. });
  132. resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
  133. if (err) t.fail(err);
  134. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  135. t.equal(pkg.main, './lib');
  136. });
  137. resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  138. if (err) t.fail(err);
  139. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  140. t.equal(pkg.main, './lib');
  141. });
  142. });
  143. test('quux', function (t) {
  144. t.plan(2);
  145. var dir = path.join(__dirname, 'resolver/quux');
  146. resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
  147. if (err) t.fail(err);
  148. t.equal(res, path.join(dir, 'foo/index.js'));
  149. t.equal(pkg.main, 'quux');
  150. });
  151. });
  152. test('normalize', function (t) {
  153. t.plan(2);
  154. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  155. resolve('../grux', { basedir: dir }, function (err, res, pkg) {
  156. if (err) t.fail(err);
  157. t.equal(res, path.join(dir, 'index.js'));
  158. t.equal(pkg, undefined);
  159. });
  160. });
  161. test('cup', function (t) {
  162. t.plan(5);
  163. var dir = path.join(__dirname, 'resolver');
  164. resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  165. if (err) t.fail(err);
  166. t.equal(res, path.join(dir, 'cup.coffee'));
  167. });
  168. resolve('./cup.coffee', { basedir: dir }, function (err, res) {
  169. if (err) t.fail(err);
  170. t.equal(res, path.join(dir, 'cup.coffee'));
  171. });
  172. resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
  173. t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
  174. t.equal(err.code, 'MODULE_NOT_FOUND');
  175. });
  176. // Test that filename is reported as the "from" value when passed.
  177. resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
  178. t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
  179. });
  180. });
  181. test('mug', function (t) {
  182. t.plan(3);
  183. var dir = path.join(__dirname, 'resolver');
  184. resolve('./mug', { basedir: dir }, function (err, res) {
  185. if (err) t.fail(err);
  186. t.equal(res, path.join(dir, 'mug.js'));
  187. });
  188. resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
  189. if (err) t.fail(err);
  190. t.equal(res, path.join(dir, '/mug.coffee'));
  191. });
  192. resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  193. t.equal(res, path.join(dir, '/mug.js'));
  194. });
  195. });
  196. test('other path', function (t) {
  197. t.plan(6);
  198. var resolverDir = path.join(__dirname, 'resolver');
  199. var dir = path.join(resolverDir, 'bar');
  200. var otherDir = path.join(resolverDir, 'other_path');
  201. resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
  202. if (err) t.fail(err);
  203. t.equal(res, path.join(resolverDir, 'other_path/root.js'));
  204. });
  205. resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
  206. if (err) t.fail(err);
  207. t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
  208. });
  209. resolve('root', { basedir: dir }, function (err, res) {
  210. t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
  211. t.equal(err.code, 'MODULE_NOT_FOUND');
  212. });
  213. resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
  214. t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
  215. t.equal(err.code, 'MODULE_NOT_FOUND');
  216. });
  217. });
  218. test('path iterator', function (t) {
  219. t.plan(2);
  220. var resolverDir = path.join(__dirname, 'resolver');
  221. var exactIterator = function (x, start, getPackageCandidates, opts) {
  222. return [path.join(resolverDir, x)];
  223. };
  224. resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
  225. if (err) t.fail(err);
  226. t.equal(res, path.join(resolverDir, 'baz/quux.js'));
  227. t.equal(pkg && pkg.name, 'baz');
  228. });
  229. });
  230. test('incorrect main', function (t) {
  231. t.plan(1);
  232. var resolverDir = path.join(__dirname, 'resolver');
  233. var dir = path.join(resolverDir, 'incorrect_main');
  234. resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
  235. if (err) t.fail(err);
  236. t.equal(res, path.join(dir, 'index.js'));
  237. });
  238. });
  239. test('missing index', function (t) {
  240. t.plan(2);
  241. var resolverDir = path.join(__dirname, 'resolver');
  242. resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
  243. t.ok(err instanceof Error);
  244. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  245. });
  246. });
  247. test('missing main', function (t) {
  248. t.plan(1);
  249. var resolverDir = path.join(__dirname, 'resolver');
  250. resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
  251. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  252. });
  253. });
  254. test('null main', function (t) {
  255. t.plan(1);
  256. var resolverDir = path.join(__dirname, 'resolver');
  257. resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
  258. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  259. });
  260. });
  261. test('main: false', function (t) {
  262. t.plan(2);
  263. var basedir = path.join(__dirname, 'resolver');
  264. var dir = path.join(basedir, 'false_main');
  265. resolve('./false_main', { basedir: basedir }, function (err, res, pkg) {
  266. if (err) t.fail(err);
  267. t.equal(
  268. res,
  269. path.join(dir, 'index.js'),
  270. '`"main": false`: resolves to `index.js`'
  271. );
  272. t.deepEqual(pkg, {
  273. name: 'false_main',
  274. main: false
  275. });
  276. });
  277. });
  278. test('without basedir', function (t) {
  279. t.plan(1);
  280. var dir = path.join(__dirname, 'resolver/without_basedir');
  281. var tester = require(path.join(dir, 'main.js')); // eslint-disable-line global-require
  282. tester(t, function (err, res, pkg) {
  283. if (err) {
  284. t.fail(err);
  285. } else {
  286. t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
  287. }
  288. });
  289. });
  290. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  291. t.plan(2);
  292. var dir = path.join(__dirname, 'resolver');
  293. resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  294. if (err) t.fail(err);
  295. t.equal(res, path.join(dir, 'same_names/foo.js'));
  296. });
  297. resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  298. if (err) t.fail(err);
  299. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  300. });
  301. });
  302. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  303. t.plan(2);
  304. var dir = path.join(__dirname, 'resolver');
  305. resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  306. if (err) t.fail(err);
  307. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  308. });
  309. resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  310. if (err) t.fail(err);
  311. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  312. });
  313. });
  314. test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
  315. var testFile = path.basename(__filename);
  316. t.test('sanity check', function (st) {
  317. st.plan(1);
  318. resolve('./' + testFile, function (err, res, pkg) {
  319. if (err) t.fail(err);
  320. st.equal(res, __filename, 'sanity check');
  321. });
  322. });
  323. t.test('with a fake directory', function (st) {
  324. st.plan(4);
  325. resolve('./' + testFile + '/blah', function (err, res, pkg) {
  326. st.ok(err, 'there is an error');
  327. st.notOk(res, 'no result');
  328. st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  329. st.equal(
  330. err && err.message,
  331. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  332. 'can not find nonexistent module'
  333. );
  334. st.end();
  335. });
  336. });
  337. t.end();
  338. });
  339. test('async dot main', function (t) {
  340. var start = new Date();
  341. t.plan(3);
  342. resolve('./resolver/dot_main', function (err, ret) {
  343. t.notOk(err);
  344. t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
  345. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  346. t.end();
  347. });
  348. });
  349. test('async dot slash main', function (t) {
  350. var start = new Date();
  351. t.plan(3);
  352. resolve('./resolver/dot_slash_main', function (err, ret) {
  353. t.notOk(err);
  354. t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  355. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  356. t.end();
  357. });
  358. });
  359. test('not a directory', function (t) {
  360. t.plan(6);
  361. var path = './foo';
  362. resolve(path, { basedir: __filename }, function (err, res, pkg) {
  363. t.ok(err, 'a non-directory errors');
  364. t.equal(arguments.length, 1);
  365. t.equal(res, undefined);
  366. t.equal(pkg, undefined);
  367. t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
  368. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  369. });
  370. });
  371. test('non-string "main" field in package.json', function (t) {
  372. t.plan(5);
  373. var dir = path.join(__dirname, 'resolver');
  374. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  375. t.ok(err, 'errors on non-string main');
  376. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  377. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  378. t.equal(res, undefined, 'res is undefined');
  379. t.equal(pkg, undefined, 'pkg is undefined');
  380. });
  381. });
  382. test('non-string "main" field in package.json', function (t) {
  383. t.plan(5);
  384. var dir = path.join(__dirname, 'resolver');
  385. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  386. t.ok(err, 'errors on non-string main');
  387. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  388. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  389. t.equal(res, undefined, 'res is undefined');
  390. t.equal(pkg, undefined, 'pkg is undefined');
  391. });
  392. });
  393. test('browser field in package.json', function (t) {
  394. t.plan(3);
  395. var dir = path.join(__dirname, 'resolver');
  396. resolve(
  397. './browser_field',
  398. {
  399. basedir: dir,
  400. packageFilter: function packageFilter(pkg) {
  401. if (pkg.browser) {
  402. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  403. delete pkg.browser; // eslint-disable-line no-param-reassign
  404. }
  405. return pkg;
  406. }
  407. },
  408. function (err, res, pkg) {
  409. if (err) t.fail(err);
  410. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  411. t.equal(pkg && pkg.main, 'b');
  412. t.equal(pkg && pkg.browser, undefined);
  413. }
  414. );
  415. });
  416. test('absolute paths', function (t) {
  417. t.plan(4);
  418. var extensionless = __filename.slice(0, -path.extname(__filename).length);
  419. resolve(__filename, function (err, res) {
  420. t.equal(
  421. res,
  422. __filename,
  423. 'absolute path to this file resolves'
  424. );
  425. });
  426. resolve(extensionless, function (err, res) {
  427. t.equal(
  428. res,
  429. __filename,
  430. 'extensionless absolute path to this file resolves'
  431. );
  432. });
  433. resolve(__filename, { basedir: process.cwd() }, function (err, res) {
  434. t.equal(
  435. res,
  436. __filename,
  437. 'absolute path to this file with a basedir resolves'
  438. );
  439. });
  440. resolve(extensionless, { basedir: process.cwd() }, function (err, res) {
  441. t.equal(
  442. res,
  443. __filename,
  444. 'extensionless absolute path to this file with a basedir resolves'
  445. );
  446. });
  447. });
  448. var malformedDir = path.join(__dirname, 'resolver/malformed_package_json');
  449. test('malformed package.json', { skip: !fs.existsSync(malformedDir) }, function (t) {
  450. /* eslint operator-linebreak: ["error", "before"], function-paren-newline: "off" */
  451. t.plan(
  452. (3 * 3) // 3 sets of 3 assertions in the final callback
  453. + 2 // 1 readPackage call with malformed package.json
  454. );
  455. var basedir = malformedDir;
  456. var expected = path.join(basedir, 'index.js');
  457. resolve('./index.js', { basedir: basedir }, function (err, res, pkg) {
  458. t.error(err, 'no error');
  459. t.equal(res, expected, 'malformed package.json is silently ignored');
  460. t.equal(pkg, undefined, 'malformed package.json gives an undefined `pkg` argument');
  461. });
  462. resolve(
  463. './index.js',
  464. {
  465. basedir: basedir,
  466. packageFilter: function (pkg, pkgfile, dir) {
  467. t.fail('should not reach here');
  468. }
  469. },
  470. function (err, res, pkg) {
  471. t.error(err, 'with packageFilter: no error');
  472. t.equal(res, expected, 'with packageFilter: malformed package.json is silently ignored');
  473. t.equal(pkg, undefined, 'with packageFilter: malformed package.json gives an undefined `pkg` argument');
  474. }
  475. );
  476. resolve(
  477. './index.js',
  478. {
  479. basedir: basedir,
  480. readPackage: function (readFile, pkgfile, cb) {
  481. t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
  482. readFile(pkgfile, function (err, result) {
  483. try {
  484. cb(null, JSON.parse(result));
  485. } catch (e) {
  486. t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
  487. cb(null);
  488. }
  489. });
  490. }
  491. },
  492. function (err, res, pkg) {
  493. t.error(err, 'with readPackage: no error');
  494. t.equal(res, expected, 'with readPackage: malformed package.json is silently ignored');
  495. t.equal(pkg, undefined, 'with readPackage: malformed package.json gives an undefined `pkg` argument');
  496. }
  497. );
  498. });