grantwest

grantwest

I am really struggling with getting D3.js working in my Phoenix app. I have a default Phoenix umbrella app with webpack.

Steps that I have done so far:

  1. Added "d3": ">=5.9.1" to my assets/package.json
  2. Added import "d3" to my assets/js/app.js
  3. Confirmed that the app.js that is loaded in the browser does indeed contain the d3 library.

But no matter what I seem to do every time I try to use d3 I get:

ReferenceError: d3 is not defined

I’m not a web developer. Phoenix generally has been easy enough to figure out because I am pretty good with Elixir, but I am having a really hard time getting this javascript stuff to work. Are my above steps correct? What else am I supposed to be doing?

Showing Posts 1 to 5

kokolegorille

kokolegorille

Hello, welcome to the forum,

Between 1 and 2 You should

npm install (or yarn install)

I am not sure for your syntax in 2, I think it should be something like

import d3 from 'd3';
grantwest

grantwest OP

Thanks kokolegorille, but it still isn’t working.

I have looked through the d3 code, and it does not assign the d3 variable anywhere. I can search for d3 = or with regex d3\s*= but I get nothing. Maybe this is why the import is not working? Maybe a different import method is required?

The simple D3 tutorials show this:

<script src='https://d3js.org/d3.v4.min.js'></script>

<script>
    d3.select('h3').style('color', 'darkblue');
    d3.select('h3').style('font-size', '24px');
</script>

I’m not sure how to translate that to canonical Phoenix.

sfusato

sfusato

Looking at D3.js wiki, try to import it like this in every Javascript file you use it:

import * as d3 from "d3";

Then you should be able to reference d3 in your code.

adrianrl

adrianrl

Hi, welcome to the forum!

import * as d3 from 'd3';

should work as mentioned above, you can also add it to the Webpack dependencies by creating a new entry point:

entry: {
  app: ['./js/app.js', './css/app.css'],
  vendor: ['d3'],
},

or using a more complex configuration, so Webpack takes care of splitting your vendor dependencies:

optimization: {
  splitChunks: {
    cacheGroups: {
      commons: {
        test: /[\\/]node_modules[\\/]/,
        name: 'vendor',
        chunks: 'all',
      },
    },
  },
},

I didn’t test it with D3, but should work. Have in mind that D3 is a heavy dependency, so splitting your JavaScript files in smaller chunks should help to build a better experience for your users.

peerreynders

peerreynders

Using tutorial: Simple Bar Chart Video Tutorial

$ mix phx.new d3jsDemo --no-ecto

    * creating d3jsDemo/config/config.exs
    ...
    * creating d3jsDemo/assets/static/favicon.ico

    Fetch and install dependencies? [Yn] Y
    * running mix deps.get
    * running cd assets && npm install && node node_modules/webpack/bin/webpack.js --mode development
    * running mix deps.compile

    We are almost there! The following steps are missing:

        $ cd d3jsDemo

    Start your Phoenix app with:

        $ mix phx.server

$ cd d3jsDemo
d3jsDemo $ mix phx.server

    Compiling 12 files (.ex)
    Generated d3jsDemo app
    [info] Running D3jsDemoWeb.Endpoint with cowboy 2.6.1 at 0.0.0.0:4000 (http)
    [info] Access D3jsDemoWeb.Endpoint at http://localhost:4000

    Webpack is watching the files…

    Hash: b9775afde64001345ea9
    Version: webpack 4.4.0
    Time: 382ms
                    Asset       Size       Chunks             Chunk Names
           ../css/app.css   10.6 KiB  ./js/app.js  [emitted]  ./js/app.js
                   app.js   7.23 KiB  ./js/app.js  [emitted]  ./js/app.js
           ../favicon.ico   1.23 KiB               [emitted]  
            ../robots.txt  202 bytes               [emitted]  
    ../images/phoenix.png   13.6 KiB               [emitted]  
       [0] multi ./js/app.js 28 bytes {./js/app.js} [built]
    [../deps/phoenix_html/priv/static/phoenix_html.js] 2.17 KiB {./js/app.js} [built]
    [./css/app.css] 39 bytes {./js/app.js} [built]
    [./js/app.js] 493 bytes {./js/app.js} [built]
        + 2 hidden modules
    Child mini-css-extract-plugin node_modules/css-loader/dist/cjs.js!css/app.css:
        [./node_modules/css-loader/dist/cjs.js!./css/app.css] 284 bytes {mini-css-extract-plugin} [built]
        [./node_modules/css-loader/dist/cjs.js!./css/phoenix.css] 10.9 KiB {mini-css-extract-plugin} [built]
            + 1 hidden module

Check in the browser http://localhost:4000/ to see the “Phoenix Framework” page.

^C

    BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
           (v)ersion (k)ill (D)b-tables (d)istribution
a
d3jsDemo $ cd assets
assets $ npm i d3 -D
    npm WARN assets No description

    + d3@5.9.2
    added 33 packages from 1 contributor and audited 14506 packages in 7.422s
    found 2 low severity vulnerabilities
      run `npm audit fix` to fix them, or `npm audit` for details
assets $

Edit d3jsDemo/assets/js/app.js

import css from '../css/app.css'
import 'phoenix_html'
import { select } from 'd3-selection'

const chartData = [30, 86, 168, 281, 303, 365]

select('.chart')
  .selectAll('div')
  .data(chartData)
  .enter()
  .append('div')
  .style('width', d => d.toString() + 'px' )
  .text(d => d)

Edit d3jsDemo/assets/styles/app.css

@import "./phoenix.css";

.chart div {
  font: 10px sans-serif;
  background-color: steelblue;
  text-align: right;
  padding: 3px;
  margin: 1px;
  color: white;
}

Edit d3jsDemo/lib/d3jsDemo_web/templates/page/index.html.eex at the end

  </article>
</section>
<section class="chart"></section>
assets $ cd ..
d3jsDemo $ mix phx.server
    [info] Running D3jsDemoWeb.Endpoint with cowboy 2.6.1 at 0.0.0.0:4000 (http)
    [info] Access D3jsDemoWeb.Endpoint at http://localhost:4000

    Webpack is watching the files…

    Hash: 9560b5464b76ab875009
    Version: webpack 4.4.0
    Time: 547ms
                    Asset       Size       Chunks             Chunk Names
           ../css/app.css   10.8 KiB  ./js/app.js  [emitted]  ./js/app.js
                   app.js   80.7 KiB  ./js/app.js  [emitted]  ./js/app.js
           ../favicon.ico   1.23 KiB               [emitted]  
            ../robots.txt  202 bytes               [emitted]  
    ../images/phoenix.png   13.6 KiB               [emitted]  
       [0] multi ./js/app.js 28 bytes {./js/app.js} [built]
    [../deps/phoenix_html/priv/static/phoenix_html.js] 2.17 KiB {./js/app.js} [built]
    [./css/app.css] 39 bytes {./js/app.js} [built]
    [./js/app.js] 314 bytes {./js/app.js} [built]
        + 53 hidden modules
    Child mini-css-extract-plugin node_modules/css-loader/dist/cjs.js!css/app.css:
        [./node_modules/css-loader/dist/cjs.js!./css/app.css] 433 bytes {mini-css-extract-plugin} [built]
        [./node_modules/css-loader/dist/cjs.js!./css/phoenix.css] 10.9 KiB {mini-css-extract-plugin} [built]
            + 1 hidden module
    [info] Replied phoenix:live_reload :ok

Checking the browser at http://localhost:4000/ you should see:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews