Modules — import, export and the CommonJS Split — Concept Check
Questions on Modules — import, export and the CommonJS Split. ES Modules are the standard: static import/export, resolved before execution, with live bindings. CommonJS require() is the older Node system, and the two do not mix cleanly — which is the source of most "cannot use import outside a module" errors.
Questions in this quiz
8 questions on Modules — import, export and the CommonJS Split — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer tries to use `import` in a Node.js file without any special configuration. They get an error saying 'Cannot use import statement outside a module'. What is the most likely cause?
- AThe file extension is .js and lacks 'type': 'module' in package.json
- BThe file is located in the node_modules directory
- CThe file extension is .mjs
- DThe file extension is .cjs
2.You have a Node.js project with a mix of .mjs and .cjs files. What is a potential issue you might encounter?
- ABoth .mjs and .cjs files are treated as ESM
- B.mjs files cannot import from .cjs files
- CBoth .mjs and .cjs files are treated as CommonJS
- D.cjs files cannot import from .mjs files
3.A module exports a function using `export default`. What is a potential downside of this approach?
- ADefault exports cannot be imported alongside named exports
- BDefault exports are not supported in Node.js
- CDefault exports are always slower to import
- DThe import name is chosen by the importer, which can lead to inconsistency
4.A developer sees that after calling a function from a module, the imported variable's value changes. What explains this behavior?
- AImports are live views of the exported binding, not snapshots
- BThe import statement is inside a function
- CThe imported variable is a constant
- DThe module is being re-imported every time
5.In a Node.js project, a developer uses `require()` to import an ESM module and encounters an error. What is the reason?
- ACommonJS cannot require ESM modules
- BThe ESM module has circular dependencies
- CESM modules cannot be imported at runtime
- DThe ESM module is missing a default export
6.Which of the following is true about named exports in ES Modules?
- AThey must be the only export in a module
- BThey are resolved at runtime
- CThey can be renamed at the import site
- DThey cannot be used with default exports
7.A developer uses `import { greet } from './greet.js'` but the function `greet` is not defined in the module. What will happen?
- AThe import will silently fail
- BAn error will be thrown at runtime
- CThe import will return undefined
- DA syntax error will occur
8.What happens when you use `require()` in a .mjs file?
- AIt will throw an error because .mjs files are ESM
- BIt will work as expected, importing the module
- CIt will import the module asynchronously
- DIt will convert the .mjs file to CommonJS