gus

gus

Nerves Core Team

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

Showing Posts 1 to 10

aptinio

aptinio

  1. In your app.css, add
@import "simplemde";
  1. In your app.js, check if #post_body exists:
var postBody = document.getElementById('post_body');

if (postBody) {
  new SimpleMDE({
    element: postBody,
    spellChecker: true
  })
}
mythicalprogrammer

mythicalprogrammer

Welcome to elixir forum.

Did you manually add that?

Cause npm install --save "yourpackagename" will add it to your package.json file.

As for SimpleMDE I believe, correct me if I’m wrong, you need to tell webpack that it’s global. Check your javascript console if it’s complaining about SimpleMDE not being found.

-  9 module.exports = (env, options) => ({
+ 10   optimization: {...}
+ 27   entry: {...}
+ 30   output: {...}
+ 34   module: {...}
| 63   plugins: [
| 64     new MiniCssExtractPlugin({ filename: '../css/app.css' }),
| 65     new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
- 66     new Webpack.ProvidePlugin({ // inject ES5 modules as global vars
2 67       $: 'jquery',
2 68       jQuery: 'jquery', 'window.jQuery': 'jquery',
2 69       Popper: ['popper.js', 'default'],
2 70       Fuse: 'fuse.js',
2 71     }),
| 72   ],
| 73 });

So line 66, you can put SimpleMDE: 'simplemde' like I did with $: 'jquery',. Making it global so all other javascript can detect it if it’s expecting it to be named SimpleMDE.

adrianrl

adrianrl

Hi,

Is up to you, I’d likely use a public CDN if the dependency is very large (and indeed 263kb of JavaScript + 10.7kb of CSS is heavy enough!), also a CDN helps you to speed up your webpage if the user has already cached that dependency in its browser. However it’s totally fine if you want to serve SimpleMDE by yourself, just have in mind that you don’t need it in every page, so you should split that dependency from your app bundle. app.js will load by default in every page, so it’s a nice file to put some dependencies to interact with your UI (React.js, Vue.js, jQuery…), nothing more, it should be as lightweight as possible to load faster.

Most of the time I generate a vendor.js from the dependencies of app.js to keep both small.

gus

gus OP

Nerves Core Team

Hi @aptinio -

Thanks for the quick reply! I tried adding @import "simplemde" to the css file and the suggestion to wrap my simplemde object with an if statement in app.js, however, I was running into further errors. Apparently, it cannot find the module.

Error: Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './simplemde'

Also, what is the difference between @import and import? Is the @ only used in a css file?

Thanks

gus

gus OP

Nerves Core Team

Hi @mythicalprogrammer -

Thanks for the warm welcome.

Did you manually add that?

Yeah I did - hadn’t realized that npm would take care of this for me! I undid my work and added it with this command. Out of curiosity, why did npm add it to dependencies instead of devDependencies (which is where I had put it originally)?

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

ReferenceError: Webpack is not defined

Thanks!

gus

gus OP

Nerves Core Team

Hi @adrianrl -

Thanks for this suggestion. I guess I hadn’t thought much about how a CDN can speed up the loading time of the application. With this suggestion, I probably will revert back to using the CDN for this dependency. However, I think it is still valuable for me to figure out how to properly import and use node modules, because I will be doing it more in the future. That being said, is it typically a better practice to write javascript within a script tag of the html, or to create a separate file that can be loaded via the src attribute of a script tag?

Thanks!

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

I think, inline JavaScript (that’s how it’s called), is used by people who doesn’t want to add extra configuration to Webpack, you’ve to set the dependency pulled from a CDN as a external dependency, just like @mythicalprogrammer did with Webpack.ProvidePlugin().

Typically with inline JavaScript you initialize / configure dependencies that are pulled from a CDN, because by default, Webpack will assume you have that dependency installed locally. When you use a library from a CDN it exposes a global object (SimpleMDE, React, ReactDOM…), that’s why you can configure it using inline JavaScript.

gus

gus OP

Nerves Core Team

Ah yes, that makes sense. Thanks for following up. I had figured webpack wasn’t reconfiguring on save, so I was doing that when I made changes. Thanks for the help!

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
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
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews