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.
Which syntax should you use to import a built-in module in Node.js using the CommonJS approach?
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.
What is the primary purpose of the 'package.json' file in a Node.js project?
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.
What command installs a package called 'express' and saves it as a dependency using npm?
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.
If you want to add a new library as a dependency in your project using yarn, which command should you use?
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.
When you install a package globally with a package manager, where can it be accessed?
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.
Which of the following describes a development dependency in the context of a Node.js project?
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.
Which command should be used to update all dependencies in a Node.js project managed with npm?
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.
How do you export a function from a module in Node.js using CommonJS syntax?
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.
What is a simple command to check the version of an installed package, such as 'express', using npm?
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.
What is the main role of the 'node_modules' directory in a Node.js project?
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.