Defining global variable for Browserify

Writing Spine = require(‘spine’) in each file is the right way to do. Yet, there are several possibilities by using the global or window object (browserify sets the global object to window, which is the global namespace): in spine.js: global.Spine = module.exports in any other .js file bundled by browserify: global.Spine = require(‘spine’) in a … Read more

How do I use Browserify with external dependencies?

You can achieve that by using browserify-shim. Assuming that you’ve got a module named mymodule.js that depends on jQuery in the global scope with the following contents: var $ = require(‘jQuery’); console.log(typeof $); Install browserify-shim: npm install browserify-shim –save-dev In package.json file, tell browserify to use browserify-shim as a transform: { “browserify”: { “transform”: [ … Read more

How to import part of object in ES6 modules

Unfortunately import statements does not work like object destructuring. Curly braces here mean that you want to import token with this name but not property of default export. Look at this pairs of import/export: //module.js export default ‘A’; export var B = ‘B’; //script.js import A from ‘./a.js’; //import value on default export import {B} … Read more

require is not defined error with browserify

The “require” function is only available in the “https://stackoverflow.com/questions/28696511/bundle.js” script context. Browserify will take all the script files necessary and put them into the “https://stackoverflow.com/questions/28696511/bundle.js” file, so you should only have to include “https://stackoverflow.com/questions/28696511/bundle.js” in the HTML file, not the “script.js” file.

browserify error /usr/bin/env: node: No such file or directory

Some linux distributions install nodejs not as “node” executable but as “nodejs”. In this case you have to manually link to “node” as many packages are programmed after the “node” binary. Something similar also occurs with “python2” not linked to “python”. In this case you can do an easy symlink. For linux distributions which install … Read more

Browserify – How to call function bundled in a file generated through browserify in browser

The key part of bundling standalone modules with Browserify is the –s option. It exposes whatever you export from your module using node’s module.exports as a global variable. The file can then be included in a <script> tag. You only need to do this if for some reason you need that global variable to be … Read more

Is there a way to render multiple React components in the React.render() function?

Since the React v16.0 release you can render an array of components without wrapping items in an extra element when you are inside a component: render() { return [ <li key=”one”>First item</li>, <li key=”two”>Second item</li>, <li key=”three”>Third item</li>, <li key=”four”>Fourth item</li>, ]; } Remember only to set the keys. UPDATE Now from the 16.2 version … Read more

Browserify, Babel 6, Gulp – Unexpected token on spread operator

That syntax is an experimental proposed syntax for the future, it is not part of es2015 or react so you’ll need to enable it. npm install –save-dev babel-plugin-transform-object-rest-spread and add “plugins”: [“transform-object-rest-spread”] into .babelrc alongside your existing presets. Alternatively: npm install –save-dev babel-preset-stage-3 and use stage-3 in your presets to enable all stage-3 experimental functionality.

How to import jquery using ES6 syntax?

index.js import {$,jQuery} from ‘jquery’; // export for others scripts to use window.$ = $; window.jQuery = jQuery; First, as @nem suggested in comment, the import should be done from node_modules/: Well, importing from dist/ doesn’t make sense since that is your distribution folder with production ready app. Building your app should take what’s inside … Read more

tech