For the first few homeworks, I've setup skeleton projects for you. Eventually you'll have to create your own project from scratch. So… in this set of slides, we'll talk about some things were done to set up homeworks 1 through 3. →
package.json
--save
vs --save-dev
eslint
package.json is a file that contains metadata about your project. It tells npm
There's a very comprehensive page on package.json
on npm's site.
package.json
is used for specifying how your code is imported into another file (what name to use, what the license is, etc.).npm install .
As the name implies (uh, of course), package.json is actually a json file (no surprise there). How is that different from an object literal again? →
There are two required properties:
You'll also get warnings on the following, but everything will work even if you don't have these:
description
repository field
Some other data in package.json
includes:
An example minimal package.json
:
{
"name": "my-site",
"version": "1.0.0",
"author": "Joe Versoza",
"private": true,
"dependencies": {}
}
Of course, you don't have to write all of that by hand. You can use npm to create a new one for you! Just run:
npm init
It'll ask you a bunch of questions.
Let's give it a try →
(remember, name is all lowercase with underscores and dashes)
{
"name": "projectname",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
You can manually specify dependencies. The dependencies property is an object with:
{
"name": "foo",
"version": "1.0.0",
"dependencies": {
"request": "^2.44.0"
}
}
A list of example version specifiers is included npm's documentation. Some common ones include:
version
- exact version^version
- compatible with version>version
- greater than version1.2.x
- all 1.2.x versions*
- matches any versionHow do I install the request module? →
npm install request
We could then manually add that to our package.json
if it's a dependency of our project… just drop it into the dependency
property.
As we're developing, we may want to keep track of all of the packages/modules that we've installed.
Using the --save
flag will add it to your package.json
automatically.
npm install request --save
Let's see how that works. →
There's also a --save-dev
flag. This flag saves what you just installed to the devDependencies property.
A linter is a program that performs static analysis on your code to determine if there is any use of suspicious or non-standard language constructs.
Static analysis is the inspection of the text of your program without actually executing it.
As you know, the linter that we're using is ESLint. What are some issues that it reports on that could potentially lead to errors? →
===
vs ==
var
So, linters have actually moved beyond just detecting potential problems/errors in code. They also can: →
lint was actually the name of a program that would check your C code for potential errors (errors such as dividing by zero, using variables before they're set, etc.).
Of course, linters are no longer just for C. There are linters for other languages (and pretty popular with dynamically typed languages as well):
There are a actually a bunch of linters for JavaScript, not just ESLint:
The trend seems to be shifting towards ESLint in the last year (argh, JavaScript fatigue is real).
You can install ESLint through npm (of course).
npm install eslint --save-dev