Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
if (value.get_string().get(field_value)) {
return throw_invalid_package_config();
}

if (field_value != "commonjs" && field_value != "module") {
ProcessEmitWarning(realm->env(),
"Invalid \"type\" field in package.json: \"%.*s\". "
"Expected \"commonjs\" or \"module\".",
"InvalidPackageType",
static_cast<int>(field_value.size()),
field_value.data());
}

// Only update type if it is "commonjs" or "module"
// The default value is "none" for backward compatibility.
if (field_value == "commonjs" || field_value == "module") {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-find-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,6 @@ describe('findPackageJSON', () => { // Throws when no arguments are provided
assert.ok(stdout.includes(foundPjsonPath), stdout);
assert.strictEqual(code, 0);
});


});
70 changes: 70 additions & 0 deletions test/parallel/test-package-json-type-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('node:assert');
const fs = require('node:fs');
const { describe, it } = require('node:test');


describe('package.json type field validation', () => {
it('should warn for invalid type field values', async () => {
tmpdir.refresh();

fs.mkdirSync(tmpdir.resolve('invalid-type-pkg'), { recursive: true });
fs.writeFileSync(
tmpdir.resolve('invalid-type-pkg/package.json'),
JSON.stringify({ type: 'CommonJS' })
);
fs.writeFileSync(
tmpdir.resolve('invalid-type-pkg/index.js'),
'module.exports = 42;'
);

const { stderr } = await common.spawnPromisified(process.execPath, [
tmpdir.resolve('invalid-type-pkg/index.js'),
]);

assert.ok(stderr.includes('MODULE_INVALID_TYPE'));
assert.ok(stderr.includes('CommonJS'));
});

it('should not warn for valid type values', async () => {
tmpdir.refresh();

fs.mkdirSync(tmpdir.resolve('valid-type-pkg'), { recursive: true });
fs.writeFileSync(
tmpdir.resolve('valid-type-pkg/package.json'),
JSON.stringify({ type: 'commonjs' })
);
fs.writeFileSync(
tmpdir.resolve('valid-type-pkg/index.js'),
'module.exports = 42;'
);

const { stderr } = await common.spawnPromisified(process.execPath, [
tmpdir.resolve('valid-type-pkg/index.js'),
]);

assert.ok(!stderr.includes('MODULE_INVALID_TYPE'));
});

it('should not warn when type field is missing', async () => {
tmpdir.refresh();
fs.mkdirSync(tmpdir.resolve('no-type-pkg'), { recursive: true });
fs.writeFileSync(
tmpdir.resolve('no-type-pkg/package.json'),
JSON.stringify({ name: 'test' })
);
fs.writeFileSync(
tmpdir.resolve('no-type-pkg/index.js'),
'module.exports = 42;'
);

const { stderr } = await common.spawnPromisified(process.execPath, [
tmpdir.resolve('no-type-pkg/index.js'),
]);

assert.ok(!stderr.includes('MODULE_INVALID_TYPE'));
});
});