### Example Project Setup and npm ci Source: https://docs.npmjs.com/cli/v11/commands/npm-ci Demonstrates the typical workflow: navigating to a project directory, running npm install to ensure a lock file exists, and then running npm ci for a clean installation. ```bash $ cd ./my/npm/project $ npm install added 154 packages in 10s $ ls | grep package-lock $ npm ci added 154 packages in 5s ``` -------------------------------- ### Example package.json with start script Source: https://docs.npmjs.com/cli/v11/commands/npm-start Defines a 'start' script in the 'scripts' object of a package.json file. ```json { "scripts": { "start": "node foo.js" } } ``` -------------------------------- ### npm Install Algorithm Example 1 Source: https://docs.npmjs.com/cli/install Illustrates dependency resolution for a simple package structure where dependencies are satisfied by higher-level installations. ```text A +-- B +-- C +-- D ``` -------------------------------- ### Example: Deny All Packages with Install Scripts Source: https://docs.npmjs.com/cli/v11/commands/npm-deny-scripts This example shows how to deny install scripts for all packages that have install scripts and are not already approved. This is useful for a comprehensive security audit. ```bash # Deny everything that has install scripts and isn't already approved npm deny-scripts --all ``` -------------------------------- ### npm Install Algorithm Example 2 Source: https://docs.npmjs.com/cli/install Demonstrates dependency resolution with version conflicts, showing how npm installs specific versions to satisfy individual package requirements. ```text A +-- B +-- C `-- D@2 +-- D@1 ``` -------------------------------- ### Example: All Packages Have Verified Signatures Source: https://docs.npmjs.com/verifying-registry-signatures This is an example of a successful response when all installed package versions have valid registry signatures. ```bash audited 1640 packages in 2s 1640 have verified registry signatures ``` -------------------------------- ### Explain Package at Specific Folder Path Example Source: https://docs.npmjs.com/cli/v11/commands/npm-explain Demonstrates the dependency chain for the 'find-up' package located within the 'nyc' package's `node_modules` folder, showing its installation path and related dependencies. ```text find-up@3.0.0 dev node_modules/nyc/node_modules/find-up find-up@"^3.0.0" from nyc@14.1.1 node_modules/nyc nyc@"^14.1.1" from tap@14.10.8 node_modules/tap dev tap@"^14.10.8" from the root project ``` -------------------------------- ### GitHub Actions Setup for Node.js and npm Publish Source: https://docs.npmjs.com/trusted-publishers Example of setting up Node.js in GitHub Actions, configuring npm registry, and publishing a package using trusted publishing. It also shows how to use a read-only token for installing private dependencies. ```yaml # GitHub Actions example - uses: actions/setup-node@v6 with: node-version: '24' registry-url: 'https://registry.npmjs.org' package-manager-cache: false # never use caching in release builds # Use a read-only token for installing dependencies - run: npm ci env: NODE_AUTH_TOKEN: ${{ secrets.NPM_READ_TOKEN }} # Publish uses OIDC - no token needed - run: npm publish ``` -------------------------------- ### npm start command synopsis Source: https://docs.npmjs.com/cli/v11/commands/npm-start The basic syntax for the npm start command. ```bash npm start [-- ] ``` -------------------------------- ### Installing a Package by Tag Source: https://docs.npmjs.com/cli/dist-tag Demonstrates how to install a specific version of a package using a distribution tag. ```bash npm install @ ``` -------------------------------- ### Executing the npm start command Source: https://docs.npmjs.com/cli/v11/commands/npm-start Demonstrates running the 'npm start' command after defining a 'start' script in package.json. The output shows the script being executed. ```bash npm start > npm@x.x.x start > node foo.js (foo.js output would be here) ``` -------------------------------- ### Install from GitHub Repository Source: https://docs.npmjs.com/cli/v11/commands/npm-install Installs a package directly from a GitHub repository. ```bash npm install githubname/reponame ``` -------------------------------- ### npm Install with Flags Source: https://docs.npmjs.com/cli/v11/commands/npm-install Installs packages and specifies where they should be saved in `package.json`. ```bash npm install node-tap --save-dev ``` ```bash npm install dtrace-provider --save-optional ``` ```bash npm install readable-stream --save-exact ``` ```bash npm install ansi-regex --save-bundle ``` -------------------------------- ### Install from Git URL Source: https://docs.npmjs.com/cli/install Installs a package directly from a Git URL. ```bash npm install git://github.com/npm/cli.git#v1.0.27 ``` -------------------------------- ### Install Local Package for Testing Source: https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages Test your local package by installing it using npm install with the full path to your package directory. ```bash npm install path/to/my-package ``` -------------------------------- ### Install a package Source: https://docs.npmjs.com/cli/install Basic command to install one or more packages. Aliases include 'add', 'i', 'in', 'ins', 'inst', 'insta', 'instal', 'isnt', 'isnta', 'isntal', and 'isntall'. ```bash npm install [ ...] aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall ``` -------------------------------- ### Install from Bitbucket repository Source: https://docs.npmjs.com/cli/install Installs a package from a Bitbucket repository using the `bitbucket:` prefix. ```bash npm install bitbucket:mybitbucketuser/myproject ``` -------------------------------- ### Install from GitLab repository Source: https://docs.npmjs.com/cli/install Installs a package from a GitLab repository using the `gitlab:` prefix. ```bash npm install gitlab:mygitlabuser/myproject ``` -------------------------------- ### Install from Gist Source: https://docs.npmjs.com/cli/install Installs a package from a GitHub Gist, referencing the Gist ID. ```bash npm install gist:101a11beef ``` -------------------------------- ### Basic npm Install Source: https://docs.npmjs.com/cli/install Installs a package and saves it to your project's dependencies by default. ```bash npm install sax ``` -------------------------------- ### Example: Uninstall Global Package Source: https://docs.npmjs.com/uninstalling-packages-and-dependencies This is an example of uninstalling the 'jshint' package globally. ```bash npm uninstall -g jshint ``` -------------------------------- ### Example package.json with multiple scripts Source: https://docs.npmjs.com/cli/v11/using-npm/scripts Illustrates a package.json file with multiple scripts defined, such as 'prepare', 'build', and 'test', showcasing how different lifecycle events can trigger specific build or test commands. ```json { "scripts": { "prepare": "scripts/build.js", "test": "scripts/test.js" } } ``` -------------------------------- ### Node.js 'Cannot find module' error example Source: https://docs.npmjs.com/using-npm-packages-in-your-projects This error occurs when a required module has not been installed correctly. Ensure packages are installed using `npm install`. ```javascript module.js:340 throw err; ^ Error: Cannot find module 'lodash' ``` -------------------------------- ### Install Package Dependencies Source: https://docs.npmjs.com/cli/npm Installs all dependencies listed in the package.json file for the current project. This is a fundamental step when starting with a Node.js project. ```bash npm install ``` -------------------------------- ### npm rebuild with install script Source: https://docs.npmjs.com/cli/v11/commands/npm-rebuild Example of a package.json configuration where npm uses a default install hook to rebuild using node-gyp. ```json "scripts": { "install": "node-gyp rebuild" } ``` -------------------------------- ### npm install-test Synopsis Source: https://docs.npmjs.com/cli/v11/commands/npm-install-test This is the basic command structure for installing packages and running tests. ```bash npm install-test [ ...] alias: it ``` -------------------------------- ### Initialize a New Workspace Source: https://docs.npmjs.com/cli/v11/using-npm/workspaces Use 'npm init -w' to automate the setup of a new workspace, including directory creation and package.json configuration. ```bash npm init -w ./packages/a ``` -------------------------------- ### Install from tarball URL Source: https://docs.npmjs.com/cli/install Fetches and installs a package from a remote tarball URL. The URL must start with 'http://' or 'https://'. ```bash npm install https://github.com/indexzero/forever/tarball/v0.5.6 ``` -------------------------------- ### Get Help Source: https://docs.npmjs.com/cli/npm Displays a list of all available npm commands and their descriptions. ```bash npm help ``` -------------------------------- ### Example: Deny a Specific Package Source: https://docs.npmjs.com/cli/v11/commands/npm-deny-scripts This example demonstrates how to deny install scripts for the 'telemetry-pkg' package. The command will update your package.json to reflect this denial. ```bash # Deny a specific package outright npm deny-scripts telemetry-pkg ``` -------------------------------- ### Install from GitHub repository (github: prefix) Source: https://docs.npmjs.com/cli/install Installs a package from a GitHub repository using the `github:` prefix. ```bash npm install github:mygithubuser/myproject ``` -------------------------------- ### Verbose npm Install Source: https://docs.npmjs.com/common-errors When facing installation problems, use the `-verbose` flag to get more detailed output, which can help pinpoint the cause of the error. ```bash npm install -verbose ``` -------------------------------- ### npm init command synopsis Source: https://docs.npmjs.com/cli/v11/commands/npm-init Shows the basic syntax for using npm init with package initializers or scopes. ```bash npm init (same as `npx create-`) npm init <@scope> (same as `npx <@scope>/create`) ``` -------------------------------- ### Example: Caret Dependency Below 1.0.0 Source: https://docs.npmjs.com/cli/update Shows that for caret dependencies below 1.0.0, npm update installs the exact version if it satisfies the constraint (e.g., '^0.2.0' installs '0.2.0'). ```json { "dependencies": { "dep1": "^0.2.0" } } ``` -------------------------------- ### Run npm init questionnaire Source: https://docs.npmjs.com/about-package-json-and-package-lock-json-files Use 'npm init' to create a package.json file by answering a series of questions in the command line. ```bash npm init ``` -------------------------------- ### Install a local package for testing Source: https://docs.npmjs.com/creating-and-publishing-scoped-public-packages Install your local package using its file path to test it before publishing. Replace '/path/to/my-test-package' with the actual path. ```bash npm install /path/to/my-test-package ``` -------------------------------- ### Install a package globally without sudo Source: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally After configuring npm to use a new directory, test your setup by installing a package globally without needing administrator privileges. ```bash npm install -g npm-check-updates ``` -------------------------------- ### Example: npm fund in a project with workspaces Source: https://docs.npmjs.com/cli/v11/commands/npm-fund Demonstrates the output of `npm fund` when run in a project with configured workspaces. ```bash $ npm fund test-workspaces-fund@1.0.0 +-- https://example.com/a | | `-- a@1.0.0 | `-- https://example.com/maintainer | `-- foo@1.0.0 +-- https://example.com/npmcli-funding | `-- @npmcli/test-funding `-- https://example.com/org `-- bar@2.0.0 ``` -------------------------------- ### Example: Caret Dependency Below 1.0.0 (Higher Version) Source: https://docs.npmjs.com/cli/update Demonstrates that for caret dependencies below 1.0.0, npm update installs the highest satisfying version if a newer patch is available (e.g., '^0.4.0' installs '0.4.1'). ```json { "dependencies": { "dep1": "^0.4.0" } } ``` -------------------------------- ### Example Package JSON with Caret Dependency Below 1.0.0 Source: https://docs.npmjs.com/cli/v11/commands/npm-update Demonstrates a caret dependency on a version below 1.0.0. npm installs the highest-sorting version that satisfies the specified range. ```json "dependencies": { "dep1": "^0.2.0" } ``` -------------------------------- ### Example Package JSON with Caret Dependency Source: https://docs.npmjs.com/cli/v11/commands/npm-update Illustrates a package.json with a caret dependency on 'dep1'. When updated, npm installs the latest version of 'dep1' that satisfies the caret constraint. ```json "dependencies": { "dep1": "^1.1.1" } ``` -------------------------------- ### Create a new React project Source: https://docs.npmjs.com/cli/v11/commands/npm-init Use npm init with create-react-app to scaffold a new React-based project in a specified directory. ```bash $ npm init react-app ./my-react-app ``` -------------------------------- ### Example package.json structure Source: https://docs.npmjs.com/about-package-json-and-package-lock-json-files A basic example of a package.json file including name, version, and author fields. Ensure 'name' is lowercase without spaces and 'version' follows semantic versioning. ```json { "name": "my-awesome-package", "version": "1.0.0", "author": "Your Name (https://example.com)" } ``` -------------------------------- ### Example: Tilde Dependency Update Source: https://docs.npmjs.com/cli/update Illustrates how npm update handles a tilde dependency ('~1.1.1'), installing the highest version ('1.1.2') that satisfies the constraint (>=1.1.1 <1.2.0). ```json { "dependencies": { "dep1": "~1.1.1" } } ``` -------------------------------- ### View Nested Field: Repository URL Source: https://docs.npmjs.com/cli/v11/commands/npm-view Access nested fields by separating them with a period. This example shows how to get the git repository URL for the latest version of npm. ```bash npm view npm repository.url ``` -------------------------------- ### Example Package JSON with Tilde Dependency Source: https://docs.npmjs.com/cli/v11/commands/npm-update Shows a package.json with a tilde dependency on 'dep1'. npm will install the highest-sorting version that satisfies the tilde constraint, which may not be the 'latest' version. ```json "dependencies": { "dep1": "~1.1.1" } ``` -------------------------------- ### Example package.json scripts for npm version Source: https://docs.npmjs.com/cli/v11/commands/npm-version This example demonstrates how to use 'preversion', 'version', and 'postversion' scripts in package.json to automate tasks like testing, building, and deploying during the versioning process. ```json { "scripts": { "preversion": "npm test", "version": "npm run build && git add -A dist", "postversion": "git push && git push --tags && rm -rf build/temp" } } ``` -------------------------------- ### Extract All Fields from Array Objects Source: https://docs.npmjs.com/cli/v11/commands/npm-view Requesting a non-numeric field on an array will return all corresponding values from the objects within that array. Examples show getting all contributor emails and names. ```bash # Get all contributor emails npm view express contributors.email # Get all contributor names npm view express contributors.name ``` -------------------------------- ### Enable Output of Lifecycle Scripts Source: https://docs.npmjs.com/cli/v11/using-npm/logging Set the `foreground-scripts` configuration to `true` to enable the logging of output from lifecycle scripts during `npm install`. This behavior was hidden by default starting from npm v7. ```bash npm config set foreground-scripts true ``` -------------------------------- ### Install from GitHub repository (user/repo format) Source: https://docs.npmjs.com/cli/install Installs a package directly from a GitHub repository using the user/repo format. ```bash npm install mygithubuser/myproject ``` -------------------------------- ### List All Configuration Settings Source: https://docs.npmjs.com/cli/config Displays all current configuration settings. Use the -l flag to include defaults or --json to format the output as JSON. ```bash npm config list ``` -------------------------------- ### View Specific Element in an Array Field Source: https://docs.npmjs.com/cli/v11/commands/npm-view Use numeric indices in square brackets to select a specific item from an array field. This example gets the email address of the first contributor. ```bash npm view express contributors[0].email ``` -------------------------------- ### Example package.json Dependencies Source: https://docs.npmjs.com/cli/v11/commands/npm-outdated Illustrates the dependencies section in a package.json file that would produce the example npm outdated output. ```json { "glob": "^5.0.15", "nothingness": "github:othiym23/nothingness#master", "npm": "^3.5.1", "once": "^1.3.1" } ``` -------------------------------- ### Basic Dockerfile for Node.js application Source: https://docs.npmjs.com/docker-and-private-modules This Dockerfile sets up a basic Node.js environment, copies package.json, installs dependencies, copies source files, and starts the application. It is not suitable for private npm packages. ```docker FROM node COPY package.json package.json RUN npm install # Add your source files COPY . . CMD npm start ``` -------------------------------- ### Example Package JSON with Caret Dependency Below 1.0.0 (Higher Version) Source: https://docs.npmjs.com/cli/v11/commands/npm-update Illustrates a caret dependency on '0.4.0'. npm installs '0.4.1' as it's the highest version satisfying the constraint (>= 0.4.0 <0.5.0). ```json "dependencies": { "dep1": "^0.4.0" } ``` -------------------------------- ### Using Client Key for Registry Access Source: https://docs.npmjs.com/cli/v11/using-npm/config Provides an example of how to configure a client key for accessing a registry. The key value should be in PEM format with newlines replaced by '\n'. ```bash key="-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----" ``` -------------------------------- ### Defining Pre, Compress, and Post Scripts Source: https://docs.npmjs.com/cli/v11/using-npm/scripts This example shows how to define pre, post, and main scripts for a custom task like 'compress'. npm automatically executes these in the correct order when 'npm run compress' is invoked. ```json { "scripts": { "precompress": "{{ executes BEFORE the `compress` script }}", "compress": "{{ run command to compress files }}", "postcompress": "{{ executes AFTER `compress` script }}" } } ``` -------------------------------- ### Example: Subdependency Version Resolution Source: https://docs.npmjs.com/cli/update Illustrates how npm update prioritizes a single version of a subdependency ('dep1') that satisfies multiple constraints, installing '1.1.2' to satisfy 'dep2's '~1.1.1' requirement. ```json { "name": "my-app", "dependencies": { "dep1": "^1.0.0", "dep2": "1.0.0" } } ``` ```json { "name": "dep2", "dependencies": { "dep1": "~1.1.1" } } ``` -------------------------------- ### Initialize npm Package Source: https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages Run the npm init command in the package root directory to generate a package.json file. Respond to the prompts to configure your package. ```bash npm init ``` -------------------------------- ### Access Array Elements by Index Source: https://docs.npmjs.com/cli/v11/commands/npm-view Use numeric indices within square brackets to retrieve specific elements from array fields. Examples show getting the email of the first contributor and the name of the second maintainer. ```bash # Get the first contributor's email npm view express contributors[0].email # Get the second maintainer's name npm view express maintainers[1].name ``` -------------------------------- ### Set npm configuration via command line flags Source: https://docs.npmjs.com/cli/v11/using-npm/config Use command-line flags to set npm configuration parameters. For example, `--prefix` changes the directory for npm commands, `--global` installs packages globally, and `--save-dev` saves packages to devDependencies. ```bash npm install --prefix /path/to/dir npm install --global npm install --save-dev ```