# Brunch using javascript-obfuscator package

**URL:** <https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076>\
**Category:** Questions\
**Tags:** phoenix, brunch\
**Created:** [January 31, 2018, 2:16am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076 "2018-01-31T02:16:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 2:16am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/1 "2018-01-31T02:16:23Z")

</div>

I got this package [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator), installed it via npm install --save.

When I view source of the page it shows up as installed.

But when I try to use it as described in the documentation:

````var
var obfuscationResult = JavaScriptObfuscator.obfuscate(source_code, options);```

it throws error saying:
```Uncaught ReferenceError: __dirname is not defined
    at pjson.js:7```

And that line 7 is this: `var root = path.resolve(__dirname, "..", "..");`

I am so confused of this npm and brunch thing ... have opened so many pages and tried various things but not able to resolve this. 

Any help will be appreciated.
````

---

<div class="post-metadata">

**Author:** ![andre1sk](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andre1sk/32/1380_2.png) [@andre1sk](https://forum.elixirforum.com/u/andre1sk)\
**Post date:** [January 31, 2018, 3:06am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/2 "2018-01-31T03:06:18Z")

</div>

> [@potentElixir](#):
>
> When I view source of the page it shows up as installed

Are you trying to run it in browser or in Node?

---

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 3:45am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/3 "2018-01-31T03:45:45Z")

</div>

hey @andre1sk I am trying it in the browser

---

<div class="post-metadata">

**Author:** ![ChaseGilliam](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chasegilliam/32/2753_2.png) [@ChaseGilliam](https://forum.elixirforum.com/u/ChaseGilliam)\
**Post date:** [January 31, 2018, 4:04am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/4 "2018-01-31T04:04:01Z")

</div>

are you using `const path = require('path')`? You need to require path o make use of `__dirname`.

---

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 4:07am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/5 "2018-01-31T04:07:45Z")

</div>

Hey @ChaseGilliam thanks for responding. I put `const path = require('path')` above the line `var JavaScriptObfuscator = require('javascript-obfuscator'); ` and still the same error exists.

---

<div class="post-metadata">

**Author:** ![ChaseGilliam](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chasegilliam/32/2753_2.png) [@ChaseGilliam](https://forum.elixirforum.com/u/ChaseGilliam)\
**Post date:** [January 31, 2018, 4:27am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/6 "2018-01-31T04:27:26Z")

</div>

can you post your whole config?

---

<div class="post-metadata">

**Author:** ![andre1sk](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andre1sk/32/1380_2.png) [@andre1sk](https://forum.elixirforum.com/u/andre1sk)\
**Post date:** [January 31, 2018, 4:32am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/7 "2018-01-31T04:32:24Z")

</div>

> [@potentElixir](#):
>
> \_\_dirname

is only available in Node

---

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 4:33am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/8 "2018-01-31T04:33:23Z")

</div>

```elixir
exports.config = {
  files: {
    javascripts: {
      joinTo: {
        "js/app.js": /^(web\/static\/vendor)|(js)/
      }
    },
    stylesheets: {
      joinTo: "css/app.css",
      order: {
        after: ["web/static/css/app.css"] // concat app.css last
      }
    },
    templates: {
      joinTo: "js/app.js"
    }
  },

  conventions: {
    assets: /^(web\/static\/assets)/
  },

  // Phoenix paths configuration
  paths: {
    // Dependencies and current project directories to watch
    watched: [
      "web/static",
      "test/static"
    ],

    // Where to compile files to
    public: "priv/static"
  },

  // Configure your plugins
  plugins: {
    babel: {
      // Do not use ES6 compiler in vendor code
      ignore: [/web\/static\/vendor/]
    }
  },

  modules: {
    autoRequire: {
      "js/app.js": ["web/static/js/app"]
    }
  },
  
  npm: {
    enabled: true
  }
};

```

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [January 31, 2018, 4:41am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/9 "2018-01-31T04:41:38Z")

</div>

And what is the contents of your app.js file?

---

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 4:47am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/11 "2018-01-31T04:47:49Z")

</div>

app.js

```elixir
// Import dependencies
//
// If you no longer want to use a dependency, remember
// to also remove its path from "config.paths.watched".
import "phoenix_html"

```

---

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 4:51am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/12 "2018-01-31T04:51:19Z")

</div>

Hey @andre1sk pardon me for my ignorance, when you meant to ask that am I trying to run it in the browser or in the node …

To be clear, I am placing the code:  
`var JavaScriptObfuscator = require('javascript-obfuscator'); var obfuscationResult = JavaScriptObfuscator.obfuscate(source_code, options);`  
in a posts.js file in the server side under /static/js folder.

---

<div class="post-metadata">

**Author:** ![andre1sk](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andre1sk/32/1380_2.png) [@andre1sk](https://forum.elixirforum.com/u/andre1sk)\
**Post date:** [January 31, 2018, 4:56am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/13 "2018-01-31T04:56:08Z")

</div>

It’s meant to be running in Node you are including it in the file that is getting sent to the browser.

---

<div class="post-metadata">

**Author:** ![potentElixir](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@potentElixir](https://forum.elixirforum.com/u/potentElixir)\
**Post date:** [January 31, 2018, 5:14am UTC](https://forum.elixirforum.com/t/brunch-using-javascript-obfuscator-package/12076/14 "2018-01-31T05:14:24Z")

</div>

Ah yes thats what I am doing.
