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

if (field_value != "commonjs" && field_value != "module") {
fprintf(stderr,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using the usual machinery (process.emitWarning() in JS, or ProcessEmitWarning() in C++) to emit warning so that it can be suppressed with NODE_NO_WARNINGS=1 etc. or captured through event listeners by users.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Changed to use ProcessEmitWarning() instead of fprintf(). Let me know if the implementation looks correct.

"node: [MODULE_INVALID_TYPE] Invalid \"type\" field in package.json: \"%.*s\". "
"Expected \"commonjs\" or \"module\".\n",
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 Expand Up @@ -275,7 +284,7 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {

if (package_json == nullptr) {
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}


args.GetReturnValue().Set(package_json->Serialize(realm));
}
Expand Down
64 changes: 64 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,68 @@ describe('findPackageJSON', () => { // Throws when no arguments are provided
assert.ok(stdout.includes(foundPjsonPath), stdout);
assert.strictEqual(code, 0);
});


});

describe('package.json type field validation', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a new test file instead of appending it to the same test file? See

In principle, when adding a new test, it should be placed in a new file.
Unless there is strong motivation to do so, refrain from appending
new test cases to an existing file. Similar to the reproductions we ask

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I've added a new test file.

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'));
});
});
Loading