• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5const path = require('path');
6
7const { createRequire, createRequireFromPath } = require('module');
8
9const p = path.resolve(__dirname, '..', 'fixtures', 'fake.js');
10const u = new URL(`file://${p}`);
11
12const req = createRequireFromPath(p);
13assert.strictEqual(req('./baz'), 'perhaps I work');
14
15const reqToo = createRequire(u);
16assert.deepStrictEqual(reqToo('./experimental'), { ofLife: 42 });
17
18assert.throws(() => {
19  createRequire('https://github.com/nodejs/node/pull/27405/');
20}, {
21  code: 'ERR_INVALID_ARG_VALUE'
22});
23
24assert.throws(() => {
25  createRequire('../');
26}, {
27  code: 'ERR_INVALID_ARG_VALUE'
28});
29
30assert.throws(() => {
31  createRequire({});
32}, {
33  code: 'ERR_INVALID_ARG_VALUE',
34  message: 'The argument \'filename\' must be a file URL object, file URL ' +
35           'string, or absolute path string. Received {}'
36});
37