How to create a new Node.js project

Creating a new Node.js project involves setting up the proper directory structure, configuration files, and development environment for building server-side applications and tools. As the creator of CoreUI, a widely used open-source UI library, I’ve created countless Node.js projects for build tools, CLI utilities, server applications, and development scripts that power our component libraries and enterprise solutions. From my expertise, the most effective approach is to create a structured project with proper initialization and essential configuration files. This method ensures maintainable code organization, proper dependency management, and seamless integration with development tools and deployment pipelines.

Create a new directory, initialize with npm init, and set up basic project structure for Node.js development.

# Create project directory
mkdir my-nodejs-project
cd my-nodejs-project

# Initialize npm project
npm init -y
# or interactive mode
npm init

# Create basic project structure
mkdir src
mkdir tests
mkdir docs
touch src/index.js
touch .gitignore
touch README.md

# Create basic index.js file
cat > src/index.js << 'EOF'
#!/usr/bin/env node

/**
 * Main application entry point
 */

const packageJson = require('../package.json');

console.log(`${packageJson.name} v${packageJson.version}`);
console.log('Hello from Node.js!');

// Basic HTTP server example
const http = require('http');
const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    message: 'Hello World!',
    timestamp: new Date().toISOString(),
    url: req.url,
    method: req.method
  }));
});

if (require.main === module) {
  server.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
  });
}

module.exports = { server };
EOF

# Create .gitignore file
cat > .gitignore << 'EOF'
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Coverage directory used by tools like istanbul
coverage/
*.lcov

# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# IDE files
.vscode/
.idea/
*.swp
*.swo

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
EOF

# Make index.js executable (for CLI tools)
chmod +x src/index.js

# Test the project
node src/index.js

A well-structured Node.js project starts with npm init to create package.json, followed by organizing code into logical directories like src/ for source code, tests/ for testing, and docs/ for documentation. The main entry point typically goes in src/index.js or index.js. Create a comprehensive .gitignore to exclude node_modules, environment files, and system files. Consider adding scripts in package.json for common tasks like start, test, and build. Use consistent naming conventions and include a README.md for project documentation.

Best Practice Note:

This is the same project structure we use for CoreUI Node.js tools and backend services in enterprise environments. Start with a clear directory structure, use semantic versioning in package.json, include proper error handling in your main file, set up linting and formatting tools early, and consider using TypeScript for larger projects.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Use Bootstrap Tooltip in React Without Breaking React – CoreUI Integration Guide
How to Use Bootstrap Tooltip in React Without Breaking React – CoreUI Integration Guide

How to sleep in Javascript
How to sleep in Javascript

What are the three dots `...` in JavaScript do?
What are the three dots `...` in JavaScript do?

How to Hide Scrollbar with CSS
How to Hide Scrollbar with CSS

Answers by CoreUI Core Team