12

I'm new to NodeJS and I'm having trouble understanding npm

I'm following this tutorial and I can't seem to map the concepts tot he ones I know from my Django experience.

Where exactly do my packages get installed? Can't I have project-specific packages like Django does with virtualenv? If I understand correctly this might cause compatibility issues in the future.

Zaham2
  • 337
  • 1
  • 4
  • 11

3 Answers3

21

npm is like pip in Python, its a way to download and install packages. node_modules is a directory where these packages are installed. This is not the same as a virtual environment; which has the interpreter along with additional libraries.

In node, you can get a virtual environment (see: is there an virtual environment for node.js? for more details). The idea is the same - an isolated environment for better testing and portability.

In Python, there is requirements.txt (and pipenv), in node you have package.json (for packages), and modules (which go in node_modules).

The documentation goes into a bit more detail into the difference of these; but coming from Python you can think of a node package as something that has a package.json (so, like a setup.py), and a module is just any file you can import (or include() in node).

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    This is exactly the answer I was looking for. Something to help map between the two. Many thanks. – Zaham2 Dec 22 '19 at 13:25
0

Hey when you install a package with npm the packages are installed to node_modules within the working directory (on a project to project basis).

With the exception of when installing packages globally (npm install -g packagename)

IndieBlock
  • 48
  • 5
0

To add to the excellent answer above, and to confirm if this suspicion of mine is true: The major difference between node_modules not creating a true virtual environment is that whenever you run a node application, it will "reach_out" to use the node/js interpreter that is installed "globally" .. this of course can be manipulated with nVm, but python's virtual environments don't run on a "globally" installed python interpreter, instead with the one scoped-narrowly to the virtual environment