Tringlyman
Tringlyman3w ago

NodeJS projects to Deno projects

I just recently found out about Deno and I already have used NodeJS in other projects and most clones of projects in github and such also use NodeJS The point is that I want to have the forks/clones but using Deno instead While I can't clone/fork them with a Deno version I will have to change it manually for compatibility purposes Is there any important features that I should be aware of before trying out? :deno_logo: :deno_logo: :deno_logo: -# for example: -# having to use deno.json or smth and remove package.json An idea got but it's rather complicated and not acceptable in the guidelines of #ideas is for a CLI command like deno nodeToDeno or smth with an argument which can be the path that it will check on which it could read nodeJS exclusive code and log it with an alternative for compatibility purposes I think that's all I need to say Feel free to ask me smth if you don't understand
19 Replies
veodium
veodium2w ago
Deno is compatible with Nodejs. what you should do is just adding deno.json and type
"nodeModulesDir": "auto"
"nodeModulesDir": "auto"
Go to cmd and type
deno task <script>
deno task <script>
If you want to port Nodejs code to Deno, yes - you have to do all of that manually
Tringlyman
TringlymanOP2w ago
I want to know code related stuff that I should change for deno Like replace commonjs module system with es6's
veodium
veodium2w ago
you have to add ".ts" to end of each import local code. like
import foo from "./foo"
import foo from "./foo"
to
import foo from "./foo.ts"
import foo from "./foo.ts"
Tringlyman
TringlymanOP2w ago
What if I use JavaScript (js) or Express (ejs) or JavaScript XML (jsx)
veodium
veodium2w ago
then add extension name like
import foo from "./foo.jsx"
import foo from "./foo.jsx"
veodium
veodium2w ago
Check this: https://docs.deno.com/runtime/reference/cli/unstable_flags/ sloppy import suppports importing without extension. but it doesn't support compile and it is unsafe feature 🙁
Deno
Unstable feature flags
In-depth documentation, guides, and reference materials for building secure, high-performance JavaScript and TypeScript applications with Deno
Tringlyman
TringlymanOP2w ago
Ok Any other stuff I should change in code for deno compatibility -# not using nodeJS
veodium
veodium2w ago
You mean you want to replace all Nodejs functions to Deno? like writeFile of Nodejs to Deno.writeFile ?
Tringlyman
TringlymanOP2w ago
Pretty much I just want to overall make it use deno so I can use a newer and fresher runtime instead of an older one -# see want I did there😏
Antonio Sampaio
I just finished porting an entire codebase from nodejs to deno 2 frontends, 1 backend, 5 packages You'll have to update import file extensions like veodium said you have to check if all the libs you're working with is working fine. any libs that manage binaries or external stuff needs to be tested like Prisma ORM, @aws-sdk*, googleapis, etc one very good i can say: you can clean all the garbage that comes with nodejs (biome, eslint, prettier, tsc, esbuild, swc, vitest, jest etc) deno have everything you need by default
Tringlyman
TringlymanOP2w ago
And how do I do that? If you mean smth other than files manually added to work with like TS config
Antonio Sampaio
all you need is deno.json
deno fmt
deno check
deno lint
deno bundle
deno compile
deno test
deno fmt
deno check
deno lint
deno bundle
deno compile
deno test
Tringlyman
TringlymanOP2w ago
so I format, check(?), lint, bundle, compile and test? could you give me an explanation as of why these particularly and what does check do?
veodium
veodium2w ago
deno check
deno check
prints all warnings and type errors
Antonio Sampaio
you can format all your files with deno fmt just like prettier you can manage its defaults at deno.json -> "fmt" deno check does type checking just like tsc, you can configure its defaults at deno.json -> "compilerOptions" deno lint (eslint, biome) will blame bad patterns based on rules defined at deno.json -> "lint"
i'm tired of typing, you can read the docs for the CLI https://docs.deno.com/runtime/reference/cli/check/
Deno
deno check
Download and type-check code without execution
None
None2w ago
Check does the same as tsc --noEmit
jeff.hykin
jeff.hykin2w ago
I know everyone said "manually" but actually I made a tool that does 90% automatically. It converts imports/exports/require makes sloppy imports non-sloppy (e.g. adds .js .ts and handles importing a folder as index.js/.ts), adds prefixes for node: and npm: and adds FIXME for stuff like dynamic imports that it can't handle: https://github.com/jeff-hykin/to-esm/ It uses deno tree sitter (another library I maintain) to do actual parsing (not regex) over the codebase.
GitHub
GitHub - jeff-hykin/to-esm
Contribute to jeff-hykin/to-esm development by creating an account on GitHub.
Tringlyman
TringlymanOP2w ago
So you went through the process of making automatic module system upgrade? Cool
jeff.hykin
jeff.hykin2w ago
Thanks! Yeah I don't use a library unless I can either import it with esm.sh (auto converts code much more reliably than my to-esm but its messy/compiled) or import it with relative imports. So after manually converting ~12 codebases I got pretty tired of doing everything manually

Did you find this page helpful?