How to specify browserify extensions in package.json?

4.3k views Asked by At

In package.json:

...
"browserify": {
  "transform": [
    "coffee-reactify"
  ],
  "extension": [ ".cjsx", ".coffee", ".js", ".json" ],
  "extensions": [ ".cjsx", ".coffee", ".js", ".json" ]
},
...

When using browserify transform option works as expected, however browserify is not seeing extension(s) options - it throws error and I have to pass extension options manually to browserify...


in gulpfile.coffee

b = browserify
  entries: './' # ./ = root = directory where package.json is
  debug: true
b.bundle()
.pipe(source('client.js'))
.pipe(buffer())
.pipe(gulp.dest(distDir))

in package.json

"browser": "src/client/client",
"browserify": {
  "transform": [
    "coffee-reactify"
  ],
  "extension": [
    "cjsx",
    "coffee",
    "js",
    "json"
  ]
},

src/client/client.cjsx

otherModule = require './other-module' # other-module.cjsx
  1. When I remove coffee-reactify from transforms in package.json then browserify throws error Parsing file .../src/client/client.cjsx: Unexpected token (2:16)

  2. When I put back coffee-reactify to transforms in package.json, then browserify successfully parses client.cjsx as long as I wont require any other .cjsx files from inside of client.cjsx. So for the example code of client.cjsx above browserify throws error: Cannot find module './other-module' from '/src/client - browserify still does not recognize extension...

So browserify reads package.json (recognizes package.browserify.transforms and package.browser fields but it does not recognize extensions)

2

There are 2 answers

4
marcel On

Try this:

"browserify": {
  "transform": [
    "coffee-reactify"
  ],
  "extension": [ 
    "cjsx", 
    "coffee", 
    "js", 
    "json" 
  ]
},

Remove the . dots. Take a look at this question.

0
Jim Hall On

We were running into the same problem. We were able to get it working by adding extensions to the browserify gulp function call.

browserify({
  entries: "src/index.coffee",
  extensions: [".cjsx", ".coffee", ".js", ".json" ]
})

We don't have it in the package.json at all, just in the gulp command.