Giter VIP home page Giter VIP logo

strict-import's Introduction

strict-import

Prevent require from searching upwards for required modules

Background

The require() algorithm works by searching for a node_modules directory with your required module from the current directory and upwards until it reaches the system root directory.

This means that if you have nested projects, and have a module called foo installed at the top-level, the sub-projects can also import foo without installing it. While useful in some cases, it can also cause problems.

I made this module because I'm working on an Electron app, where we use electron-builder with a two package structure. We depended on module foo in the renderer, which was defined top-level, since we use Webpack for bundling. We later started using foo in the main process code too, which is placed in an app subdirectory. The problem is that we forgot to add foo as a dependency in the app directory, but it worked fine in development as require just found it at the top-level. In production, however, it crashed, since we no longer had the top-level dependency, as only the app directory is included in the built app. With this module, we can ensure that doesn't happen again.

Install

$ npm install strict-import

Usage

At the top of your index.js file.

require('strict-import')(module);

// This now only works if `foo` is in `./node_modules`,
// but not if it's in `../node_modules`
const foo = require('foo');

License

MIT © Sindre Sorhus

strict-import's People

Contributors

itaisteinherz avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

richienb

strict-import's Issues

Add an option to allow certain modules to search upwards

Issuehunt badges

I had the need to allow a specific module to require upwards, so I added a private option _allowedMethods in 64ac0d0. I made it private because I'm not totally happy with it. It only works for top-level imports. So you can allow require('a') to be searched upwards by including a in the option, but if a imports a module name b, there's no way to allow b specifically, other than allowing any module from a.

Context: atomiclabs/hyperdex@d54a47c

There is a $40.00 open bounty on this issue. Add more on Issuehunt.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.