Mac os npm install global

Как установить Node.js и NPM в Mac OS

Node JS – популярная платформа Javascript, которая широко используется разработчиками, а npm – это аккомпанирующий её менеджер пакетов. Когда вы установите Node.js, npm установится автоматически. Соответственно, если вам нужен npm, придётся установить и NodeJS.

Есть несколько способов установить Node.js и NPM на Mac, включая использование установщика и использование Homebrew. В данной статье мы расскажем об обоих способах, которые должны работать во всех современных версиях MacOS.

Как установить Node.js и NPM через Homebrew

Самый простой способ установить node.js и npm – через утилиту командной строки Homebrew. Естественно, для этого у вас должен быть установлен менеджер пакетов Homebrew. Кроме того, не помешает обновить программу перед тем, как приступать к инструкции. Выполните следующую команду:

После этого в программе Terminal выполните такую команду, чтобы установить сразу Node.js и npm:

Данный способ самый простой из всех, и с его помощью node.js и npm легко обновлять. Кроме того, удалить их будет так же просто.

Как установить Node.js и NPM через установщик

Если вы по какой-то причине не хотите использовать Homebrew, можете использовать установщик с сайта nodejs.org:

Запустите установщик, как любую другую программу на Mac.

Как узнать, установлены ли NPM и Node.js на Mac

Вы можете узнать, установлены ли node.js и npm на вашем компьютере, использовав следующие команды:

Как проверить работу Node.js

Когда программная платформа node.js будет установлена, вы можете проверить его работу, создав простой сервер. Создайте файл с названием «app.js» со следующим кодом:

Сохраните файл app.js, а затем запустите сервер следующей командой:

Затем откройте браузер и перейдите по этой ссылке:

Вы должны увидеть сообщение « Hello from Node.js».

Это самый простой веб-сервер, созданный с помощью node.js. Вы также можете установить программу Grunt CLI для тестирования node и npm с помощью такой команды:

После этого запустите в командной строке «grunt».

Выше мы рассказали о самых простых способах установить NodeJS и npm на Mac. Надеемся, они вам помогут.

Источник

LearnInstall Node.js and NPM on a Mac: How-To Guide

Dave McFarland
writes on October 8, 2014

If you’re looking to take your JavaScript coding to another level, Treehouse offers unlimited-access courses in JavaScript (and many other subjects) starting at $25/month. Try our program out with a free seven-day trial today.

JavaScript is one of the most popular programming languages in the world. Because it’s built into most web browsers, programmers and web designers can use JavaScript to add interactive features to websites that reach billions of people. But in the past couple of years, JavaScript has started to play a larger role outside of the browser, due in large part to Node.js.

Node.js is a tool for building fast network applications. It’s known as a “JavaScript runtime environment” which simply means it lets you write JavaScript code that can run on your computer free of any web browser. Node.js is used to create fast web servers by companies like Walmart, eBay and Netflix.

But because Node.js can be used on your desktop computer, programmers have created useful Node-based tools that help with the process of building web sites. For example, Grunt is a popular tool used to automate common tasks like compiling Sass files to CSS, making JavaScript files smaller so they load in less time, and compressing images to smaller file size. While these tools run through the Node.js environment, you’ll use another tool, NPM, to install them. NPM is what’s called a “package manager.” NPM makes installing a tool like Grunt as easy as npm install -g grunt-cli .

Читайте также:  Windows 10 профессиональная системные требования

But before you can use Node.js or NPM you need to install them — while the NodeJS website includes an installer, there’s a better way to install them on a Mac. In this article, I’ll take you through the process of installing Node.js and NPM on a Mac using Homebrew. In another article, I’ll show you Windows users how to install them on the Windows operating system.

Prerequisites

Before you install Node.js and NPM you’ll first need to have some familiarity with the Mac Terminal application. Terminal lets you dig into the underbelly of the operating system and issue text commands to your computer. You’ll need to use Terminal (or a similar application like iTerm) to not only install Node.js but also to use it and NPM.

Before you can install Node, you’ll need to install two other applications. Fortunately, once you’ve got these on your machine, installing Node takes just a few minutes.

  1. XCode. Apple’s XCode development software is used to build Mac and iOS apps, but it also includes the tools you need to compile software for use on your Mac. XCode is free and you can find it in the Apple App Store.
  2. Homebrew. Homebrew is a package manager for the Mac — it makes installing most open source sofware (like Node) as simple as writing brew install node . You can learn more about Homebrew at the Homebrew website. To install Homebrew just open Terminal and type ruby -e «$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)» . You’ll see messages in the Terminal explaining what you need to do to complete the installation process.

Why Homebrew?

Observant readers will notice in the screenshot above that there’s an installer for NodeJS. You can download it directly from NodeJS.org. I recommend Homebrew over that installer for a few reasons:

  1. When installing Node via the installer, you have to use the sudo command to make sure it installs properly (there is a workaround for this, but it’s complicated). sudo lets the installer place files in areas of your file system that are only accessible to administrators. One nice thing about Homebrew is that it doesn’t require access to administrator-only areas of your computer in order to install NodeJS (or any other package). This is a safer approach as it makes sure that any package you install with Homebrew can’t wreak havoc on your computer.
  2. After installing Node via the installer you have to add the path to the node executable to your system $PATH. This involves mucking around with your shell login file. For experienced Terminal users or Unix-people this isn’t a big deal, but for those new to the command line that step can be daunting. Although the Homebrew method involves installing several different pieces of software, it’s generally just a simple process of point-click-and-wait. It takes a bit longer this way but there’s less room for error.
  3. Homebrew is a great tool for web developers. First, it makes removing Node very easy (otherwise you have to crawl through your file system and delete a bunch of files manually). Second, it greatly simplfies the installation of other useful packages like Git, Ruby, or the very useful wget utility.

Installation

Installing Node.js and NPM is pretty straightforward using Homebrew. Homebrew handles downloading, unpacking and installing Node and NPM on your system. The whole process (after you have XCode and Homebrew installed) should only take you a few minutes.

  1. Open the Terminal app and type brew install node .
  2. Sit back and wait. Homebrew downloads some files and installs them. And that’s it.
Читайте также:  Базовый набор windows 10

To make sure you have Node and NPM installed, run two simple commands to see what version of each is installed:

  • To see if Node is installed, type node -v in Terminal. This should print the version number so you’ll see something like this v0.10.31 .
  • To see if NPM is installed, type npm -v in Terminal. This should print the version number so you’ll see something like this 1.4.27

Check and Update Your Node and NPM Versions with Homebrew

New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.

  1. Make sure Homebrew has the latest version of the Node package. In Terminal type brew update
  2. Upgrade Node: brew upgrade node

How to Uninstall Node and NPM

You can use Homebrew to uninstall packages that it installed: brew uninstall node

With Node.js and NPM installed you’ll soon be able to take advantage of the huge world of NPM modules that can help with a wide variety of tasks both on the web server and on your desktop (or laptop) machine. The NPM site lists all of the official Node packages making it easy to make the choice. Have fun and check out my current courses at Treehouse.

Learn with Treehouse

Learning with Treehouse starts at only $25 per month. If you think you’re ready to start exploring if tech is right for you, sign up for your free seven day trial.

What sets Treehouse apart is their dedication to helping you find your perfect job or develop your own business. – SwitchUp.org

Follow us on Twitter, Instagram, and Facebook for our favorite tips, and to share how your learning is going. We’ll see you there!

If you liked reading this article, you should also look at these two:

100 Responses to “Install Node.js and NPM on a Mac: How-To Guide”

‘brew install node’ installed node 10.1
‘npm -v’ was 5.6 although 6.01 is the most recent
‘brew upgrade npm’ did not upgrade to latest version of npm
‘npm install -g npm ‘ Will upgrade to the latest version of npm
Hope this helps someone else!

i am getting error when i checking the version npm -v
ERROR: npm is known not to run on Node.js v0.10.40
You’ll need to upgrade to a newer version in order to use this
version of npm. Supported versions are 4, 6, 7, 8, 9. You can find the
latest version at https://nodejs.org/

What confuses me is that I have npm all set up in some projects at work and it works fine. However, I just downloaded a new project and when I run “npm -v” in that project, I get “npm: command not found”. I can’t seem to find any docs on installing node per project. They all seem to be global, including this one.

So how is it npm works in one project but not another? And if there is a way to install it per project, how is that done?

You need node.js and npm installed on your home machine, too. “Command not found” means it’s not installed. Follow the instructions above, and you’ll have everything you need to destroy your free time with work stuff.

You need to install NodeJS globally. NPM comes with it and that is how you’re going to be building your project dependancies. Now if you don’t want to install all those packages that your project requires, then you’re going to want to have your JSON file call them up as devDependancies. Take a look at the link below, it might explain it better than I can.

Читайте также:  Sleeping dogs windows 64 bit

Источник

rcugut / node-npm-install.md

Fixing npm On Mac OS X for Homebrew Users

Installing node through Homebrew can cause problems with npm for globally installed packages. To fix it quickly, use the solution below. An explanation is also included at the end of this document.

This solution fixes the error caused by trying to run npm update npm -g . Once you’re finished, you also won’t need to use sudo to install npm modules globally.

Before you start, make a note of any globally installed npm packages. These instructions will have you remove all of those packages. After you’re finished you’ll need to re-install them.

Run the following commands to remove all existing global npm modules, uninstall node & npm, re-install node with the right defaults, install npm as its own pacakge, and configure the location for global npm modules to be installed.

1. Re-install node without npm

You should now be able to run the node command

2. Prepare the npm new home: /usr/local/npm_packages

Add/append the following lines in

And make sure you load the .bashrc in your .bash_profile :

  • IMPORTANT: make sure you do not have both a .profile and a .bash_profile ; if you do, copy everything from .profile into .bash_profile and then remove the .profile
  • Check if the following or similar line exists in your .bash_profile :
  • make sure .bashrc is executable

3. Restart your iTerm / Terminal

IMPORTANT: close all existing iTerm/Terminal sessions and re-open iTerm/Terminal before going further

Advanced: or just reload the .bash_profile — source

3. Install npm from npmjs.com

First, set the correct npm home in

/.npmrc file exists, and contains a line as below:

Run the npm install script from npmjs.com

NOTE @ July 13 2017: the install script from above (from npmjs.com) silently fails and doesn’t copy the npm package contents correctly (see comments below from @hughsw). In order to (hack)fix this you can do the following after running the above npm install.sh script.

The instructions below apply to npm 5.2.0 , but may work for earlier or later 5.x releases:

That’s it. You should now be able to run the npm command without sudo .

NEVER run npm as sudo; so never do sudo npm install .

Explanation of the issue

If you’re a Homebrew user and you installed node via Homebrew, there is a major philosophical issue with the way Homebrew and NPM work together. If you install node with Homebrew and then try to do npm update npm -g , you may see an error like this:

There’s an NPM bug for this exact problem. The bug has been «fixed» by Homebrew installing npm in a way that allows it to manage itself once the install is complete. However, this is error-prone and still seems to cause problems for some people. The root of the the issue is really that npm is its own package manager and it is therefore better to have npm manage itself and its packages completely on its own instead of letting Homebrew do it.

Also, using the Homebrew installation of npm will require you to use sudo when installing global packages. Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.

Источник

Оцените статью