Module imports not recognised

I’m having no end of problems attempting to import libraries. Every time I attempt to import a library it claims it can’t locate it. It doesn’t seem to recognise libraries that are located in the node_modules folder at all.

The Problem
I’m attempting to install a couple of packages ‘itty-router’ & ‘@immersa/auth0-jwt-validator’ to perform some authentication process in a worker acting like a rest Api, with auth capabilities. None of the libraries will be recognised when I attempt to publish or run dev on the work via wrangler. It always claims it can’t resolve the libraries.

When I type “wrangler publish” or “wrangler dev” in the command, it returns an error

Error: Something went wrong with the request to Cloudflare...
Uncaught Error: internal error
 [API code: 10021]

Or

Uncaught Error: No such module "common/router/@immersa/auth0-jwt-validator".
  imported from "common/router/authenticationHandler.js"
 [API code: 10021]

The Configuration
/packages.json:

{
  "private": true,
  "name": "api_v1",
  "version": "1.0.0",
  "description": "A template for kick starting a Cloudflare Workers project",
  "main": "src/index.js", // this is where the start index.js lives. (confirmed)
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "format": "prettier --write '**/*.{js,css,json,md}'"
  },
  "author": "omitted",
  "license": "MIT",
  "devDependencies": {
    "prettier": "^1.18.2"
  },
  "dependencies": {
    "@immersa/auth0-jwt-validator": "^1.0.1" // I uninstalled titty router because I wrote my own (works)
  }
}

/wrangler.toml:

name = "api_v1"
type = "javascript"
workers_dev = true

account_id = ""
route = ""
zone_id = ""
compatibility_date = "2022-11-30"

[build.upload]
format = "modules"
dir = "./src"
main = "./index.js" // with dir and main combined this works out to be src/index.js (confirmed)

[[build.upload.rules]]
type = "ESModule"
globs = ["**/*.js"]

# Uncomment if you have a build script.
# [build]
# command = "npm run build"

There is no typescript configured.

versions

  • wrangler 1.20.0
  • node v16.14.0
  • npm v8.3.1

What I have attempted
I have written my own router which seems to work after giving up on the titty-router not importing.
It’s basically a port of titty with an authentication middleware added in.

I got to the stage of dealing with JWT tokens and validating them against an authority. Had the same issue, but didn’t want to role my own version of that due to the risk of getting it wrong…

I even threw this worker out completely and attempted to get a tutorial from the official site to work. Followed all the steps, and it complained about using import outside of a module. Even though I had not customised the tutorial at all. None of the tutorials seem to work out of the box for me.

Request
It feels like I have some configuration or something fundamentally wrong with my configuration of my PC or environment. And I’m unable to recognise what it is… I have the worker working and published at this point of time, but I feel like I can’t continue if I can’t leverage off existing libraries. Feels like it’s old school coding well you need to roll everything yourself.
So, I would really appreciate if someone could help me to identify this!

Thanks in advance
AJ

Wrangler 1 is very old and missing lots of features. I would suggest that you migrate to wrangler 2.

Yes, I installed that based off an old tutorial it seemed and didn’t get the memo that wrangler 2 was out of beta. I’ve installed version 2 now, and I can see new errors complaining about the node_modules installed. Meaning it is at least looking at them now. I should be able to move forward. Thanks for pointing that out.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.