gus

gus

Nerves Core Team

Basics for using npm modules in Phoenix 1.4 application

Hello all,

I recently started experimenting with Phoenix for a personal project, and am loving it so far. However, I’ve gotten really stuck with Webpack and using node modules in my project. I don’t have any experience in front end development, and am in over my head with some of these topics.

I’m trying to use the node module simplemde to be able to turn a textarea into a markdown editor. While I was able to get it working by including the CDN in my html.eex file as follows:

<%= form_for @changeset, Routes.post_path(@conn, :create), fn f -> %>
  <%= label f, :title %>
  <%= text_input f, :title, required: true, placeholder: "Post Title" %>
  <%= error_tag f, :title %>

  <%= label f, :body %>
  <%= textarea f, :body, rows: 20, required: true, focusable: true, placeholder: "Post Body" %>
  <%= error_tag f, :body %>

  <div class="row">
    <div class="column">
      <%= submit "Publish" %>
    </div>
    <div class="column column-50">
      <%= link "Save", class: "button button-outline", to: Routes.post_path(@conn, :index), data: [confirm: "Really discard all changes?"] %>
    </div>
    <div class="column">
      <%= link "Discard", class: "button button-outline float-right", to: Routes.post_path(@conn, :index), data: [confirm: "Really discard all changes?"] %>
    </div>
  </div>
<% end %>


<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
<script type="text/javascript">
  new SimpleMDE({
    element: document.getElementById('post_body'),
    spellChecker: true
  })
</script>

I’ve been having great difficulty actually adding it to my project via webpack though. The steps I’ve taken have been:

Added to package.json and installed node modules

"simplemde": "^1.11.2"
npm install

Added the import to my app.js file:

import css from "../css/app.css"
import "phoenix_html"
import SimpleMDE from "simplemde"

Tried to use simplemde within a script tag of my html.eex file (for brevity, it is the same code as above, except I removed the stylesheet and script with the cdn src tag)

This resulted in the textarea returning to the plain html textarea, with no markdown controls, and an error that SimpleMDE is not defined in the browser console.

My next idea was to remove the script tag with the setup for simplemde from the html.eex file and move it to the app.js file. This worked, and the code is as follows:

import css from "../css/app.css"
import "phoenix_html"
import SimpleMDE from "simplemde"

new SimpleMDE({
  element: document.getElementById('post_body'),
  spellChecker: true
})

There seem to be two problems with this approach, however. 1) the formatting of the textarea isn’t correct. I believe I still need to import or include the stylesheet. 2) this solution applies to all of the pages across my application, because it is in the app.js file. So, when I go to any other page, I get console error messages that say SimpleMDE: Error. No element was found (like it should, since there is no element with id post_body on those pages).

So, from this I have several questions:
How do I share the modules that have been imported to the scripts within my html files?
What is the best practice for using javascript in my application - does it make more sense to always create a javascript file instead of embedding it into a script tag? (is that more maintainable?)
I’m quite certain that the CSS file for simplemde can be imported as well - how do I go about that, and in the future how do I figure out what the names of importable objects are?

I appreciate the help! Cheers

Most Liked

mythicalprogrammer

mythicalprogrammer

Out of curiosity, why did npm add it to dependencies instead of devDependencies (which is where I had put it originally)?

That’s another command:

npm install --save-dev "package-name-here"

Also, I am getting errors when trying to add the dependency to Webpack.ProvidePlugin… my understanding is that Webpack is not defined

Yeah that’s line 1 of my webpack file (added the first 1 to 8 lines that I skipped out on):

   1 const Webpack = require('webpack');
   2 const path = require('path');
   3 const glob = require('glob');
   4 const MiniCssExtractPlugin = require('mini-css-extract-plugin');
   5 const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
   6 const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
   7 const CopyWebpackPlugin = require('copy-webpack-plugin');
   8

Also watch out for iex -S mix phx.server. It caches webpack and doesn’t run webpack often enough. If you change anything to webpack config you should just rerun that command again to force it to rerun webpack.

aptinio

aptinio

Try:

@import "../node_modules/simplemde/src/css/simplemde.css";
adrianrl

adrianrl

Exactly! You do need to generate another bundle, so you must create another entry in the Webpack configuration, like this:

entry: {
  './js/app.js': ['./js/app.js'].concat(glob.sync('./vendor/**/*.js')),
  'simplemde_bundle': ['./js/editor.js'] // The key would be the name of the output
},

Then inside editor.js:

import SimpleMDE from 'simplemde'; // Include the dependency

SimpleMDE({ blah: 'blah' }); // and configure the object

Lastly include the script only when you need it:

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

Please remember to include the script below the element (I’ll assume a textarea), otherwise it won’t find where to use its magic. I can’t test the code, but this will guide you in the right direction, the CSS should be included inside the script too.

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement