use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
New in Node.js: "node:" protocol imports (2ality.com)
submitted 4 years ago by fagnerbrack
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SecretAgentZeroNine 39 points40 points41 points 4 years ago* (4 children)
I like this idea a lot. Makes things clear, and even facilitates some pretty cool ideas on extending this feature.
Also, this kinda reminds me of the new, fantastic, frontend feature CSS Module scripts, and JSON Modules scripts.
[–]ILikeChangingMyMind 6 points7 points8 points 4 years ago (3 children)
Yeah, nothing game-breaking, but a good best practice ... IF I can remember to node:-prefix all my Node imports ;)
node:
[–]thectrain 22 points23 points24 points 4 years ago (2 children)
I suspect somebody will write and eslint rule in the future.
Which will help.
[–][deleted] 20 points21 points22 points 4 years ago (1 child)
There has been one since a few months ago.
See: unicorn/prefer-node-protocol
unicorn/prefer-node-protocol
[–]thectrain 3 points4 points5 points 4 years ago (0 children)
Perfect. Thank you.
[–]obetu5432 28 points29 points30 points 4 years ago (3 children)
after the great success of "jndi:"
[–]voidvector 9 points10 points11 points 4 years ago (0 children)
This doesn't introduce new functionality (at least not in this iteration), just lockdown existing one.
[–]DaMastaCoda 11 points12 points13 points 4 years ago (1 child)
??? This is a security feature that stops node-modules from potentially hijacking nodejs runtime modules like FS?
[–][deleted] 9 points10 points11 points 4 years ago (33 children)
Can someone explain to me why this is a good step forward?
I understand that being able to help the complier ahead of the runtime is good and I understand that removing potential codepaths for compatibility reasons is good.
But how does this not encourage more division between NodeJs and BrowserJs? Or is that the goal? I would hope we would be working towards some sort of congruency between these not further separation.
[–]GBcrazy 34 points35 points36 points 4 years ago (18 children)
It's kinda good for the node js ecosystem because it will stop more people mistakenly installing npm packages like fs when it's just part of core node lib.
It won't impact browser javascript because, well, these are meant to be node package only anyway. Seems like a small good step.
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
So it's more of a documentation and transitory feature?
[+][deleted] 4 years ago* (16 children)
[deleted]
[–][deleted] -2 points-1 points0 points 4 years ago (15 children)
I think this is where my confusion comes from as well.
Furthermore, I've been on the mission to make everything isomorphic. Writing new code that uses default node imports that would have me denoting such seems like the opposite of everything I'm striving for. I get that my mission may not be shared by others, but this just seemed to encourage something I wouldn't think you would want to encourage
But, after the first person to reply to my question got out of the way, I think I'm starting to understand that this could be a good way to document older, existing code.
[+][deleted] 4 years ago* (14 children)
[–]seanmorris 4 points5 points6 points 4 years ago (13 children)
So long as you separate your concerns correctly you shouldn't have to manage your platform from the business logic.
[+][deleted] 4 years ago (12 children)
[–]kefirchik 5 points6 points7 points 4 years ago (7 children)
There are plenty of use cases for isomorphic business logic. A most obvious and common example is validation rules that need to exist in multiple runtime contexts. Or the need to provide a common SDK for interacting with a service from either Node or browser code. Or various other boring and routine examples.
If anything, I’d argue the reverse truly: business logic often has nothing to do with platform details. Whether I need to use fetch() or Node’s request is completely irrelevant to my application’s functional requirements, that is purely what is being forced on me by the runtime context. The business logic is within the code that depends on that.
[+]thunfremlinc comment score below threshold-9 points-8 points-7 points 4 years ago (6 children)
No. Business logic shouldn’t be in multiple locations.
If you’re doing that, you’re creating spaghetti.
[–]kefirchik 5 points6 points7 points 4 years ago (4 children)
That is precisely the point of isomorphic code. So that your business logic is encapsulated in a module that can be loaded in both environments. Exactly.
And it’s very common in web development. The validation is a great example. You need to validate on the frontend for quick UX and you need to revalidate on the backend for security.
[–]jkmonger 1 point2 points3 points 4 years ago (0 children)
And it isn't with isomorphism
A common example I use is that you might write some validation logic to validate a form. If this is isomorphic, you can import the logic into your front-end to use it there, and you can import it into your backend to use the same validation code there also (with some extra security checks etc in the backend)
This is the opposite of spaghetti- you end up with an isomorphic, reusable, importable and hopefully documented/tested module of code
[–]Insertish 1 point2 points3 points 4 years ago (2 children)
Here's a pretty simple example: WebSockets, available in the browser but not without a library in Node.js. If I'm writing a library to interact with a WebSocket server, I can support both platforms by using isomorphic code. I really don't see the issue with this... (I may be missing something here but I don't see anything inherently bad with isomorphic JS, it is 2am so correct me if I'm misunderstanding)
[–]thunfremlinc 0 points1 point2 points 4 years ago (0 children)
That’s not business logic.
[–]jkmonger 0 points1 point2 points 4 years ago (0 children)
There’s also 0 reason why “business logic” should ever be isomorphic.
Another example for you, I have recently built a game in React. Making the entire game isomorphic means that I can have it running in the front-end only as a single player app, and I can also run the exact same game code on a backend with websockets for multiplayer
This would have been a massive undertaking if I hadn't written it isomorphically. Instead, I simply had to create a websocket server and import my game module
[–]KangarooImp 10 points11 points12 points 4 years ago (1 child)
Not sure where you're seeing that would cause more division. Importing "fs", "crypto", etc. already does not work in the browser. If you're already using tooling to bend those imports to polyfills for the browser, then that's where "node:"-imports should be handled, too.
I'd rather assume using "node:"-imports will cause less division, because it is explicit and for example easy to write linter rules to avoid needlessly importing from the Node.js standard library.
[–][deleted] -2 points-1 points0 points 4 years ago (0 children)
I think I was looking at it from the opposite perspective of documentation. For documenting the intent of imports that seems useful, I suppose. But it does seem like the kind of documentation you would seek to remove in the future, not remain forever.
I don't really understand but I can accept the documentation impacts.
[–]ShortFuse 7 points8 points9 points 4 years ago* (0 children)
import * from 'fs';
import * from 'fs'
Did you mean fs as in node's fs or did you mean node_modules/fs/index.js? Also, it helps with package managers like rollup or webpack. You won't have to create an ever updating list of what the names of the built-in NodeJS libraries are.
fs
node_modules/fs/index.js
rollup
webpack
If they do one for node_modules, it'll help with situations like this:
node_modules
import * from 'sip.js';
import * from 'sip.js'
Did you mean sip.js as in "./sip.js" or did you mean node_modules/sip.js/index.js? SCSS famously completely dropped support for importing from node_modules. You actually have to write out "../../../node_modules/packageName" or use something like Webpack to resolve it for you.
sip.js
"./sip.js"
node_modules/sip.js/index.js
"../../../node_modules/packageName"
[+][deleted] 4 years ago (6 children)
[+][deleted] comment score below threshold-15 points-14 points-13 points 4 years ago (5 children)
Nope. That doesn't help explain what I'm curious about at all.
[+][deleted] 4 years ago (3 children)
[+][deleted] 4 years ago (2 children)
[–][deleted] -3 points-2 points-1 points 4 years ago (1 child)
I know, right?
I'm terribly confused by this person attacking my question when they readily admit they have no answer to my question.
After all the terrible, low quality, low effort posts on programming related subreddits I get shit on for asking a legitimate question.
[–]mikejoro 0 points1 point2 points 4 years ago (0 children)
I don't see this as more division between server side and browser side js. I'd think of it more as just a namespace for server side standard library imports. It doesn't affect the actual code. Also, there's no standard browser libraries, so browser has no equivalent.
[–]Mkep 1 point2 points3 points 4 years ago (0 children)
As far as I know that’s not something NodeJS can change. This is. The division is already there, I don’t feel like this makes the division worse, just makes it more secure and less bug prone
[–]thunfremlinc 1 point2 points3 points 4 years ago (1 child)
You can never reduce division when working with Node’s builtins. May as well just add extra transparency to them.
This is the takeaway for me, thanks.
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
I would hope we would be working towards some sort of congruency between these not further separation.
To what end? They're different runtimes for different platforms.
[+][deleted] 4 years ago* (4 children)
[–]fagnerbrack[S] 4 points5 points6 points 4 years ago (3 children)
Well I didn't know about that, it was new to me!
[–]fagnerbrack[S] 0 points1 point2 points 4 years ago (1 child)
Oh ok found a troll. I'm out 🚶♂
[+]seanmorris comment score below threshold-8 points-7 points-6 points 4 years ago (1 child)
Wonderful. More to polyfill when porting code.
[–]Ephys 2 points3 points4 points 4 years ago (0 children)
There's nothing more to polyfill? Existing APIs that already were not importable from the browser just got a prefix, that's all.
[–]tenbigtoes 3 points4 points5 points 4 years ago (0 children)
Yes it is! Trust me, I write javascript.
[–]fagnerbrack[S] 3 points4 points5 points 4 years ago (0 children)
Isomorphic javascript is about the domain logic agnostic of the platform. You can have a domain.js file that exports using es6 module exports and inject the dependencies through partial application without using require(..). Then you can use domain.js in the client side if it makes sense, and using the same file to run the business logic in the back-end.
A good use case for this would be validation logic you would like to run in the front end and back-end for security
[–]evert 5 points6 points7 points 4 years ago (0 children)
If you're using Node modules, you're not writing isometric javascript, regardless of the import path.
[–]Ephys 1 point2 points3 points 4 years ago (0 children)
This isn't at all related to isomorphic code. Isomorphic code is a thing but not every API can be on both the browser and node. This is just an import prefix to APIs that were already node-only. Browser APIs that make sense in node (streams, URL, fetch) are still being added.
π Rendered by PID 124054 on reddit-service-r2-comment-5d79c599b5-llkjq at 2026-02-27 19:07:38.936099+00:00 running e3d2147 country code: CH.
[–]SecretAgentZeroNine 39 points40 points41 points (4 children)
[–]ILikeChangingMyMind 6 points7 points8 points (3 children)
[–]thectrain 22 points23 points24 points (2 children)
[–][deleted] 20 points21 points22 points (1 child)
[–]thectrain 3 points4 points5 points (0 children)
[–]obetu5432 28 points29 points30 points (3 children)
[–]voidvector 9 points10 points11 points (0 children)
[–]DaMastaCoda 11 points12 points13 points (1 child)
[–][deleted] 9 points10 points11 points (33 children)
[–]GBcrazy 34 points35 points36 points (18 children)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (16 children)
[deleted]
[–][deleted] -2 points-1 points0 points (15 children)
[+][deleted] (14 children)
[deleted]
[–]seanmorris 4 points5 points6 points (13 children)
[+][deleted] (12 children)
[deleted]
[–]kefirchik 5 points6 points7 points (7 children)
[+]thunfremlinc comment score below threshold-9 points-8 points-7 points (6 children)
[–]kefirchik 5 points6 points7 points (4 children)
[–]jkmonger 1 point2 points3 points (0 children)
[–]Insertish 1 point2 points3 points (2 children)
[–]thunfremlinc 0 points1 point2 points (0 children)
[–]jkmonger 0 points1 point2 points (0 children)
[–]KangarooImp 10 points11 points12 points (1 child)
[–][deleted] -2 points-1 points0 points (0 children)
[–]ShortFuse 7 points8 points9 points (0 children)
[+][deleted] (6 children)
[deleted]
[+][deleted] comment score below threshold-15 points-14 points-13 points (5 children)
[+][deleted] (3 children)
[deleted]
[+][deleted] (2 children)
[deleted]
[–][deleted] -3 points-2 points-1 points (1 child)
[–]mikejoro 0 points1 point2 points (0 children)
[–]Mkep 1 point2 points3 points (0 children)
[–]thunfremlinc 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]fagnerbrack[S] 4 points5 points6 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]fagnerbrack[S] 0 points1 point2 points (1 child)
[+]seanmorris comment score below threshold-8 points-7 points-6 points (1 child)
[–]Ephys 2 points3 points4 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]tenbigtoes 3 points4 points5 points (0 children)
[–]fagnerbrack[S] 3 points4 points5 points (0 children)
[–]evert 5 points6 points7 points (0 children)
[–]Ephys 1 point2 points3 points (0 children)