Monotter
Monotter15mo ago

Importing local NodeJS module from Deno

Is it possible to import a javascript file that has node imports for standart modules like http just designed for NodeJS use? For Example:
//main.ts [Deno]
import crypto from 'node:./module.js'
const data = crypto.createHmac('sha256', secret).update('I love cupcakes').digest('hex');
console.log(data)
//main.ts [Deno]
import crypto from 'node:./module.js'
const data = crypto.createHmac('sha256', secret).update('I love cupcakes').digest('hex');
console.log(data)
//module.js [Node]
import crypto from 'crypto'
export { crypto }
//module.js [Node]
import crypto from 'crypto'
export { crypto }
I am asking this because, I am using sveltekit-deno-adapter to use Svelte on Deno Deploy. Unfortunately, when I use Axios on server-side script, it gives Relative import path "events" not prefixed with / or ./ or ../ and not in import map from "file:///Users/x/x/x/x/build/server.js" error when I start Deno after the build. I can only think on 2 possibilities at this point, 1st one is forking sveltekit-deno-adapter to replace all Node imports with node: prefix before the import. And the 2nd is just using fetch on server-side code instead of Axios. 2nd one looks more reasonable in short term perspective but this issue is not happening only on Axios, do I have to throw away the modules that I use every time?
3 Replies
Deno AI Helper
Deno AI Helper15mo ago
Based on the documentation, Deno does support importing Node.js built-in modules using the node: prefix. However, it's important to note that importing via a bare specifier (like import crypto from 'crypto') without an import map is not supported in Deno. If you attempt to do so and the bare specifier matches a Node.js built-in module not found in an import map, Deno will provide a helpful error message asking if you meant to import with the node: prefix. In your case, if you're trying to import a local Node.js module into a Deno script, you would need to ensure that all Node.js built-in modules in the local module are imported with the node: prefix. For example, your module.js file should look like this:
//module.js [Node]
import crypto from 'node:crypto'
export { crypto }
//module.js [Node]
import crypto from 'node:crypto'
export { crypto }
Then, you can import this local module into your Deno script like so:
//main.ts [Deno]
import { crypto } from './module.js'
const data = crypto.createHmac('sha256', secret).update('I love cupcakes').digest('hex');
console.log(data)
//main.ts [Deno]
import { crypto } from './module.js'
const data = crypto.createHmac('sha256', secret).update('I love cupcakes').digest('hex');
console.log(data)
Regarding the issue with Axios, it's likely that Axios or one of its dependencies is using a bare specifier to import a Node.js built-in module, which is causing the error you're seeing. One solution could be to use an import map to map these bare specifiers to their corresponding node: specifiers. However, this could be a complex task if there are many such imports. As you mentioned, another solution could be to use fetch instead of Axios for server-side requests. This would be a more straightforward solution, and fetch is a standard API that works in both Deno and the browser, which could make your code more portable. In the long term, it might be worth considering whether the npm packages you're using are compatible with Deno,
Deno AI Helper
Deno AI Helper15mo ago
or if there are Deno-compatible alternatives available. Deno is still relatively new compared to Node.js, and not all npm packages will work out of the box with Deno.
Monotter
MonotterOP15mo ago
I weren't know there was a thing like import map, I will definitely use that way. Thank you! https://docs.deno.com/runtime/manual/basics/import_maps
Import Maps | Deno Docs
In order for Deno to resolve a bare specifier like "react" or "lodash", it