The difference between require() and import in Node.js is their origins, syntax, and the context in which they are used.
- require(): Part of the CommonJS (CJS) module system.
- Used in older Node.js projects and environments.
- import: Part of the ES Modules (ESM) system.
- Introduced in ECMAScript 2015 (ES6) and is the modern standard for JavaScript.
- require()
- Function syntax:
const module = require('module-name')
- import:
- Declarative syntax:
import module from 'module-name'
require()
: Automatically resolves.js
.json
, or.node
extensions.import
: Needs extensions in some cases, especially when using ESM (e.g.,import module from './module.js';
).
require()
: Synchronous. Modules are loaded and executed at runtime.import
: Support asynchronous loading natively in ESM environments.
require()
:- Works in Node.js without additional configuration.
- Not natively supported in browsers.
import
- Used in modern JS and browsers.
- Needs
javascript "type": "module"
in the package.json for Node ESM support.
require():
Can be used dynamically within functions or blocks:
if (condition) {
const module = require('module-name')
}
import
: is static and must be declared at the top of the file. Dynamic imports are supported viaimport()
:
if (condition) {
const module = await import('module-name')
}
require()
: Handles default exports directly.
const module = require('./module')
import
: Explicit handling of default and named exports:
import defaultExport from './module.js'
import { namedExport } from './module.js'
Use require() for legacy projects or when working in CommonJS environments. Use import for new and modern projects that leverage ES Modules for better compatibility with browsers and modern tooling.
In this section, we looked at how to use __dirname
in Node.js, even with ES modules in the newer versions (14 and later).
__dirname
is a built-in global variable in Node.js that represents the directory name of the current module.
In newer versions of Node.js, __dirname
is not automatically defined when using ES modules. To define it, you can use the following code:
const __dirname = dirname(fileURLToPath(import.meta.url))
Alternatively, you can use the path.dirname()
function with the import.meta.url
property to get the current directory:
import path from 'path'
import { fileURLToPath } from 'url'
const currentDir = path.dirname(fileURLToPath(import.meta.url))
Both approaches achieve the same result, but the first approach using **dirname is generally considered cleaner and more readable. Additionally, the **dirname
syntax is more concise and easier to write than the alternative approach.
In summary, when using ES modules in Node.js, you need to define __dirname manually or use an alternative approach to get the current directory. The choice between the two approaches depends on your personal preference and the specific requirements of your project. And this is it.
This file defines the metadata, dependencies, and scripts for the project.
-
name
:"yelpcamp"
- The name of the project or package.
-
version
:"1.0.0"
- The current version of the project.
-
type
:"module"
- Specifies that the project uses ES modules (i.e.,
import
/export
syntax instead ofrequire
/module.exports
).
- Specifies that the project uses ES modules (i.e.,
-
main
:"src/app.ts"
- The entry point of the application, usually the file where the app starts running. In this case, it’s the
app.ts
file located in thesrc
folder.
- The entry point of the application, usually the file where the app starts running. In this case, it’s the
-
scripts
: Defines custom commands to run in the terminal.dev
:"tsx --env-file=.env app.ts"
- Runs the application in development mode using the
tsx
tool. The--env-file=.env
option loads environment variables from a.env
file.
- Runs the application in development mode using the
start
:"tsx --env-file=.env app.ts"
- Runs the application in a similar way to
dev
. Typically,start
is used for production, but in this case, it runs the app in the same manner asdev
.
- Runs the application in a similar way to
test
:"echo \"Error: no test specified\" && exit 1"
- A placeholder for running tests. Currently, it just prints an error message and exits with a status of
1
, indicating no tests are defined.
- A placeholder for running tests. Currently, it just prints an error message and exits with a status of
-
keywords
:[]
- An empty array, typically used to define search terms for npm or other package management tools.
-
author
:""
- The author of the project. This is left empty here.
-
license
:"ISC"
- The license type for the project.
ISC
is a permissive open-source license.
- The license type for the project.
-
description
:""
- A short description of the project. This is empty here.
These are the packages required for the project to run in production.
-
ejs
:^3.1.10
- A templating engine for rendering HTML pages. Commonly used in Express apps for server-side rendering of views.
-
express
:^4.21.2
- A minimal and flexible Node.js web application framework. It simplifies the creation of web servers and APIs.
-
method-override
:^3.0.0
- Middleware for overriding HTTP methods like
PUT
andDELETE
in environments that don't support them natively (like HTML forms).
- Middleware for overriding HTTP methods like
-
mongoose
:^8.8.4
- An Object Data Modeling (ODM) library for MongoDB and Node.js, providing a straightforward way to interact with MongoDB from JavaScript.
-
morgan
:^1.10.0
- A HTTP request logger middleware for Node.js, commonly used for logging request details in development and production environments.
These are the packages required only for development (e.g., testing, build tools, type definitions).
-
@types/express
:^5.0.0
- TypeScript definitions for the
express
module, enabling type-checking and IntelliSense in TypeScript projects.
- TypeScript definitions for the
-
@types/method-override
:^3.0.0
- TypeScript definitions for the
method-override
module.
- TypeScript definitions for the
-
@types/morgan
:^1.9.9
- TypeScript definitions for the
morgan
module.
- TypeScript definitions for the
-
@types/node
:^22.10.1
- TypeScript definitions for Node.js, which includes types for Node's built-in modules like
fs
,path
, andhttp
.
- TypeScript definitions for Node.js, which includes types for Node's built-in modules like
-
tsx
:^4.19.2
- A tool that enables running TypeScript files directly without compiling them first. It's commonly used for running TypeScript code in a development environment.
This package.json
sets up a TypeScript-based project using express
, mongoose
, morgan
, and other dependencies. It includes a dev
script to start the project in development mode using tsx
and an empty test
script placeholder. The dependencies are divided into regular dependencies (required in production) and devDependencies (required only for development purposes).
For future reference, if you're working with TypeScript, @types/
packages are essential for enabling type-checking and autocompletion for JavaScript libraries.
YelpCamp/
├── views/
│ ├── layouts/
│ │ └── boilerplate.ejs
│ ├── partials/
│ │ └── navbar.ejs
│ ├── home.ejs
│ └── index.ejs
├── utils/
│ ├── ExpressError.ts
│ └── catchAsync.ts
├── app.ts
└── README.md
- Node.js with TypeScript (ESM modules, TSX)
- Express.js web framework
- MongoDB with Mongoose
- EJS templating engine
- JOI for data validation
- Method Override for RESTful routes (PUT, DELETE)
- Morgan for request logging
- Dotenv for environment variables
- TSX watch handles:
- TypeScript compilation
- File watching
- Environment variables
- Auto-restart on changes