v1.1.0
StoneJS Framework - NPM Modules Demo

📦 Using NPM Modules in StoneJS Pages

You can use ANY npm module in your StoneJS pages!

Simply use require() in your EJS code blocks to load built-in Node.js modules or installed npm packages.

1. Built-in Crypto Module - Hash Generation

Generate Hash for Your Text

Input: Hello, StoneJS Framework!
MD5: d80a5eb810ba2db75d0d73b513df08bb
SHA-256: a04d81a08896642a356c559f24eaeb65848b86e2a38caba697d51a57048737a4

Code Used:

<% const crypto = require('crypto'); const generateMD5 = (text) => { return crypto.createHash('md5').update(text).digest('hex'); }; const hash = generateMD5('Your text here'); %>

Example Hashes

Text Description MD5 Hash SHA-256 Hash
Hello World Simple greeting b10a8db164e0754105b7a99be72e3fe5 a591a6d40bf420404a011733cfb7b190...
StoneJS Framework Framework name 1914a33660442e92859b894ed76fa1a1 59b297787f231a737a29ccf34ced0ec4...
password123 Example password 482c811da5d5b4bc6d497ffa98491e38 ef92b778bafe771e89245b89ecbc08a4...
/demo/npm-modules.html Current request path bd382a2c78d9b718b0b52b8ce341401f f8fe5cbd90b181a521fcc283a227c195...
2025-12-26T10:50:58.221Z Current timestamp 0439218d477a14bcc01ac4db4764c089 cf0dd8d48c9a5b030c520a0d25fc91e9...

2. Built-in OS Module - System Information

Your Server Information

Property Value
platform linux
arch x64
cpus 4
totalMemory 3.82 GB
freeMemory 2.81 GB
uptime 406033 minutes

Code Used:

<% const os = require('os'); const systemInfo = { platform: os.platform(), arch: os.arch(), cpus: os.cpus().length, totalMemory: (os.totalmem() / 1024 / 1024 / 1024).toFixed(2) + ' GB' }; %>

3. Built-in Path Module - File Path Operations

Path Utilities

Operation Result
current /data/embedded-apps/pages/demo/npm-modules.html
dirname /data/embedded-apps/pages/demo
basename npm-modules.html
extname .html
joined /pages/components/user-card.html

Code Used:

<% const path = require('path'); const joined = path.join('/pages', 'components', 'user-card.html'); const basename = path.basename('/path/to/file.html'); // 'file.html' const extname = path.extname('/path/to/file.html'); // '.html' %>

4. Using External NPM Packages

How to Use External Packages:

Step 1: Install the package

npm install package-name

Step 2: Require it in your page

<% const packageName = require('package-name'); const result = packageName.doSomething(); %>

Popular Packages to Try:

  • moment - Date/time manipulation
  • lodash - Utility functions
  • axios - HTTP requests
  • uuid - Generate UUIDs
  • validator - String validation
  • cheerio - HTML parsing (server-side jQuery)
  • marked - Markdown to HTML
  • sharp - Image processing

5. Practical Use Cases

Use Case Examples:

1. Generate File Checksums

<% const crypto = require('crypto'); const fs = require('fs'); // Read file and generate checksum const fileBuffer = await fs.promises.readFile('/path/to/file.pdf'); const checksum = crypto.createHash('md5').update(fileBuffer).digest('hex'); %>

2. Format Dates

<% // Using built-in Date const now = new Date(); const formatted = now.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); %> Today is: Friday, December 26, 2025

3. Process JSON Data

<% const data = { name: 'John', age: 30 }; const json = JSON.stringify(data, null, 2); const parsed = JSON.parse(json); %>

4. URL Parsing

<% const url = require('url'); const parsed = url.parse('http://example.com/path?foo=bar'); %>

📚 Best Practices

Tips for Using NPM Modules:

  • Cache module requires: Put require() statements at the top of your page for better performance
  • Use built-in modules first: Node.js has many useful built-in modules (crypto, fs, path, os, url, etc.)
  • Install before deploying: Make sure all packages are in package.json
  • Handle errors: Wrap module operations in try-catch blocks
  • Check compatibility: Ensure packages work in a server environment (some are browser-only)
  • ⚠️ Avoid heavy operations: Don't block the request with long-running operations
  • ⚠️ Security: Validate user input before passing to modules

Common Built-in Node.js Modules:

Module Purpose
cryptoEncryption, hashing, random bytes
fsFile system operations
pathFile path utilities
osOperating system info
urlURL parsing and formatting
utilUtility functions
querystringQuery string parsing
http/httpsHTTP client/server