meta pixel

Trying to do without require in Node.js

I will share some experiments with duplicating require functionality in Node.js. The practice is rather vicious, but sheds some light on the problems of this popular platform. The initial motivation for such research arose from the following observations.

    Posted byEvgeniy Arshanskiy|on Apr 29th 2025|1 min read

    Technology Expert

There is some conceptual difference in how external libraries and project files are included. And the standard mechanism for this is the same. The second point that confuses me is the somewhat confused logic of the require itself.

As a result, I wanted to at least connect my files in a self-made, but more transparent way. In parallel, adding a few additional features. This is how the functions described below appeared.

get_modules =( string, modules ={} )-> groups =string .split ' ' for group in groups items =group .split ':' id =items[ 0 ] named =items .length > 1 name =if named then items[ 1 ] else id .replace( /\-/g, '_' ) obj =require id obj =obj[ name ] if named and obj[ name ] modules[ name ] =obj modules Auxiliary function for connecting standard modules. Works as follows. After, for example, modules =get_modules 'fs path coffeescript vm' in the modules object, we will get the standard modules fs, path and others, which can be accessed accordingly modules .[module name] .[function name].

So read =( file )-> path =modules .path .join __dirname, file modules .fs .readFileSync path, 'utf8' - returns the contents of the source file. compile =( coffee, file )-> options = inlineMap: false, sourceMap: true, bare: true, filename: file res =modules .coffeescript .compile coffee, options res .js - Translates CoffeeScript to JavaScript. ``` evaluate =( js, filename ='', lineOffset =0, columnOffset =0 )-> modules .vm .runInThisContext js, { filename, lineOffset, columnOffset }

- executes JavaScript code. And all together, in case we want to wrap our code in some kind of wrapper function: get_wrapper =( id, args )-> file =id + '.coffee' #путь к файлу str =@read( file ) .replace( /\n/g, "\n\t" ) #добавляем отступы coffee ="( #{ args } )->\n\t#{ str }" #собственно "оборачиваем" в функцию js =@compile coffee, file @evaluate js, file#, 1, 0 `` whereargs` is a string with comma-separated variable names that will become available as "global" in this module.

source code

About the Author

Evgeniy Arshanskiy
Evgeniy Arshanskiy

Experienced full-stack developer from Saint Petersburg, now in Georgia, with a bachelor's in technical physics and a master's in mathematical and IT. Proficient in front-end (HTML, CSS, JavaScript, React) and back-end (Ruby, Erlang/OTP, Node.js) development. Skilled in responsive design, user interactions, and appealing UI. Strong in databases, server management, APIs, and system integration. Knowledgeable in tools like Git, npm, and task runners, plus RESTful APIs, security, and performance. Adept at debugging, troubleshooting, and teamwork. Delivers end-to-end solutions, excelling in UI/UX and server-side functionality.

Expertise

React 3+ yrs
TypeScript 3+ yrs
Node.js 6+ yrs
Ruby on Rails 6+ yrs
Erlang 2+ yrs
Evgeniy Arshanskiy

Evgeniy Arshanskiy

Technology Expert


Trending Posts

Global HR>Global Employment

Payroll Compliance Checklist and Tax Law Guide for 2025

Highlight the critical nature of staying informed on payroll compliance and tax laws for tech companies with a global footprint in 2025.

Brian Mc Sweeney
Brian Mc SweeneyTechnology • 11+ years
Project & Product>Methodology

Agile Project Management: Key Benefits for Modern Product Teams

In the ever-evolving landscape of product development, staying ahead of the curve is paramount.

Yana Bell
Yana BellHuman Resources • 10+ years
Future of work

How Much Software Engineers Make in 2025?

We will delve into the various factors that influence software engineer salaries in 2025, provide detailed salary breakdowns by region, and explore how remote work is shaping compensation trends.

Yana Bell
Yana BellHuman Resources • 10+ years
Evgeniy Arshanskiy

Evgeniy Arshanskiy

Technology Expert


Trending Posts

Global HR>Global Employment

Payroll Compliance Checklist and Tax Law Guide for 2025

Highlight the critical nature of staying informed on payroll compliance and tax laws for tech companies with a global footprint in 2025.

Brian Mc Sweeney
Brian Mc SweeneyTechnology • 11+ years
Project & Product>Methodology

Agile Project Management: Key Benefits for Modern Product Teams

In the ever-evolving landscape of product development, staying ahead of the curve is paramount.

Yana Bell
Yana BellHuman Resources • 10+ years
Future of work

How Much Software Engineers Make in 2025?

We will delve into the various factors that influence software engineer salaries in 2025, provide detailed salary breakdowns by region, and explore how remote work is shaping compensation trends.

Yana Bell
Yana BellHuman Resources • 10+ years