Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: window is not defined #3

Open
MaartenFaddegon opened this issue Jul 30, 2017 · 3 comments
Open

ReferenceError: window is not defined #3

MaartenFaddegon opened this issue Jul 30, 2017 · 3 comments

Comments

@MaartenFaddegon
Copy link

I cannot run the example code. Am I missing something or is there an incompatibility with Purescript version 0.11.6?

To reproduce:

Create a new project with pulp init && bower install purescript-phoenix

Copy the example code into src/Main.purs.

When trying to run the example with pulp run I get the unexpected output

...
Compiling Phoenix
Compiling Main
Compiling Data.Map.Gen
* Build successful.
1. Creating
/home/maarten/Purescript/hello/output/Phoenix/foreign.js:5
      return new window.Phoenix.Socket(endpoint, options);
                 ^

ReferenceError: window is not defined
    at /home/maarten/Purescript/hello/output/Phoenix/foreign.js:5:18
    at Object.__do [as main] (/home/maarten/Purescript/hello/output/Main/index.js:64:212)
    at Object.<anonymous> (/tmp/pulp-run117630-6785-zn62c0.vc4s.js:1:79)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
* ERROR: Subcommand terminated with exit code 1
@brandonhamilton
Copy link
Owner

purescript-phoenix is meant to run in a client-side browser environment alongside the phoenix.js client library.

However, the pulp run command will build and then run your code using node, which does not define window.

To get started with purescript-phoenix, the purescript code can be compiled into a single javascript file with the pulp build --to option, and can then be included and served from a phoenix project as a static asset.

@MaartenFaddegon
Copy link
Author

Thanks for following up.

I copied main.js to assets/static/js/ in my Phoenix deployment, and in app.html.eex I added the following line

<script src="<%= static_path(@conn, "/js/main.js") %>"></script>

Then, in the javascript console I get the error:

TypeError: window.Phoenix is undefined[Learn More]  main.js:717:9
    exports.newSocketImpl/</< http://localhost:4000/js/main.js:717:9
       __do http://localhost:4000/js/main.js:915:15
      <anonymous> http://localhost:4000/js/main.js:941:1

So am I still not running alongside the phoenix.js client library? From the guide you linked to I thought to understand that Phoenix always serves this alongside any asset?

@brandonhamilton
Copy link
Owner

If you are not using brunch (i.e. you created your phoenix project with --no-brunch option), you would need to manually include phoenix.js as well as your main.js (that you have put into priv/static/js/) in the template, e.g.

<script src="<%= static_path(@conn, "/js/phoenix.js") %>"></script>
<script src="<%= static_path(@conn, "/js/main.js") %>"></script>

If you are using brunch, you would need to incorporate your purescript code into the brunch build instead of directly including the main.js file in your html template. I suggest you look into the pulp browserify options.
If you want a quick and dirty integration, just put your main.js from above into the assets/js directory, and include the following lines in assets/js/app.js to pull the purescript code into the brunch compiled script:

global.Phoenix = require("phoenix");
require("./main");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants