Skip to content

Instantly share code, notes, and snippets.

@ManuKpL
Last active October 20, 2022 00:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManuKpL/36fc140deebe3678f7612b5951a7c6be to your computer and use it in GitHub Desktop.
Save ManuKpL/36fc140deebe3678f7612b5951a7c6be to your computer and use it in GitHub Desktop.
Quick init node project from terminal (JS or TS)

Quickly init empty node project

General information

Nota Bene

  • npm v7 is required to use npm-set-script command (see npm doc)

Dependencies

  • I use run-s for its side effect of using the same package manager as the user to run subsequent scripts while being a cross OS solution as opposed to $npm_execpath (see npm-run-all doc)
  • I use esm for JS project to provide support for esm modules syntax (see esm doc)
  • I use nodemon to run and watch the source code for change in dev mode (see nodemon doc)

Commands

Init a Javascript project

WITH YARN

  yarn init -y
  mkdir src
  echo "console.log('Hello world');" >./src/index.js
  echo "require('./src');" >./index.js
  npm set-script start "node -r esm src"
  npm set-script dev "nodemon --watch src start"
  yarn add esm
  yarn add -D nodemon

WITH NPM

  npm init -y
  mkdir src
  echo "console.log('Hello world');" >./src/index.js
  echo "require('./src');" >./index.js
  npm set-script start "node -r esm src"
  npm set-script dev "nodemon --watch src start"
  npm i -S esm
  npm i -D nodemon

Init a typescript project (without ts-node)

WITH YARN

yarn init -y
mkdir src
echo "console.log('Hello world');" >./src/index.ts
echo "require('./dist');" >./index.js
npm set-script start "node dist"
npm set-script build "tsc --outDir dist"
npm set-script dev:build "tsc --watch --outDir dist"
npm set-script dev:ready "wait-on dist/index.js"
npm set-script dev:start "nodemon --watch dist start"
npm set-script dev "concurrently \"run-s dev:build\" \"run-s dev:ready dev:start\" -n build,run"
yarn add typescript
yarn add -D nodemon wait-on concurrently npm-run-all
yarn tsc --init

WITH NPM

npm init -y
mkdir src
echo "console.log('Hello world');" >./src/index.ts
echo "require('./dist');" >./index.js
npm set-script start "node dist"
npm set-script build "tsc --outDir dist"
npm set-script dev:build "tsc --watch --outDir dist"
npm set-script dev:ready "wait-on dist/index.js"
npm set-script dev:start "nodemon --watch dist start"
npm set-script dev "concurrently \"run-s dev:build\" \"run-s dev:ready dev:start\" -n build,run"
npm i -S typescript
npm i -D nodemon wait-on concurrently npm-run-all
./node_modules/.bin/tsc --init

Init a typescript project (with ts-node)

WITH YARN

yarn init -y
mkdir src
echo "console.log('Hello world');" >./src/index.ts
echo "require('./dist');" >./index.js
npm set-script start "node dist"
npm set-script build "tsc --outDir dist"
npm set-script dev:start "ts-node src"
npm set-script dev "nodemon --watch src --exec \"run-s dev:start\""
yarn add typescript
yarn add -D nodemon ts-node npm-run-all
yarn tsc --init

WITH NPM

npm init -y
mkdir src
echo "console.log('Hello world');" >./src/index.ts
echo "require('./dist');" >./index.js
npm set-script start "node dist"
npm set-script build "tsc --outDir dist"
npm set-script dev:start "ts-node src"
npm set-script dev "nodemon --watch src --exec \"run-s dev:start\""
npm i -S typescript
npm i -D nodemon ts-node npm-run-all
./node_modules/.bin/tsc --init

Use as bash function

Sourced as an alias, it can be used to quickly init your project. Pass a name argument to define a target directory.

initProject() {
  if [ "$#" -ne 1 ]; then
    echo "One argument required: target directory name"
    return 1
  elif [[ -d "./$1" ]]; then
    echo "Target directory already exists: $1"
    return 1
  else
    mkdir $1
    cd $1
    # Your commands of choice go here
  fi
}

Example usage: initProject my-project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment