What does ':' colon do in JavaScript's import?
Asked Answered
B

2

20

Given the example below.

import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

The use of the colon has confused me and never understood where the imported files could be referenced from.

Bishop answered 6/3, 2021 at 15:48 Comment(4)
It's not part of JavaScript; it's something relevant for whatever module "bundler" you're using. JavaScript just says that the from value has to be a string constant; it says nothing about the semantics of the string contents.Ambo
And do you have idea of which bundler could be able to translate that? And what does that mean for the bundler?Bishop
not 100% familar with adonis.js, but I suspect it has something to do with scoped packages <docs.npmjs.com/about-scopes>. Some tooling might have installed the packages as scoped packages, and node (I am assuming node?) can resolve the package.Wigwam
I don't recognize that, sorry. Where did you see it?Ambo
S
6

Prefixes for modules have existed for a while, though until recently they didn't affect the module functionality at all (you can read more about this development here). It looks like this specific prefix on these modules was supplied by the installation process for Adonis.js and given that the toolkit uses custom commands for things like building production versions of your project, it may have some custom processes for e.g. loading minimised versions of a dependency specifically for production builds.

There is not currently a standardised meaning or effect of using a prefix like this, outside of node: (which restricts imports to core packages and prevents you from accidentally downloading a malicious package by mistake, due to a typo). Some projects use this for internal purposes and it will depend on the specific project what effects the prefix may or may not have.

Shark answered 18/4, 2023 at 0:39 Comment(0)
F
1

In addition to this answer.

2021-12-12

Node.js now supports a node: protocol for built-in modules...

Previously: import * as fs from 'fs/promises'; Now: import * as fs from 'node:fs/promises';...

What are the benefits of using node: module specifiers?

It’s immediately clear that a built-in Node.js module is imported. Given how many of them there now are, that’s useful information. There is no risk of a module in node_modules overriding the built-in module. This is especially important whenever Node.js adds a new built-in module.

Source

It's also related to "namespace polluting" subjects, and one of the examples could be C++:

using namespace std;

cout << "Values:";
// std::cout << "Values:";

Related

- What does it mean global namespace would be polluted?;
- What's the problem with "using namespace std;"?.

Fala answered 11/6 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.