Eureka moment: Fixing import problems in Deno project
I just had to share my excitement after figuring out a bunch of tricky import issues in my Deno project. Maybe this will help someone else who’s pulling their hair out over similar problems.
For the longest time, I was stuck using ESM imports because NPM imports were giving me weird errors. I thought it might be some conflict between server-side and client-side code, but I wasn’t sure. Using ESM imports fixed that, but then I ran into a whole new set of problems with package dependencies clashing. It was driving me crazy!
Turns out, the real culprit was something much sneakier. It had to do with how Deno was handling the node_modules folder and its cache.
The game-changer was tweaking my deno.json file. Here’s what I did:
-
Set “nodeModulesDir”: “none” in deno.json
-
Changed the storybook task to:
“storybook”: “deno run --node-modules-dir -A npm:storybook dev -p 6006 --no-open”
In my package.json, I set “type”: “commonjs”.
Now, NPM imports in deno.json stay out of node_modules, while package.json imports go into node_modules. No more conflicts! I can use npm: imports in deno.json without any issues.
If you’re struggling with similar problems, give this a try. It might save you a lot of headaches!
Man, I feel your pain with those import hassles. Been there, done that, and it’s no picnic. Your solution is pretty slick, especially that nodeModulesDir trick. I had a similar headache a while back with a Deno project, and what saved my bacon was using import maps. They’re like a Swiss Army knife for imports.
Here’s what worked for me: I created an import_map.json file in my project root and specified all my dependencies there. Then I added --import-map=import_map.json to my Deno run commands. It was like magic - suddenly all my import woes vanished.
Your approach with deno.json tweaks is definitely worth keeping in the toolbox, though. It’s amazing how often these little config changes can make or break a project. Thanks for sharing your eureka moment - it’s nuggets like these that make the dev community awesome.
Nice find! those import issues can be a real pain. i’ve been stuck on similar probs with deno before. didnt know about that nodeModulesDir trick tho - gonna try it out on my next project for sure. thx for sharing ur solution, might save me some headaches down the road!
I’m glad you found a solution to your import issues! I’ve been down that rabbit hole before, and it can be incredibly frustrating. Your fix with the deno.json configuration is clever - I hadn’t considered the impact of the node_modules handling on import conflicts.
One thing I’ve found helpful in similar situations is to use import maps. They allow you to specify custom import paths and can help manage dependencies more explicitly. It might be worth looking into as an additional tool for your project, especially if you’re dealing with a mix of ESM and CommonJS modules.
Thanks for sharing your experience. It’s always valuable to see how others solve these tricky development problems.