Node.js Modules and Package Management Essentials Quiz Quiz

Challenge your understanding of Node.js modules and the essentials of package management with npm and yarn, including dependencies, scripts, and module imports. This quiz is designed to reinforce core concepts and best practices in working with JavaScript project modules and package ecosystems.

  1. Module Import Syntax

    Which syntax should you use to import a built-in module in Node.js using the CommonJS approach?

    1. const fs = require('fs');
    2. import fs from 'fs';
    3. use fs();
    4. include fs;

    Explanation: The require syntax is the standard way to import modules in Node.js using the CommonJS module system. The import syntax is used with ES modules, not CommonJS, and is only available in newer JavaScript versions with proper configuration. 'Include' and 'use' are not valid statements for importing modules in Node.js. Only the first option will correctly load the built-in module in most Node.js projects.

  2. Understanding package.json

    What is the primary purpose of the 'package.json' file in a Node.js project?

    1. To list only the JavaScript files to be executed
    2. To define dependencies, scripts, and metadata for the project
    3. To contain all project source code
    4. To store environment variables securely

    Explanation: The package.json file provides essential information about the project, including dependencies, scripts, version, and other metadata. It does not store environment variables; this is usually managed in separate files. Project source code is not stored in package.json, and although scripts can refer to JavaScript files, the file itself does not list them directly for execution.

  3. Installing a Package

    What command installs a package called 'express' and saves it as a dependency using npm?

    1. npm fetch express
    2. npm install express
    3. npm upload express
    4. npm build express

    Explanation: npm install express is the correct command to add a package as a dependency. 'npm upload' does not exist as a way to install packages; it is related to publishing. 'npm build' is about building scripts, not installing, and 'npm fetch' is not a recognized npm command for package installation.

  4. Yarn Add Command

    If you want to add a new library as a dependency in your project using yarn, which command should you use?

    1. yarn get lodash
    2. yarn new lodash
    3. yarn add lodash
    4. yarn install lodash-only

    Explanation: The yarn add command installs a new dependency and records it in your project's manifest. The commands yarn get and yarn new do not exist for dependency installation. 'yarn install lodash-only' is not a valid command; 'yarn install' without parameters installs all project dependencies but does not add new ones.

  5. Local vs Global Installation

    When you install a package globally with a package manager, where can it be accessed?

    1. It can only be accessed by system administrators.
    2. It is only available in the current project's directory.
    3. It becomes available to remote users on the network.
    4. It can be accessed from any project or directory on your system.

    Explanation: A globally installed package becomes accessible system-wide, not just to a specific project. Installing locally limits package availability to the project directory only. System administrators are not uniquely privileged for global packages, and installing globally does not make the package available over a network.

  6. Development Dependencies

    Which of the following describes a development dependency in the context of a Node.js project?

    1. A library required for running the application in all environments
    2. A utility that is always loaded at runtime by end-users
    3. A module used for database connections in production
    4. A tool required only during development or testing, not in production

    Explanation: Development dependencies are only necessary during development, such as test frameworks or build tools, and are not included in production environments. Database connection modules and libraries required in all environments should be regular dependencies. Utilities loaded at runtime by users usually belong to production dependencies.

  7. Updating Packages

    Which command should be used to update all dependencies in a Node.js project managed with npm?

    1. npm update
    2. npm refresh
    3. npm upgrade
    4. npm revise

    Explanation: The npm update command updates all dependencies specified in the package.json file. 'npm upgrade', 'npm revise', and 'npm refresh' are invalid commands for updating dependencies in standard npm workflows. Only 'npm update' ensures your installed packages are up to date.

  8. Exporting Functions

    How do you export a function from a module in Node.js using CommonJS syntax?

    1. export function myFunction() {}
    2. exports =u003E myFunction()
    3. public myFunction = function() {}
    4. module.exports = myFunction;

    Explanation: In CommonJS, module.exports is used to export functions or values. The export statement is for ES modules, not CommonJS. The 'public' keyword and 'exports =u003E' syntax are not valid in Node.js for exporting functions. Only module.exports follows CommonJS conventions correctly.

  9. Checking Installed Package Version

    What is a simple command to check the version of an installed package, such as 'express', using npm?

    1. npm find express
    2. npm current express
    3. npm show-version express
    4. npm list express

    Explanation: The npm list express command displays the installed version of the specified package. 'npm show-version' and 'npm find' are not valid commands, and 'npm current' does not exist. Only 'npm list express' will show you which version is currently installed, along with its tree in the dependency graph.

  10. Purpose of node_modules Directory

    What is the main role of the 'node_modules' directory in a Node.js project?

    1. It contains user authentication credentials.
    2. It stores installed packages and their dependencies locally for the project.
    3. It holds the configuration for deployment pipelines.
    4. It keeps track of the project's commit history.

    Explanation: node_modules is the standard folder for storing all project dependencies and their nested modules. It does not handle credentials, version history, or deployment pipeline configuration. These other aspects are managed by different files or tools within a project.