A step by step beginner tutorial.
-
git init # or # git clone <git repository url>
-
gitignore.io is a great tool to generate custom
.gitignore
files that follows the community best practices.Just select your OS, IDE and language (or combination of those) and save the output as your
.gitignore
file.npx gitignore-dot-io node macos visualstudiocode # or use http://gitignore.io/ to create the file
-
echo "console.log('hello world 👋 👋 👋')" > index.js
-
node index.js
-
Node.js native
http
-
Request API - https://nodejs.org/api/http.html#http_class_http_clientrequest
Response API - https://nodejs.org/api/http.html#http_class_http_serverresponse
Good guide: https://stackabuse.com/node-http-servers-for-static-file-serving/
-
path
native module - https://nodejs.org/api/path.html -
use
fs
module to find file existence -
use
fs
module to open a file stream and pipe it to the http response object
-
CommonJS - exporting
-
CommonJS - consumption
-
npm init # or 'npm init -y' for skipping interactive init (replay yes to all)
-
ESLint is highly recommend on every JavaScript project we will add it to our project as a development dependency.
npm install eslint --save-dev
Notice the
package.json
has been updated with a"devDependencies"
field,package-lock.json
file been created, and lastly thenode_module
folder (should be greyed out on the IDE and SHOULD NOT be committed to the repo). -
The
package.json
can describe bin files, those are mounted by npm for use as cli tools.npx eslint --init ##### Those are the answers given in this tutorial # ? How would you like to use ESLint? To check syntax and find problems # ? What type of modules does your project use? CommonJS (require/exports) # ? Which framework does your project use? None of these # ? Does your project use TypeScript? No # ? Where does your code run? Node # ? What format do you want your config file to be in? JavaScript
npx
is a npm package runner command, it looks for bin file on./node_modules/.bin/
folder, if none is found then npm downloads it and run a package temporarily.Now we can run eslint on our project.
npx eslint
-
npm accept
-g
flag for managing global dependencies, this is not recommended as at not reflected as a dependency on thepackage.json
file, the following examples are for educational purposes.npm install -g eslint
Now
eslint
is available on our terminaleslint .
And and don't forget to uninstall it
npm remove -g eslint
We can add scripts to our package.json
file under the "scripts"
section.
-
By default we have the
"test"
script but we can add any script, npm also supportpre
andpost
prefix for running scripts preceding or following scripts. -
npm run lint
-
npm install express
Notice the
package.json
has been updated with a"dependencies"
field,package-lock.json
file been updated as well. -
Notice that now files are serve with the correct mime type.