My Docs
BlogGithubLinkedin
Blog-Content
Blog-Content
  • Blog
  • README
    • 10. Regular Expression Matching
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 14. Longest Common Prefix
    • 19. Remove Nth Node From End of List
    • 19. Remove Nth Node From End of List
    • 8. String to Integer (atoi)
    • Table of contents
    • All the Things You Can Do With GitHub API and Python
    • Archive
    • Articles
    • Bash Commands That Save Me Time and Frustration
    • Basic Web Development Environment Setup
    • Basic Web Development Environment Setup
    • Blog Archive
    • Blog Archive
    • Blog
    • Bookmarks
    • Cheatsheet:
    • Clock
    • Community
    • Constructor Functions
    • Content
    • Cool Github Profiles
    • Data Structures Interview
    • Docs
    • dynamic-time-warping
    • Embed Showcase
    • Es6 Features
    • Functions
    • Gatsby Paginate
    • Getting Started
    • Google Cloud
    • google-sheets-api
    • History API
    • How to install Python 2.7 on Ubuntu 20.04 LTS
    • HTML SPEC
    • index
    • Installation
    • Installing Node
    • Interactive
    • Intro To NodeJS
    • Intro To React
    • Intro To React
    • Introducing JSX
    • Introduction to npm
    • Javascript and Node
    • Javascript and Node
    • Javascript and Node
    • Javascript Interview Questions:
    • Javascript Interview Questions:
    • Javascript Practice
    • Javascript Practice
    • Javascript Practice
    • JS Fat Arrow Functions
    • Jupyter Notebooks
    • Jupyter Notebooks
    • Leetcode
    • Leetcode
    • Leetcode
    • lorem-ipsum
    • Lorem ipsum
    • Markdown
    • My Favorite VSCode Themes
    • Nature
    • New Conference
    • Node APIs With Express
    • Node APIs With Express
    • Node Buffers
    • Node Docs
    • Node Export Module
    • Node Export Module
    • Node Modules System
    • Node vs Browser
    • Overview
    • Phone Number
    • Process in Linux
    • Pull Requests
    • Python at length
    • Python Quiz
    • Python Quiz
    • Python Snippets
    • Python Snippets
    • React Class Components Demo
    • React Class Components Demo
    • React Docs
    • React In Depth
    • RECENT PROJECTS
    • RECENT PROJECTS
    • Reference
    • Resume
    • Search:
    • Semantic Versioning
    • Showcase
    • Showcase
    • Sorting Algorithms
    • Sorting Strings
    • Starter Theme
    • Starter Theme
    • The Node.js Event Loop
    • The Node.js Event Loop
    • Tools
    • Tools
    • Trouble Shooting
    • Typography
    • UI Components
    • Understanding PATH
    • Understanding PATH
    • URL:
    • Visualizing the Discrete Fourier Transform
    • Web Apis
    • Web Design
    • Web Dev Bookmarks
    • Web Developer Tools
    • What is THIS
    • What is THIS
    • where-is-npm-pack
    • with some basic knowledge of React
    • Zumzi Instant Messenger
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. README

Node Modules System

PreviousNode Export ModuleNextNode vs Browser

Last updated 3 years ago

Was this helpful?

Node.js has a built-in module system.

A Node.js file can import functionality exposed by other Node.js files.

When you want to import something you use

to import the functionality exposed in the library.js file that resides in the current file folder.

In this file, functionality must be exposed before it can be imported by other files.

Any other object or variable defined in the file by default is private and not exposed to the outer world.

This is what the module.exports API offered by the allows us to do.

When you assign an object or a function as a new exports property, that is the thing that's being exposed, and as such, it can be imported in other parts of your app, or in other apps as well.

You can do so in 2 ways.

The first is to assign an object to module.exports, which is an object provided out of the box by the module system, and this will make your file export just that object:

The second way is to add the exported object as a property of exports. This way allows you to export multiple objects, functions or data:

or directly

And in the other file, you'll use it by referencing a property of your import:

or

What's the difference between module.exports and exports?

The first exposes the object it points to. The latter exposes the properties of the object it points to.

Modules in Javascript

Differences between Node.js and browsers


Modules in Javascript

Differences between Node.js and browsers

Global vs Window

Most browsers allow you to reference global but it is really the same object as window.

Document

Browsers have access to a document object that contains the HTML of a page that will be rendered to the browser window. There is no document in Node.

Location

Browsers have access to a location that contains information about the web address being visited in the browser. There is no location in Node, since it is not on the web.

Require and module.exports

Node has a predefined require function that we can use to import installed modules like readline. We can also import and export across our own files using require and module.exports. For example, say we had two different files, animals.js and cat.js, that existed in the same directory:

If we execute animals.js in Node, the program would print ‘Sennacy is a great pet!’.

Browsers don’t have a notion of a file system so we cannot use require or module.exports in the same way.

The fs module

// change-some-files.js

const fs = require("fs");

Similar to what we saw in the readline lesson, require will return to us a object with many properties that will enable us to do file I/O.

Did you know? I/O is short for input/output. It’s usage is widespread and all the hip tech companies are using it, like.io.

The fs module contains tons of functionality! Chances are that if there is some operation you need to perform regarding files, the fs module supports it. The module also offers both synchronous and asynchronous implementations of these methods. We prefer to not block the thread and so we'll opt for the asynchronous flavors of these methods.

Creating a new file

To create a file, we can use the writeFile method. According to the documentation, there are a few ways to use it. The most straight forward way is:

Beware! If the file name specified to writeFile already exists, it will completely overwrite the contents of that file.

We won’t be using the foo.txt file in the rest of this reading.

Reading existing files

To explore how to read a file, we’ll use VSCode to manually create a poetry.txt file within the same directory as our change-some-file.js script. Be sure to create this if you are following along.

Our poetry.txt file will contain the following lines:

My code fails

I do not know why

My code works

I do not know why

We can use the readFile method to read the contents of this file. The method accepts very similar arguments to writeFile, except that the callback may be passed an error object and string containing the file contents. In the snippet below, we have replaced our previous writeFile code with readFile:

Running the code above would print the following in the terminal:

THE CONTENTS ARE:

My code fails

I do not know why

My code works

I do not know why

Success! From here, you can do anything you please with the data read from the file. For example, since data is a string, we could split the string on the newline character to obtain an array of the file's lines:

THE CONTENTS ARE:

[ 'My code fails',

'I do not know why',

'My code works',

'I do not know why' ]

The third line is My code works

File I/O

Using the same poetry.txt file from before:

My code fails

I do not know why

My code works

I do not know why

Let’s replace occurrences of the phrase ‘do not’ with the word ‘should’.

We can read the contents of the file as a string, manipulate this string, then write this new string back into the file.

We’ll need to utilize callback chaining in order for this to work since our file I/O is asynchronous:

Executing the script above will edit the poetry.txt file to contain:

My code fails

I should know why

My code works

I should know why

Refactor:

If you found this guide helpful feel free to checkout my github/gists where I host similar content:

There are many differences between Node.js and browser environments, but many of them are small and inconsequential in practice. For example, in our Asynchronous lesson, we noted how has a slightly different return value from . Let’s go over a few notable differences between the two environments.

In the Node.js runtime, the is the object where global variables are stored. In browsers, the is where global variables are stored. The window also includes properties and methods that deal with drawing things on the screen like images, links, and buttons. Node doesn’t need to draw anything, and so it does not come with such properties. This means that you can’t reference window in Node.

Node comes with an that contains methods that allow us to interact with our computer’s File System through JavaScript. No additional installations are required; to access this module we can simply require it. We recommend that you code along with this reading. Let's begin with a change-some-files.js script that imports the module:

The code above will create a new file called foo.txt in the same directory as our change-some-file.js script. It will write the string 'Hello world!' into that newly created file. The third argument specifies the encoding of the characters. There are different ways to encode characters; is the most common and you'll use this in most scenarios. The fourth argument to writeFile is a callback that will be invoked when the write operation is complete. The docs indicate that if there is an error during the operation (such as an invalid encoding argument), an error object will be passed into the callback. This type of error handling is quite common for asynchronous functions. Like we are used to, since writeFile is asynchronous, we need to utilize callback chaining if we want to guarantee that commands occur after the write is complete or fails.

module system
Node’s setTimeout
a browser’s setTimeout
global object
window object
fs module
create-a-nnew-file.js (github.com)
UTF-8
bgoonz’s gists · GitHub
bgoonz — Overview Web Developer, Electrical Engineer JavaScript | CSS | Bootstrap | Python | React | Node.js | Express | Sequelize…github.com