Skip to content

Instantly share code, notes, and snippets.

@juanmanavarro
Created June 5, 2025 09:16
Show Gist options
  • Save juanmanavarro/13b167dadeeec230668a2ca6ff67601f to your computer and use it in GitHub Desktop.
Save juanmanavarro/13b167dadeeec230668a2ca6ff67601f to your computer and use it in GitHub Desktop.
Shell function to initialize a Node.js project: creates a folder under ~/projects, generates an index.js file, initializes npm and Git, creates a .gitignore, makes an initial commit, and opens the file in VS Code.
initnpm() {
if [ -z "$1" ]; then
echo "Usage: initnpm <project_name>"
return 1
fi
mkdir -p ~/projects/"$1" && cd ~/projects/"$1" || return 1
echo "// entry point" > index.js
npm init -y > /dev/null
echo "node_modules/" > .gitignore
git init > /dev/null
git add .
git commit -m "initial commit" > /dev/null
git branch -M main 2>/dev/null || git branch -m main 2>/dev/null
code -g index.js
echo "Project '$1' successfully created and index.js opened in VS Code."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment