2

We are trying to Browserify our node app

Sample File (index.js)

module.exports = {
  index: () => 'test',
};

Browserify command

browserify src/index.js > dist/bundle.js --node

If we use a file to require and console

console.log(require('src/index'));   // { index: [Function: index] }
console.log(require('dist/bundle')); // { } 

Our expectation is that bundle.js would export the same as index.js.

Can anyone point us at what we are doing wrong or missing?


Additional Info

~This is not our app, this is a sample to demonstrate the issue

We are currently sending our entire app zipped to AWS Lambda with the entry point src/index.index and the objective is to just send the bundle.js file and be able to to have the entry point bundle.index

bundle.js

(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
module.exports = {
    index: () => 'test',
};

},{}]},{},[1]);
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • 1
    I do not have experience with AWS Lambda, but seems to me you are using the wrong tool for the job. Browserify is intended to bundle and provide some shims to replicate things like require, fs, etc. in the browser. –  Aug 21 '18 at 19:18
  • Here is a link describing how to create a Node.js app bundle for AWS Lambda https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html –  Aug 21 '18 at 19:24
  • @ErickRuizdeChavez These docs tell you to zip an entire project and load it manually. I'm looking to only bundle files that I need and send it to lambda through aws-cli. We have extra node modules, and testing files that don't need to be sent to to lambda – Seth McClaine Aug 21 '18 at 19:38
  • I can think in 2 different options, a) use a tool like grunt/gulp to bundle what you need (not my favorite as your dependencies might have their own dependencies); or b) create a new project with its own dependencies (so you can zip it) and if that code is also required outside of AWS you should be able to use it as a regular Node module. –  Aug 21 '18 at 20:07
  • `Grunt` and `Gulp` are task runners, can you elaborate on why you recommend using them? The only way I could think to include them in this process is to have them run a bundler, such as `browserify`, which I am currently doing with npm commands. – Seth McClaine Aug 21 '18 at 20:32
  • I've been doing a little bit of work on Webpack recently and seems a good fit for this task. Have you tried it? –  Aug 26 '18 at 23:27

2 Answers2

5

You need to use the --standalone flag. If I reproduce the setup you describe in your question and execute:

$ browserify src/index.js --standalone mylib > dist/bundle.js

Then I can run an interactive Node session on it and use the library in the way you expect it to be used:

$ node
> require("./dist/bundle").index()
'test'

The --standalone flag tells Browserify to wrap your code in a UMD stub which allows loading the bundle as CommonJS module, an AMD module, or as a plain script (i.e. does not use a module system). The argument you pass with --standalone indicates what name your library will take in the "plain script" case. So in the example above, if you were to load the library in a browser without any module system, you'd be able to run index as mylib.index().

Louis
  • 146,715
  • 28
  • 274
  • 320
  • 1
    Still needed to add the `--node` flag but that was what I needed! `browserify src/index.js --standalone mylib > dist/bundle.js` – Seth McClaine Aug 28 '18 at 20:25
0

You can use serverless for this, pretty easy to configure. No need to use browserify cli for this.

Keep following following official documentations to setup serverless cli.

  1. Installation
  2. AWS - Credentials
  3. Quick Start

Once everything is setup and you are able to deploy your lambda functions to AWS using serverless cli. Follow following steps to setup browserify.

  1. Install browserify as a dev dependecy.
  2. Install serverless-plugin-browserifier as a dev dependency.
  3. Add the plugin to your serverless.yml file and set package.individually to true. (Ref)

    plugins:
      - serverless-plugin-browserifier
    
    package:
      individually: true
    

Note: Personally tried this and is working.

Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25