JavaScript Modules 3 — Questions and Answers
Question 1: Which Node.js file extension signals that a file should be treated as an ES module?
- .cjs
- .mjs (Correct answer)
- .esm
- .mod
Correct answer: .mjs
Node.js recognizes `.mjs` files as ES modules regardless of the `type` field in package.json.
Question 2: What is a key behavioral difference between `require()` and `import`?
- `require()` is asynchronous; `import` is synchronous
- `import` is statically analyzed and hoisted; `require()` is evaluated at runtime (Correct answer)
- `import` only works in browsers; `require()` only in Node.js
- Both are identical in behavior
Correct answer: `import` is statically analyzed and hoisted; `require()` is evaluated at runtime
Static `import` declarations are analyzed at parse time and hoisted, whereas `require()` executes at the point it appears in the code.
Question 3: What does `import()` (dynamic import) return?
- The module's default export directly
- A Promise that resolves to the module namespace object (Correct answer)
- A synchronous module object
- An iterator over the module's exports
Correct answer: A Promise that resolves to the module namespace object
Dynamic `import()` is an asynchronous operation that returns a Promise resolving to the module namespace object.
Question 4: In a browser ES module, how is the `defer` attribute behavior affected?
- Module scripts are always deferred by default (Correct answer)
- Module scripts are always render-blocking
- You must add `defer` explicitly to defer module scripts
- Module scripts cannot be deferred
Correct answer: Module scripts are always deferred by default
Script tags with `type="module"` are deferred by default, executing after the document is parsed.
Question 5: What is a 'bare specifier' in ES module imports?
- An import with no braces: `import foo from 'foo'`
- An import path like `'lodash'` that doesn't start with `.`, `..`, or `/` (Correct answer)
- An import of a file without an extension
- An import using an absolute URL
Correct answer: An import path like `'lodash'` that doesn't start with `.`, `..`, or `/`
A bare specifier is a module name like `'lodash'` with no path prefix; browsers require an import map to resolve them.
Question 6: Which of the following correctly demonstrates a named export with a default export in the same module?
- export default function main() {} export const helper = () => {}; (Correct answer)
- export default { main, helper };
- export named const helper = () => {}; export default main;
- You cannot mix default and named exports
Correct answer: export default function main() {} export const helper = () => {};
A module can have one default export and any number of named exports simultaneously.
Question 7: What does `export * from './lib.js'` NOT re-export?
- Named exports
- The default export of `./lib.js` (Correct answer)
- Aliased named exports
- Constants declared with `const`
Correct answer: The default export of `./lib.js`
`export *` re-exports all named exports but explicitly excludes the default export of the source module.
Which Node.js file extension signals that a file should be treated as an ES module?