Get Started with JavaScript
Possible Setups:
- Basic setup: using default tools
- Better setup (recommended): using improved tools that are better and more efficient
Basic Setup
Node.js (which includes JavaScript runtime and npm) can be installed from: https://nodejs.org/en/download
Once installed, you can check the version:
node --version
npm --version
1. Project Initialization
Initialize a project:
npm init -y
This creates a package.json file that manages your project dependencies.
2. Running a JavaScript File
Create a main.js file with the code:
console.log("Hello World");
Run the file:
node main.js
3. Installing Libraries
For example, to install the dotenv library:
npm install dotenv
Uninstalling a library can be done with the npm uninstall command.
The list of installed libraries is automatically maintained in package.json and package-lock.json.
Install libraries from a package.json file:
npm install
When getting back to a project
# Install libraries
npm install
# Run JavaScript file
node main.js
Better Setup
Instead of using the default Node.js tools, you can use nvm for Node.js version management and pnpm as a faster, more efficient package manager.
Installing the Tools
nvm can be installed from: https://github.com/nvm-sh/nvm#installing-and-updating
pnpm can be installed from: https://pnpm.io/installation
1. Node.js Version Management
nvm allows you to install and switch between different Node.js versions easily.
Install the latest LTS version of Node.js:
nvm install --lts
nvm use --lts
Check your Node.js version:
node --version
2. Project Initialization
Initialize a project:
pnpm init
3. Running a JavaScript File
Create a main.js file with the code:
console.log("Hello World");
Run the file:
node main.js
# or add a script to package.json and use:
# pnpm run start
4. Installing Libraries
For example, to install the dotenv library:
pnpm add dotenv
# instead of: npm install dotenv
Uninstalling a library can be done with the pnpm remove command.
The list of installed libraries is automatically maintained in package.json and pnpm-lock.yaml.
When getting back to a project
# Use the correct Node.js version (if .nvmrc exists)
nvm use
# Install libraries (pnpm is much faster than npm)
pnpm install
# Run JavaScript file
node main.js
# or use your defined scripts:
# pnpm run start