I have ran >node init and see that I can create
this package.json file, what does this do exactly?
npm init
is used to create a package.json file interactively. This will ask you a bunch of questions, and then write a package.json for you.
package.json
is just a file that handle the project's dependencies and holds various metadata relevant to the project[ project description, version, license information etc]
I was told that I need to add dependencies. Could someone please
explain to me what this means in Node terms?
Lets say you're building an application that is dependent on a number of NPM modules, you can specify them in your package.json file this way:
"dependencies": {
"express": "2.3.12",
"jade": ">= 0.0.1",
"redis": "0.6.0"
}
Now doing npm install
would install a package, and any packages that it depends on.
A package is:
- a folder containing a program described by a package.json file
- a gzipped tarball containing (1)
- a url that resolves to (2)
- a @ that is published on the registry with (3)
- a @ that points to (4)
- a that has a "latest" tag satisfying (5)
- a that resolves to (2)
If you need to install a dependency that haven't been included in package.json, simply do npm install <packageName>
. Whether or not you want to include this newly installed package in package.json is completely your take. You can also decide how this newly installed package shall appear in your package.json
npm install <packageName> [--save|--save-dev|--save-optional]:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
Does a dependency simply mean a folder that I want node to include?
Umm, partly yes. You may consider dependencies as folders, typically stored in node_modules
directory.
Do I want to add this Node folder on my Desktop to be able to run my
scripts?
No, node manages it all. npm install
will automatically create node_modules
directory and you can refer to those dependencies with
require() in your .js files
var express = require('express');
Node REPL simply provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out.
process.cwd()
points to the directory from which REPL itself has been initiated. You may change it using process.chdir('/path')
, but once you close the REPL session and restart, it would always re-instantiate process.cwd()
to the directory from which it has been started.
If you are installing some packages/dependencies in node project1 and think those dependencies can also be useful for node project2,
- install them again for project2 (to get independent
node_modules
directory)
- install them globally [using -g flag]. see this
- reference packages in project2 as
var referencedDependency = require('/home/User/project1/node_modules/<dependency>')
Simply doing process.chdir('/home/User/project1/node_modules/')
in REPL and referencing as
var referencedDependency = require('<dependency>')
in your js file wont work.
>process.chdir('/Users/MyUser/Desktop/Node/');
change the working directory only for that particular REPL session.
Hope it helps!