Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use npm for JavaScript Development on macOS

npm (Node Package Manager) is an essential tool for JavaScript developers, allowing them to manage project dependencies, install packages, and run scripts. While npm is not exclusive to any operating system, this article will focus on how to use npm effectively in the macOS environment. Understanding how to leverage npm can significantly streamline your development process and improve productivity.


Examples:


1. Installing Node.js and npm on macOS:


To use npm, you first need to install Node.js, which includes npm by default. Follow these steps:




  • Open Terminal.




  • Install Homebrew if you haven't already:


    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"



  • Use Homebrew to install Node.js:


    brew install node



  • Verify the installation:


    node -v
    npm -v



2. Creating a New Project with npm:




  • Open Terminal and navigate to your desired directory:


    cd path/to/your/directory



  • Initialize a new project:


    npm init

    Follow the prompts to set up your package.json file.




3. Installing Packages:




  • To install a package (e.g., Express), use:


    npm install express



  • To install a package globally:


    npm install -g nodemon



4. Running Scripts:




  • Add a script to your package.json:


    "scripts": {
    "start": "node app.js"
    }



  • Run the script:


    npm run start



5. Updating Packages:




  • To update a specific package:


    npm update express



  • To update all packages:


    npm update



6. Uninstalling Packages:




  • To uninstall a package:


    npm uninstall express



To share Download PDF