Tutor to create a node project
Quick intro guide for someone who is a backend engineer
node.js?
node.js is an open-source js runtime env based on chrome v8 js engine. With which, you can execute js script by node your.js
.
npm vs yarn
Both of them can be used to install node modules or packages. npm is short for node package manager, shipped default with node. yarn is yet another resource navigator. Yarn is modern than npm as it’s invented at 2016. So it’s recommended than npm.
package.json
Package.json is like maven pom file or build.sbt file which is a meta description file of the project. It describes what the dependencies are and how the project can be built, run, etc. You can learn the file structure on the official website. It’s the most accurate and helpful way.
js module
create your first node project(a cli project)
-
create the project folder with
mkdir your-project
. -
cd into the project folder and run
yarn init
. This will prompt the basic info like name, version of your project and create package.json file for you. -
use
yarn add library
to add dependencies to package.json file and node_modules directory. -
start coding your js files.
-
setup scripts and bin in package.json file. Under scripts, you can add your own command to execute with yarn. bin can help u install your script to PATH and call it as a cli command. For example,
{ "scripts": { "start": "node index.js" }, "bin": { "myapp": "./index.js" } }
After
chmod +x index.js
andyarn link your-project
, you can executemyapp
command directly in console. I’d like to use a shell script to call the js script as I don’t like to add shell bang(#!/usr/bin/env node) to a js file.
example project