Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ':' colon do in JavaScript's import?

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.

like image 893
myckhel Avatar asked Nov 24 '25 08:11

myckhel


2 Answers

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.

like image 132
Dakeyras Avatar answered Nov 25 '25 23:11

Dakeyras


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;"?.

like image 43
Faither Avatar answered Nov 25 '25 23:11

Faither



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!