Auke
Auke4mo ago

Setup for using npm packages locally + via server.ts

Hi Deno, I have a project in which I use Deno both to execute ts/js files locally to compute features, using a command like: deno run --allow-all --v8-flags=--max-old-space-size=2192 compute.ts This compute.ts script uses .js scripts that are also used in a webpage I serve using a simple server.ts file, which I serve using deno: deno run -A server.ts which contains the following code to serve files:
Deno.serve((req: Request) => {
let pathname = new URL(req.url).pathname;

// fonts endpoint
if (pathname.startsWith("/fonts")) {
return fonts_endpoint(req, pathname);
}

// Canonicalize path
if (pathname === "/" || pathname === "") {
pathname = "/index.html";
}
const filepath = "." + pathname;
console.log(filepath);

const isReadableFile = existsSync(filepath, {
isReadable: true,
isFile: true,
});

if (isReadableFile) {
return serveFile(req, filepath);
}

// Otherwise the requested resource is not available
return new Response("404: Not Found", {
status: 404,
});
});
Deno.serve((req: Request) => {
let pathname = new URL(req.url).pathname;

// fonts endpoint
if (pathname.startsWith("/fonts")) {
return fonts_endpoint(req, pathname);
}

// Canonicalize path
if (pathname === "/" || pathname === "") {
pathname = "/index.html";
}
const filepath = "." + pathname;
console.log(filepath);

const isReadableFile = existsSync(filepath, {
isReadable: true,
isFile: true,
});

if (isReadableFile) {
return serveFile(req, filepath);
}

// Otherwise the requested resource is not available
return new Response("404: Not Found", {
status: 404,
});
});
and serves plain html, .js and .ttf files. I have this setup working for a long time, but it is a mess - I have installed npm libraries using npm, access those javascript files by using direct paths: import * as FloBezier from "./node_modules/flo-bezier3/browser/index.js"; and I do this as a solution to have this javascript file working both locally (using deno run cli) and when accessing the file through the browser (using the server.ts above). Is there a more cannonical approach to doing this? Key requirement is being able to use the same js file both locally and in the browser
1 Reply
Auke
AukeOP4mo ago
I've looked in the deno documentation, but I haven't found a guide/tutorial for this setup, so feel free to point me in that direction if there is one! I feel like i can't use the npm specifiers to refer to packages either, as those will break when serving .js files with paths starting with npm: Okay, I think I should look at the Deno import maps again Hmm, that doesn't help when serving the plain js files, as then the paths don't get resolved through the import map. My question boils down to: How can I setup my npm libraries such that I can use them both locally and when included in javascript files served to the web? Say I have an import like this; import * as FloBezier from "./node_modules/flo-bezier3/browser/index.js"; This works, but I don't leverage any of the npm functionality provided by Deno, and the import url looks overly specific to me