Starting a New JavaScript Project¶
When working with JavaScript projects using npm, you can install dependencies
locally to your project, similar to how Python virtual environments work.
Local installation ensures that the dependencies are confined to your project
and do not affect the global JavaScript environment.
Creating a New Project¶
A package.json file indicates that the current directory is a JavaScript project.
This file will automatically be generated when you initialize your project.
- Initialize Your Project:
- First, create a new directory for your project and initialize it with a
package.jsonfile. -
This file will track your project's dependencies.
* The `npm init -y` command creates a `package.json` file with default values. -
Install Dependencies Locally:
-
Install your project's dependencies locally using the
npm installcommand.
-
This will install the package in your project's
node_modulesdirectory. - By default, this will install the package only for the current project.
- For example, to install Express.js:
-
-
Using the Local
node_modules:-
The locally installed packages are accessible for your project's files
and can berequired orimported as needed. -
Node.js will look in your project's
node_modulesdirectory for these packages.
-
Managing Local Packages¶
package.json- This file tracks all your local dependencies.
- Ensure that it's included in your version control system.
node_modules- This directory contains all the locally installed packages.
- It's typically not included in version control (add it to .gitignore).
package-lock.json- Generated after installations and ensures consistent installs across machines.
- It should be included in version control.
Example package.json File¶
After running npm init in your project folder, your package.json might
look something like this:
{
"name": "my_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
After installing dependencies (Express.js in this example), your
package.json might look something like this:
{
"name": "my_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {},
}
npm init -y and package.json¶
The npm init -y command is a quick way to initialize a new Node.js project with
a default package.json file.
The -y flag stands for "yes," and it tells npm to skip the interactive
questionnaire and generate a package.json file with default values immediately.
Breakdown of npm init -y¶
npm init:npm initis the standard command used to create a newpackage.jsonfile for a Node.js project.-
When run without flags,
npm initprompts the user to answer several questions (like project name, version, description, entry point, etc.) to customize thepackage.jsonfile. -
-yFlag: - The
-yflag bypasses these questions and fills in default values automatically. -
This is useful for quickly setting up a project without the need for customization at the initial stage.
-
Generated
package.json: - The
package.jsonfile created withnpm init -ywill have standard default values:name: The name of the directory.version:"1.0.0".description: An empty string.main:"index.js".scripts: An object with a test script.keywords: An empty array.author: An empty string.license:"ISC".
Example¶
Running npm init -y in a directory named my_project will create a package.json that looks something like this:
```json { "name": "my_project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }