Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,20 +1236,20 @@ const posix = {
join(...args) {
if (args.length === 0)
return '.';
let joined;

const path = [];
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = arg;
else
joined += `/${arg}`;
path.push(arg);
}
}
if (joined === undefined)

if (path.length === 0)
return '.';
return posix.normalize(joined);

return posix.normalize(ArrayPrototypeJoin(path, '/'));
},

/**
Expand Down
56 changes: 37 additions & 19 deletions test/es-module/test-esm-type-field-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const common = require('../common');
const assert = require('assert');
const exec = require('child_process').execFile;
const { describe, it } = require('node:test');

const mjsFile = require.resolve('../fixtures/es-modules/mjs-file.mjs');
const cjsFile = require.resolve('../fixtures/es-modules/cjs-file.cjs');
Expand All @@ -13,25 +14,42 @@ const packageTypeModuleMain =
require.resolve('../fixtures/es-modules/package-type-module/index.js');

// Check that running `node` without options works
expect('', mjsFile, '.mjs file');
expect('', cjsFile, '.cjs file');
expect('', packageTypeModuleMain, 'package-type-module');
expect('', packageTypeCommonJsMain, 'package-type-commonjs');
expect('', packageWithoutTypeMain, 'package-without-type');

// Check that --input-type isn't allowed for files
expect('--input-type=module', packageTypeModuleMain,
'ERR_INPUT_TYPE_NOT_ALLOWED', true);

try {
require('../fixtures/es-modules/package-type-module/index.js');
assert.fail('Expected CJS to fail loading from type: module package.');
} catch (e) {
assert.strictEqual(e.name, 'Error');
assert.strictEqual(e.code, 'ERR_REQUIRE_ESM');
assert(e.toString().match(/require\(\) of ES Module/g));
assert(e.message.match(/require\(\) of ES Module/g));
}
describe('ESM type field errors', { concurrency: true }, () => {
it('.cjs file', () => {
expect('', cjsFile, '.cjs file');
});

it('.mjs file', () => {
expect('', mjsFile, '.mjs file');
});

it('package.json with "type": "module"', () => {
expect('', packageTypeModuleMain, 'package-type-module');
});

it('package.json with "type": "commonjs"', () => {
expect('', packageTypeCommonJsMain, 'package-type-commonjs');
});

it('package.json with no "type" field', () => {
expect('', packageWithoutTypeMain, 'package-without-type');
});

it('--input-type=module disallowed for files', () => {
expect(
'--input-type=module',
packageTypeModuleMain,
'ERR_INPUT_TYPE_NOT_ALLOWED',
true,
);
});

it('--input-type=module disallowed for directories', () => {
assert.throws(() => require('../fixtures/es-modules/package-type-module/index.js'), {
code: 'ERR_REQUIRE_ESM'
});
});
});

function expect(opt = '', inputFile, want, wantsError = false) {
const argv = [inputFile];
Expand Down