liveBook · Manning

Bare specifier

As explained in Secrets of the JavaScript Ninja, Third Edition

A bare specifier is a module import specifier expressed as a package name rather than a relative or absolute file path.

A bare specifier names a module without giving a file path or URL. In Node.js, it is typically a package name; in a browser, an import map can map a bare specifier to a URL. Whether a runtime recognizes a specifier depends on that runtime. section 3.2.2 section 13.1.2

For Node.js package imports, resolution proceeds from the importing module’s location:

  • Search upward for the closest node_modules directory containing the named package. section 3.2.2
  • Check the package.json exports field for a matching entry point. If exports is absent, use an imported subpath relative to the package directory, or the main entry for a package-root import. section 3.2.2 section 13.1.2
  • Package-manager Plug’n’Play modes can replace the node_modules search with resolution from a central cache. section 3.2.2
Listing 13.12 Importing from a package
import { lazyGetRandomTurtle } from "entry-point-example"; #A
import { getRandomTurtle } from "entry-point-example/ninjas/turtles"; #B

console.log(getRandomTurtle());
console.log(await lazyGetRandomTurtle());

Compare a package-root import with an import naming a package subpath. section 13.1.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines bare specifiers and traces Node.js lookup and entry-point selection, while deferring detailed exports mapping rules.
Examples
Uses imports from js-ninja-3e and entry-point-example, including a package subpath and a browser import-map alias.
What you can do after
Apply it by importing package roots or subpaths using Node.js entry points, or by mapping browser imports with an import map.
How it connects
Connects module specifiers to Node.js, node_modules, package.json, exports, main, and browser import maps.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.