Tutor to create a node project

frontend

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

https://leecy.me/js-module/

create your first node project(a cli project)

  1. create the project folder with mkdir your-project.

  2. 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.

  3. use yarn add libraryto add dependencies to package.json file and node_modules directory.

  4. start coding your js files.

  5. 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 and yarn link your-project, you can execute myapp 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

notion2ghost