browserify / browserify/browserify

How do I use expressions in require()

Open
#1,702 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.7k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I understand that browserify doesn't like expressions as the argument for require because it wouldn't be able to figure out what file to include in the bundle without executing the bundle's code.

However, are there any solutions for this if someone wanted to use expressions in require; or what are the alternatives?

The reason why I want to use expressions is for templates. Right now I'm updating an old application in increments. I'm using [jadeify](https://github.com/domenic/jadeify) to require my templates in on the client side (previously was using browserijade). In my JS bundle, I have to write relative paths to my jade files:

```
var template = require('../../views/my-template.jade');
```

It would be more convenient to encapsulate this into a module:

```
module.exports = function(filename){
return require(__viewsdir + filename);
}
```

This way I can write:

```
var templates = require('templates');

var template = templates('my-template.jade');
```

How can I get this to work? Is it possible? If it's not, please explain to me why it wouldn't be possible. Please give me an idea on this issue and what discussions and conclusions have been made on this topic (if any).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.