Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Node.js Tutorial

Node HOMENode IntroNode Get StartedNode JS RequirementsNode.js vs BrowserNode Cmd LineNode V8 EngineNode ArchitectureNode Event Loop

Asynchronous

Node AsyncNode PromisesNode Async/AwaitNode Errors Handling

Module Basics

Node ModulesNode ES ModulesNode NPMNode package.jsonNode NPM ScriptsNode Manage DepNode Publish Packages

Core Modules

HTTP ModuleHTTPS ModuleFile System (fs)Path ModuleOS ModuleURL ModuleEvents ModuleStream ModuleBuffer ModuleCrypto ModuleTimers ModuleDNS ModuleAssert ModuleUtil ModuleReadline Module

JS & TS Features

Node ES6+Node ProcessNode TypeScriptNode Adv. TypeScriptNode Lint & Formatting

Building Applications

Node FrameworksExpress.jsMiddleware ConceptREST API DesignAPI AuthenticationNode.js with Frontend

Database Integration

MySQL Get StartedMySQL Create DatabaseMySQL Create TableMySQL Insert IntoMySQL Select FromMySQL WhereMySQL Order ByMySQL DeleteMySQL Drop TableMySQL UpdateMySQL LimitMySQL Join
MongoDB Get StartedMongoDB Create DBMongoDB CollectionMongoDB InsertMongoDB FindMongoDB QueryMongoDB SortMongoDB DeleteMongoDB Drop CollectionMongoDB UpdateMongoDB LimitMongoDB Join

Advanced Communication

GraphQLSocket.IOWebSockets

Testing & Debugging

Node Adv. DebuggingNode Testing AppsNode Test FrameworksNode Test Runner

Node.js Deployment

Node Env VariablesNode Dev vs ProdNode CI/CDNode SecurityNode Deployment

Perfomance & Scaling

Node LoggingNode MonitoringNode PerformanceChild Process ModuleCluster ModuleWorker Threads

Node.js Advanced

MicroservicesNode WebAssemblyHTTP2 ModulePerf_hooks ModuleVM ModuleTLS/SSL ModuleNet ModuleZlib ModuleReal-World Examples

Hardware & IoT

RasPi Get StartedRasPi GPIO IntroductionRasPi Blinking LEDRasPi LED & PushbuttonRasPi Flowing LEDsRasPi WebSocketRasPi RGB LED WebSocketRasPi Components

Node.js Reference

Built-in ModulesEventEmitter (events)Worker (cluster)Cipher (crypto)Decipher (crypto)DiffieHellman (crypto)ECDH (crypto)Hash (crypto)Hmac (crypto)Sign (crypto)Verify (crypto)Socket (dgram, net, tls)ReadStream (fs, stream)WriteStream (fs, stream)Server (http, https, net, tls)Agent (http, https)Request (http)Response (http)Message (http)Interface (readline)

Resources & Tools

Node.js CompilerNode.js ServerNode.js QuizNode.js ExercisesNode.js SyllabusNode.js Study PlanNode.js BootcampNode.js Certificate

Node.js Command Line Usage


Node.js provides a powerful command line interface (CLI) that allows you to run JavaScript files, manage packages, debug applications, and more.

This guide covers the essential commands and techniques every Node.js developer should know.

Note: All commands should be run in a terminal or command prompt.

On Windows, you can use Command Prompt, PowerShell, or Windows Terminal.

On macOS/Linux, use Terminal.


Basic Node.js Commands

These are the most common commands you'll use when working with Node.js applications:

Run a JavaScript file

# Run a JavaScript file
node app.js

# Run with additional arguments
node app.js arg1 arg2

# Run in watch mode (restarts on file changes)
node --watch app.js

Using the REPL

The Node.js REPL (Read-Eval-Print Loop) is an interactive shell for executing JavaScript code.

The REPL is started by runningnode in the terminal:

Using the REPL

> const name = 'Node.js';
> console.log(`Hello, ${name}!`);
> .help // Show available commands
> .exit // Exit REPL

Command Line Arguments

Access command line arguments usingprocess.argv:

Command Line Arguments

// args.js
console.log('All arguments:', process.argv);
console.log('First argument:', process.argv[2]);
console.log('Second argument:', process.argv[3]);

// Example usage:
// node args.js hello world
// Output:
// All arguments: ['/path/to/node', '/path/to/args.js', 'hello', 'world']
// First argument: hello
// Second argument: world

Environment Variables

Access and set environment variables:

Environment Variables

// env.js
console.log('Environment:', process.env.NODE_ENV || 'development');
console.log('Custom variable:', process.env.MY_VARIABLE);
console.log('Database URL:', process.env.DATABASE_URL || 'Not set');

// Example usage with environment variables:
// NODE_ENV=production MY_VARIABLE=test node env.js

Set Environment Variables

# Set environment variables when running
NODE_ENV=production MY_VARIABLE=test node env.js


Debugging Node.js Applications

Node.js includes a powerful debugging system that integrates with Chrome DevTools:

Basic Debugging Commands

# Start with inspector (listens on default port 9229)
node --inspect app.js

# Break on first line of application
node --inspect-brk app.js

# Specify a custom port
node --inspect=9222 app.js

# Enable remote debugging (be careful with this in production)
node --inspect=0.0.0.0:9229 app.js

Using Chrome DevTools for Debugging

  1. Start your application withnode --inspect app.js
  2. Open Chrome and navigate tochrome://inspect
  3. Click on "Open dedicated DevTools for Node"
  4. Set breakpoints and debug your application

Common CLI Tools

Node.js comes with several useful command-line tools:

Node Version Manager (nvm)

# Install and use different Node.js versions
nvm install 18.16.0 # Install specific version
nvm use 18.16.0 # Switch to version
nvm ls # List installed versions

npm (Node Package Manager)

# Common npm commands
npm init # Initialize a new project
npm install # Install dependencies
npm update # Update packages
npm audit # Check for vulnerabilities

Common Command Line Flags

Node.js provides several command-line flags to control its behavior. Here are some of the most useful ones:

Basic Flags

# Show Node.js version
node --version # or -v

# Show V8 version
node --v8-options

# Show command-line help
node --help

Runtime Behavior

# Check syntax without executing
node --check app.js

# Show stack traces for warnings
node --trace-warnings app.js

# Set max memory (in MB)
node --max-old-space-size=4096 app.js

# Preload a module before execution
node --require dotenv/config app.js

Performance and Optimization

# Enable ES module loader
node --experimental-modules app.mjs

# Enable experimental features
node --experimental-repl-await

# Enable experimental worker threads
node --experimental-worker




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp