package.json
Contains info on package...
npm init
myproject
cd myproject
npm init
{
"name": "myproject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Sequoia McDowell <sequoia.mcdowell@gmail.com> (http://sequoia.makes.software/)",
"license": "ISC"
}
init.author.name=Sequoia McDowell
init.author.email=sequoia.mcdowell@gmail.com
init.author.url=http://sequoia.makes.software/
npm config
npm config set init.author.name [your name]
npm config set init.author.email [your email]
cat ~/.npmrc
In myproject/
:
index.js
console.log('hello world')
in itnode .
" to run index.js
index.js
app.js
node .
" to run app.js
npm install
npm install
npm install foo
: installs packagenpm install --save foo
: ... & saves dependencynpm remove [--save] foo
: remove packagenpm install
: installs this packageIn myproject/
:
npm install chalk
Use our dependency:
//npm/start/myproject/app.js
console.log(chalk.green.underline('Hello World!'));
console.log(chalk.red.bold('Hello Again!'));
Run the file
Hints:
In myproject/
:
rm -rf node_modules
npm install
node .
In myproject/
:
npm install --save chalk
package.dependencies
List of packages (and versions) required by our package
...
"dependencies": {
"chalk": "^1.1.1"
}
...
node_modules/chalk/package.json
...
"dependencies": {
"ansi-styles": "^2.1.0",
"escape-string-regexp": "^1.0.2",
"has-ansi": "^2.0.0",
"strip-ansi": "^3.0.0",
"supports-color": "^2.0.0"
},
...
node_modules/chalk/package.json
...
"devDependencies": {
"coveralls": "^2.11.2",
"matcha": "^0.6.0",
"mocha": "*",
"nyc": "^3.0.0",
"require-uncached": "^1.0.2",
"resolve-from": "^1.0.0",
"semver": "^4.3.3",
"xo": "*"
}
...
"Semantic Versioning"
3.10.23
3
: Major Version10
: Minor Version23
: Patch Version>=2.3
2.*
1.2.7
~3.1
(3.1.*
)^3.1.9
(3.*
+ >=3.1
)npm search
Up Next: Global Installs
npm install -g foo
Installs to privileged location (/usr/local/bin
)
Make NPM install globals in our home directory
npm set prefix /your/home/npm_packages
Don't do this if you used nvm
to install node!!
npm install -g nodemon
nodemon /path/to/myproject
myproject/app.js
& save the filepackage.bin
Like package.main
but for command-line usage:
"main": "index.js",
"bin" : "bin/index.js"
"bin" : {
"ccc" : "dist/cli/do_this.js",
"jsfoo":"dist/cli/foo.js"
}
Up Next: npm scripts
https://docs.npmjs.com/misc/scripts
package.json
npm run scriptname
...
"scripts": {
"bbl-build": "babel src --out-dir dist",
"build": "npm run bbl-build",
"postinstall": "npm run build",
"start": "node . --httpauth ../presentations.htpasswd",
"debug": "PORT=3030 DEBUG=app:*,express:* npm run start",
"pretest": "echo \"about to start tests...\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
...
In myproject/
:
cowsay
local to projectpackage.json
that runs cowsay Hello
npm run hello
Hints:
npm run hello
runs a script named hello