• Stars
    star
    427
  • Rank 97,887 (Top 2 %)
  • Language
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Spec document for the 'browser' field in package.json

The browser field is provided by a module author as a hint to javascript bundlers or component tools when packaging modules for client side use. The field is found in a package.json file (described here) usually located at the root of a project source tree.

terms

Below are common terms used in the rest of the document.

server

This is a non-dom based javascript execution environment. It usually only contains the base javascript language spec libraries and objects along with modules to communicate with OS features (available through commonjs require).

client

This is a browser execution environment. It may provide additional built in objects exposed in the global namespace. It is a specialized execution environment which provides builtin capabilities beyond the base javascript language spec.

commonjs

A require system for specifying which modules or files a particular file uses.

bundler

A tool which takes a plain javascript package and creates client usable files. It may include, but is not limited to: replacing modules or files with client versions (since the client may already provide the functionality), merging all the dependencies into a single file, etc.

package.json

Metadata information about a module. Usually found at the root of the project source tree.

packaging

The use of a bundler to create a file(s) suitable for running on a client.

Overview

When a javascript module is prepared for use on a client there are two major concerns: certain features are already provided by the client, and certain features are not available. Features provided by a client can include http requests, websockets, dom manipulation. Features not available would include tcp sockets, system disk IO.

The browser field is where the module author can hint to the bundler which elements (other modules or source files) need to be replaced when packaging.

Spec

alternate main - basic

When you specify a single string for the browser field, it will replace main and be the module entry point. The main field specifies the entry point to the module so by replacing it, you replace the entry point when the module is packaged by a bundler for browser use.

"browser": "./browser/specific/main.js"

Whenever another module requires your module by name, the bundler will load javascript from ./browser/specific/main.js instead of the typical main field entry point (or index.js by default).

All paths for browser fields are relative to the package.json file location (and usually project root as a result).

replace specific files - advanced

In many cases, there is a large amount of code which is applicable to both client and server. If is easier to just replace some files instead of creating a completely new entry point. To do this, just specify an object versus a single string.

When using an object. The left hand side (key) is the name of a module or file you wish to replace and the right side is the replacement.

"browser": {
    "module-a": "./shims/module-a.js",
    "./server/only.js": "./shims/client-only.js"
}

Now when you package your code, uses of module-a will be replaced with code from ./shims/module-a.js versus the module code itself and anytime ./server/only.js is used, it will be replaced with ./shims/client-only.js

If a module you depend on already includes a browser field, then you don't have to do anything special. Whenever you require that module, the bundler SHOULD use the hint provided by the module.

ignore a module

You can simply prevent a module or file from being loaded into a bundle by specifying a value of false for any of the keys. This is useful if you know certain codepaths will not be executed client side but find it awkward to split up or change the code structure.

"browser": {
    "module-a": false,
    "./server/only.js": "./shims/server-only.js"
}

The above will cause the following to return an empty object into a.

var a = require('module-a');

Note: The use of false should be restricted to only the most essential places in your app code where other browser field approaches do not work. It is discouraged but sometimes a necessary approach to blacklist a module from client packaging.

Advantages

Using the browser field in package.json allows a module author to clearly articulate which files are inappropriate for client use and provide alternatives. It allows the module code (and subsequently dependants on the module) to not use preprocessor hacks, source code changes, or runtime detection hacks to identify which code is appropriate when creating a client bound package.

Notes

  • If your module is pure javascript and can run in both client and server environments, then you do not need a browser field.
  • The browser field is located in the package.json file as it provides metadata in the form of a hint to bundlers about what files you have indicated are targeted for the client. It allows your source code to remain clean and free of hacks.
  • Consider that the client environment as the special case as it exposes objects into the global space to provide certain features and limits others.

More Repositories

1

zuul

[UNMAINTAINED] multi-framework javascript browser testing
JavaScript
961
star
2

form-serialize

serialize html forms
JavaScript
399
star
3

node-url

node.js core url module as a module
JavaScript
374
star
4

ansible-coreos-bootstrap

[NOT MAINTAINED] bootstrap a coreos machine for control via ansible
Python
227
star
5

npm-css

Require css from npm
JavaScript
128
star
6

badginator

badges for your badges
JavaScript
117
star
7

libuv.js

libuv javascript bindings
JavaScript
114
star
8

node-process

process information for node.js and browsers
JavaScript
111
star
9

qr.js

javascript qrcode library
JavaScript
92
star
10

node-enchilada

middleware for on-demand javascript bundling
JavaScript
74
star
11

node-required

identifies which modules your script is using
JavaScript
66
star
12

virtualbox-pxe-boot

Bootstrap for testing PXE boot configs in VirtualBox
66
star
13

bitcoin-address

bitcoin address verification and other tools
JavaScript
62
star
14

sequelize-encrypted

Encrypted fields for Sequelize ORM
JavaScript
60
star
15

fixjs

Financial Information Exchange protocol in javascript
JavaScript
55
star
16

npm-github

npm registry proxy to github repositories
JavaScript
54
star
17

changelog

changelog release management tool
JavaScript
51
star
18

docket

build docker images with secrets
Python
48
star
19

localenv

load environment variables from .env files
JavaScript
35
star
20

dom-events

DOM event binding and emitting
JavaScript
35
star
21

away

monitor when a user is inactive on a page
JavaScript
35
star
22

coreos-ansible-example

examples for using Ansible with CoreOS
Ruby
34
star
23

synthetic-dom-events

create DOM events for builtin event types
JavaScript
33
star
24

num

arbitrary size and precision number library in pure javascript
JavaScript
33
star
25

jetson-nano-image-maker

Create sd-card ready jetson nano images using Docker and Github Actions
Shell
29
star
26

node-book

fast and flexible logging for node.js
JavaScript
27
star
27

bamboo

javascript model library for basic CRUD
JavaScript
23
star
28

node-superstack

long stack traces for node.js
JavaScript
22
star
29

typeahead

bootstrap compatible typeahead web component
JavaScript
21
star
30

node-spaceport

decentralized service registry for nodejs
JavaScript
21
star
31

handlebars-extend-block

create extend blocks for handlebars
JavaScript
21
star
32

node-script

[DEPRECATED] see readme
JavaScript
19
star
33

node-presentation

c++ modules with node.js presentation and support material
C++
18
star
34

tryme

interactive code demo, documentation, and editor for github repositories
JavaScript
15
star
35

node-stackback

return array of v8 CallSite objects for a stacktrace
JavaScript
14
star
36

node-influx-collector

influxdb stats collector
JavaScript
12
star
37

qr-element

qrcode html element
JavaScript
12
star
38

node-htmltree

simple dom for xml/html
JavaScript
12
star
39

node-yummy

cookie session middleware for connect
JavaScript
12
star
40

int

arbitrary size integer library in javascript
JavaScript
12
star
41

udp-portal

tunnel UDP traffic over TCP
JavaScript
10
star
42

expressjs-tips-tricks

JavaScript
10
star
43

expando

expand/collapse html elements with variable height
JavaScript
10
star
44

docserv

documentation browser for node projects
JavaScript
9
star
45

chain-js

callback chaining library for javascript
JavaScript
8
star
46

ratelimit-middleware

ratelimit middleware for express
JavaScript
8
star
47

browser-stacks

stackframes from various browsers
JavaScript
7
star
48

intro-node-js

quick and dirty presentation of some node.js basics
JavaScript
7
star
49

reunion

JavaScript
6
star
50

socket.io-cloud

hosted socket.io (work in progress)
CSS
6
star
51

clock-input

clock-like time input control
JavaScript
6
star
52

node-jsrender

[NOT MAINTAINED] Node.js port of jsrender
JavaScript
6
star
53

react-webpack-boilerplate

Boilerplate example with react (with hot reloading), webpack, eslint, es6 (babel), and basic dockerfile.
JavaScript
6
star
54

influxdb-docker-stats

docker stats collection into influxdb
JavaScript
5
star
55

balabolka

dead simple hosted chat for your website
JavaScript
5
star
56

chrome-socket

streaming socket interface for chrome tcp
JavaScript
5
star
57

polyfill-middleware

selectively serve javascript polyfills based on user-agent
JavaScript
5
star
58

node-book-git

git middleware for book logging framework
JavaScript
4
star
59

etcd-spaceport

service registry leveraging etcd as a backend
JavaScript
4
star
60

node-taters

super hash powers for express view rendering
JavaScript
4
star
61

etcdjs-watch

watch etcd keys for changes
JavaScript
4
star
62

hubitat-somfy-mylink

Somfy MyLink Integration for Hubitat
Groovy
3
star
63

node-book-file

file transport for book logging framework
JavaScript
3
star
64

node-partial-compare

deep partial comparison of objects
JavaScript
3
star
65

node-lsmod

get a list of modules and versions for the main prog
JavaScript
3
star
66

node-bookrc

automatic config loading for the book logging framework
JavaScript
3
star
67

logspout-firehose

Logspout adapter for writing Docker container logs to AWS Kinesis Firehose
Go
3
star
68

node-connect-raven

connect error handling middleware to log via raven
JavaScript
3
star
69

events-browserify

DEPRECATED - use https://github.com/Gozala/events
JavaScript
3
star
70

dom.position

dom position functions
JavaScript
3
star
71

node-filepile

file backed worker queue
JavaScript
3
star
72

node-veto

middleware to error check parameters
JavaScript
3
star
73

flip-counter

apple style flip counter
JavaScript
3
star
74

node-weaklink

hunt down and eradicate floating dependencies
JavaScript
2
star
75

node-mandible

html and txt email templates with handlebars
JavaScript
2
star
76

node-book-pagerduty

pagertudy notifier for panic logs
JavaScript
2
star
77

qr

stdout into qr code
JavaScript
2
star
78

takeoff

cross platform onscreen takeoff estimating application
C++
2
star
79

fx.js

basic dom transitions
JavaScript
2
star
80

bootstrap-confirm

confirmation dialog with bootstrap ui classes
JavaScript
2
star
81

node-sauron

nodejs process manager and launcher
JavaScript
2
star
82

dotfiles

home directory config files
Shell
2
star
83

studio-extension-ogv-example

TypeScript
2
star
84

node-jsbundler

RENAMED
2
star
85

microtime2iso

convert decimal time values to iso8601
JavaScript
2
star
86

node-asset-manager

DEPRECATED!! don't use this shit! see readme
JavaScript
2
star
87

node-book-email

email transport for the book logging framework
JavaScript
2
star
88

node-abe

Expose ArrayBuffers as read/write text streams
JavaScript
2
star
89

braces

ctemplate and mustache inspired templates
JavaScript
2
star
90

xrequest

cross browser ajax request
JavaScript
2
star
91

npm-install

recursive installation of npm packages
JavaScript
2
star
92

fpddir

Federal Reserve E-Payments Routing Directory
JavaScript
2
star
93

d3-examples

collection of d3 examples for use with tryme
JavaScript
2
star
94

node-crumbs

[deprecated] check out my node-cookie repo for basic cookie (de)serialization instead
JavaScript
2
star
95

mailview

preview html and txt emails
JavaScript
1
star
96

stringencoding

Encode to/from Typed Array buffers
JavaScript
1
star
97

eyersee

JavaScript
1
star
98

book-bugsnag

bugsnag logger for book
JavaScript
1
star
99

node-v8-bypass

Bypass the v8 heap limit by storing cache objects in node process memory
C++
1
star
100

combobox

Lightweight select box replacement
JavaScript
1
star