diff --git a/doc/api/cli.md b/doc/api/cli.md index 840a6cad3b6a23..15371db4e255de 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -75,6 +75,10 @@ Identical to `-e` but prints the result. added: - v5.0.0 - v4.2.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/19600 + description: The `--require` option is now supported when checking a file. --> Syntax check the script without executing. diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 01efe6f32900d4..6b4bb6a7002d98 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -207,6 +207,12 @@ const CJSModule = NativeModule.require('internal/modules/cjs/loader'); + perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END); + perf.markMilestone( + NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_START); + preloadModules(); + perf.markMilestone( + NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END); // check if user passed `-c` or `--check` arguments to Node. if (process._syntax_check_only != null) { const fs = NativeModule.require('fs'); @@ -216,12 +222,6 @@ checkScriptSyntax(source, filename); process.exit(0); } - perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END); - perf.markMilestone( - NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_START); - preloadModules(); - perf.markMilestone( - NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END); CJSModule.runMain(); } else { perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_START); diff --git a/test/fixtures/no-wrapper.js b/test/fixtures/no-wrapper.js new file mode 100644 index 00000000000000..d8c6aca111d31b --- /dev/null +++ b/test/fixtures/no-wrapper.js @@ -0,0 +1 @@ +require('module').wrapper = ['', '']; diff --git a/test/parallel/test-cli-syntax.js b/test/parallel/test-cli-syntax.js index 0b1010c4dad2ec..d80e8c698d72de 100644 --- a/test/parallel/test-cli-syntax.js +++ b/test/parallel/test-cli-syntax.js @@ -140,3 +140,26 @@ syntaxArgs.forEach(function(args) { })); }); }); + +// should work with -r flags +['-c', '--check'].forEach(function(checkFlag) { + ['-r', '--require'].forEach(function(requireFlag) { + const preloadFile = fixtures.path('no-wrapper.js'); + const file = fixtures.path('syntax', 'illegal_if_not_wrapped.js'); + const args = [requireFlag, preloadFile, checkFlag, file]; + const cmd = [node, ...args].join(' '); + exec(cmd, common.mustCall((err, stdout, stderr) => { + assert.strictEqual(err instanceof Error, true); + assert.strictEqual(err.code, 1); + + // no stdout should be produced + assert.strictEqual(stdout, ''); + + // stderr should have a syntax error message + assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`); + + // stderr should include the filename + assert(stderr.startsWith(file), `${stderr} starts with ${file}`); + })); + }); +});