n1c0

n1c0

Phoenix 1.6 & esbuild having issues with web3

Hello everyone,

I am upgrading a phoenix 1.5 app to 1.6 and I am running into some issues with the way npm packages are handled with esbuild.

I am trying to use the web3 js library to connect a wallet via metamask and the likes…it works perfectly with webpack/phoenix 1.5 but when I use esbuild and phoenix 1.6 I am facing the following issue:

index.js:20 Uncaught ReferenceError: require is not defined
    at node_modules/web3-core-requestmanager/lib/index.js (index.js:20)
    at __require (app.js:26)
    at node_modules/web3-core/lib/index.js (index.js:22)
    at __require (app.js:26)
    at node_modules/web3/lib/index.js (index.js:29)
    at __require (app.js:26)
    at app.js:159

JS & its non-sense not really my area of expertise, tried a few stuff, adding some npm packages etc…but in vain

here’s my config.exs section on esbuild:

config :esbuild,
  version: "0.14.10",
  default: [
    args:
      ~w(js/app.js --bundle --sourcemap --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
    cd: Path.expand("../assets", __DIR__),
    env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
  ]

and my package.json

{
  "scripts": {
    "deploy": "NODE_ENV=production npx tailwindcss --postcss --minify --input=css/app.css --output=../priv/static/assets/app.css"
  },
  "devDependencies": {
    "autoprefixer": "^10.4.0",
    "postcss": "^8.3.11",
    "postcss-cli": "^9.0.2",
    "postcss-import": "^14.0.2"
  },
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.15.4",
    "@tailwindcss/aspect-ratio": "^0.3.0",
    "@tailwindcss/forms": "^0.3.4",
    "@tailwindcss/typography": "^0.4.1",
    "@walletconnect/web3-provider": "^1.7.0",
    "alpinejs": "^3.5.2",
    "eth-sig-util": "^3.0.1",
    "nprogress": "^0.2.0",
    "tailwindcss": "^2.2.19",
    "web3": "^1.6.1",
    "web3modal": "^1.9.5"
  }
}

I have seen the doc on using plugins for esbuild in the phoenix hexdocs pages but no idea on how to do that and if it will solve my issue.

Thanks in advance!

Most Liked

barberj

barberj

Running into same issue. Did you get this resolved?

My project is fairly vanilla. New starting from phx 1.6.

package.json

{
  "dependencies": {
    "web3": "^1.7.3"
  }
}

config/config.exs

# Configure esbuild (the version is required)
config :esbuild,
  version: "0.14.0",
  default: [
    args:
      ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
    cd: Path.expand("../assets", __DIR__),
    env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
  ]

In my app.js just importing import "web3"

n1c0

n1c0

Hi there,

I have found a workaround maybe not the most elegant…I gathered the minified js libraries into the vendor folder and put that in my app.js

import Web3 from "../vendor/web3.min";
import WalletConnect from "../vendor/walletclient.min";
import WalletConnectQRCodeModal from "../vendor/qrcode-modal.min";

It works fine like that but f you find another solution, please let us know as with this one I need to manually update for new versions of the libraries…

baldmountain

baldmountain

There is a better way to fix this. Update your esbuild config like this:

config :esbuild,
  version: "0.12.18",
  default: [
    args: ~w(js/app.js --bundle --target=es2016 --outdir=../priv/static/assets),
    cd: Path.expand("../assets", __DIR__),
    env: %{"NODE_PATH" => "#{Path.expand("../deps", __DIR__)}:#{Path.join(__DIR__, "node_modules")}"}
  ]

Note that there are two entries for NODE_PATH. One for deps and one for node_modules. You can then add whatever Javascript and CSS to node_modules and then import it into app.js and app.css.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
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

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement