npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-require

v0.3.0

Published

Simplified require in electron applications

Downloads

55

Readme

electron-require

Simplified require in electron applications

electron-require is a super basic, no dependency convenience wrapper around require for your electron applications. You can use it to avoid using complex require paths in your application.

Installation

$ npm install --save electron-require

Then include it in your code:

const rq = require('electron-require');

Usage

rq()

rq('./module.js') imports module.js from the current process directory (it is actually an alias to require.main.require('./module.js')).

rq.electron()

rq.electron('module') is the same than require('electron')['module'], except that it resolves into require('electron').remote['module'] when module is not found, if used in the renderer process.

rq.remote()

rq.remote('module') is the same than require('electron').remote.require('module'), except that it resolves into rq.main('module') when used in the main process.

Aliases

You can add your own custom alias with rq.set(key, path).

Once rq.set('myAlias', 'my/path') is called, rq.myAlias('./module.js') will try to load my/path/module.js.

Example 1:

rq.set('local', 'local');

// Import [application root]/local/my-local-module.js into myLocalModule
const myLocalModule = rq.local('./my-local-module.js');

Example 2:

let userData = electron.app.getPath('userData');
rq.set('plugin', userData + '/plugins');

// Import [userdata]/plugins/my-plugin.js into myPlugin
const myPlugin = rq.plugin('/my-plugin.js');

Template strings

You can use template string in the path passed to .set():

  • %{app} resolves to app.getAppPath()
  • %{anyOtherName} resolves to app.getPath(anyOtherName)

So we can write example 2 in a simpler way:

rq.set('plugin', '%{userData}/plugins');

// Import [userdata]/plugins/my-plugin.js into myPlugin
const myPlugin = rq.plugin('/my-plugin.js');

Read more about this in the app module documentation

Multiple alias

rq.set can also be used with an object:

rq.set({
	'local': 'local',
    'plugin': '%{userData}/plugins'
});

Custom aliases defined in package.json

In most cases you will want to use the same custom aliases for the whole project. You can define custom aliases by adding an electron-require key to your app package.json file:

"electron-require": {
    "first": "path/to/first/alias",
	"second": "path/to/second/alias"
}

Default aliases

Default aliases are the following:

{
    "root": "",
    "renderer": "app/renderer",
    "main": "app/main",
    "browser": "app/main"
}

It actually assumes that your app is organized in the following way:

.
├── app
│   ├── main
│   │   └── [main process modules]
│   └── renderer
│       └── [renderer process modules]
└── package.json

But you can of course override theses default values by using rq.set() or by adding an electron-require entry in your package.json.

License

The MIT License (MIT)
Copyright (c) 2016 Thomas Brouard