Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

smart-require

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smart-require

A better require() for large projects

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

smart-require

Avoid ugly require('../../../some-file') using absolute paths, from the root of your project.

This package follows the well-known UNIX convention where / is the root.

For example, in this source tree:

.
├── index.js
├── image
│   ├── gif.js
│   ├── jpg.js
│   └── png.js
└── video
    └── mp4.js

…if you’re in image/gif.js, this package allows you to require video/mp4.js like this:

const video = require('/video/mp4.js')

…instead of:

const video = require('../video/mp4.js')

Of course, it prohibits you to require anything from the root of your system (but why doing so?). Since the behavior of require() changes, this is not really safe, even if I never had any problem with it. Use at your own risk.

Installation

yarn add smart-require

…or:

npm install --save smart-require

Then, you just have to indicate your project root before any call to the customized require, usually at the beginning of your entrypoint. If you are in index.js in the example above, you can write:

require('smart-require')(__dirname)

…or if your entrypoint is in image/jpeg.js and you want the same behavior:

const root = require('path').join(__dirname, '..')
require('smart-require')(root)

See also

require-root, nice but different.

FAQs

Package last updated on 10 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc