Home

How I Begin New JavaScript Projects

These are the first steps I take when I start a new JavaScript project.

I found myself writing and rewriting this process on so many blog posts that it felt like it deserved a post of its own.

These are the steps I take when I am starting a new JavaScript project, whether as a client-side script or a server-side project with Node.js.

Step 1: Create the Project Directory

I like to keep all my code projects together on my machine. To do so I created a directory called workspace within my home folder.

What's inside the workspace directory mimics the structure of repositories on GitHub. For example, the code for this website is at @seancdavis/seancdavis-com on GitHub. That means it lives at ~/workspace/seancdavis/seancdavis-com on my machine.

note

The tilde character ~ represents your home directory.

The first thing I do when starting a new project is create a directory for the code and navigate to that directory. Here's an example:

$ cd ~/workspace/seancdavis
$ mkdir new-project
$ cd new-project
note

If the code is just a quick proof of concept I'll throw it in the /tmp directory, which gets automatically cleaned out. Otherwise, it goes in ~/workspace.

What if you don't plan to put the code on GitHub? Put everything on GitHub that isn't a throwaway proof-of-concept on GitHub! You won't regret it.

Step 2: Add package.json

Next, I add an empty package.json file. The npm command has a handy init option. And if you pass the -y flag to it, you'll end up with a valid-but-mostly-bare package.json file.

$ npm init -y

That will create a package.json file that looks something like this:

package.json

{
"name": "new-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
note

Sometimes I change the values right away. Other times I leave it alone and come back when I get closer to publishing the project.

Step 3: Setup Git

Then I initialize the git repository.

$ git init

And then I make sure I'm ignoring the node_modules directory by adding a .gitignore file:

.gitignore

node_modules
note

It's good idea to do this step before installing any packages. That makes it a lot easier to not accidentally commit code in node_modules.

Step 4: Install Dependencies

Next, install the dependencies you need. Usually I know the first few packages I'm going to start with. This will vary from project to project.

$ npm install ...

Step 5: Add Scripts

There's almost always some sort of build or run process for the code. I tend to set those up next. They may change over the course of the project, and that's okay. But it's a place to start.

For example, if it's a Node project, I'll probably start by adding a build script that runs the node command against an index.js file. That would look like this:

package.json

{
// ...
"scripts": {
"build": "node index.js"
}
}

Then I'd be able to run the build through the npm command:

$ npm run build

Step 6: Get Building!

Then it's time to get down to business and make that thing awesome. Or play around for awhile and throw it away. Whatever your case may be, I hope you have a little fun along the way!

Let's Connect

Keep Reading

WTF is Node.js?

An brief introduction to Node.js, along with links to dig in further with a tutorial or course.

Jun 29, 2020

Developer Advice: Put Everything on GitHub

Some of the most valuable advice I've received is to put my code out their for the world to read, no matter how embarrassing it may be.

Jun 08, 2020

Compile ES6 Code with Gulp and Babel, Part 4

In the fourth of five parts on compiling multiple ES6 files into a minified bundle, you will learn how to minify your bundle and automatically clean up temporary build files.

Dec 20, 2018