Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build package for both node and browser environment

I have developed a node NPM package which is mainly a wrapper (using node's http, https and querystring modules) for a specific JSON API. It is build in Coffeescript and enables a Node.js server to communicate with this API. The Api is mainly REST.

Now I want this library to be also available for the browsers. This means the calls to the http modules needs to be replaced with XMLHttpRequest (asynchronous). It seems to me I would make a wrapper for the adapter. For the Node environment, this adapter would pass all the calls to the http module, and for the browser environment to the XMLHttpRequest object.

Is there a nice way to make a build system so that the npm package contains both version, and I can publish the plain "browser-version" also on Github? The node package then is available via require('package-name') and should place a JS file (for the browser) in a directory.

I have looked in Component, which is nice for client-side package managing, but the problem remains how to create different build environments.

like image 648
Roemer Avatar asked May 06 '13 15:05

Roemer


People also ask

Can you use node packages in the browser?

Users can use already existing modules on the client side JavaScript application without having to use a server. But how can this be done? Well, here comes a tool called Browserify. Browserify is a command line NodeJS module that allows users to write and use already existing NodeJS modules that run on the browser.

Is Webpack used with NodeJS?

Webpack provides a Node. js API which can be used directly in Node. js runtime.

Do I need to install node modules for every project?

We do not need to install a module every time when installed globally. It takes less memory as only one copy is installed. We can make . js scripts and run them anywhere without having a node_modules folder in the same directory when packages are installed globally.

What is browser in package JSON?

browser field spec for package.json. browser.md. The browser field is provided by a module author as a hint to javascript bundlers or component tools when preparing modules for client side use.


2 Answers

You could use node-browser-resolve which adds a browser section to package.json:

{
  "browser": {
    "./index.js": "./browser.js"
  }
}

See https://nolanlawson.com/2017/01/09/how-to-write-a-javascript-package-for-both-node-and-the-browser/

like image 137
Tom Söderlund Avatar answered Oct 14 '22 21:10

Tom Söderlund


An example solution for cross-developing for node.js and browsers using browserify: https://github.com/amitayd/grunt-browserify-jasmine-node-example (and discussion at my blog post) .

Specifically for having different implementations for Browser/Node.js check PersistentReaderWriter.js.

Once you have some template to start working with browserify, and you're aware of some pitfalls, you might find you'd like to use it for small libraries as well.

Edit: Note that if you browserify the module, the isBrowser() check shouldn't be by checking if module and module.exports are defined, since Browserify's wrapper will define them in the module context. Instead, in my example, I check for window to be defined.

like image 5
Amitay Dobo Avatar answered Oct 14 '22 21:10

Amitay Dobo