Skip to content

Commit 2ac9eb6

Browse files
authored
Add notes about node usage of name field (#10)
* Add notes about `node` usage of `name` field * Address feedback * Use Node.js instead of "node". clarify Node does not require name. * Added notes clarifying import behavior for a multiply installed package * )
1 parent ae21140 commit 2ac9eb6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

research/name.md

+13
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,25 @@ Analysis of the `name` property
4141
- One scope must only ever point to one registry.
4242
- One registry can host multiple scopes.
4343

44+
### Node.js
45+
46+
- Node.js does not require a `name` field.
47+
- Importing a package from another package is based on the path it is installed at in `node_modules`, NOT the `name` field in `package.json`.
48+
- If a package is named `foo` but is installed or symlinked from `node_modules/bar`, it is found as `import('bar')`
49+
- If a package is installed under two paths (e.g. with `npm install cowsay1@npm:cowsay cowsay2@npm:cowsay`) then importing either will result in *different* module objects. If the package is instead *linked* from two paths in `node_modules` (e.g. with `pnpm install cowsay1@npm:cowsay cowsay2@npm:cowsay`), then importing either will result in the *same* module object.
50+
- The `name` field *can* be used within a package to refer to the same package<sup>[4]</sup>. That is, if you have a `package.json` file with `"name":"foo"`, then inside that directory, `import("foo")` or `import("foo/bar")` will look at that directory before consulting `node_modules`.
51+
- In order to enable self-imports by name, `package.json` must define an `exports` field.
52+
- Neither a `main` field, nor a package-relative subpath will suffice instead of `exports`. Self-imports might *seem* to work if the package happens to be installed into `node_modules`.
53+
- The scope is part of the package name for purposes of self-reference. In a package with `"name": "@scope/foo"`, you must `import('@scope/foo')`, not e.g. `import('foo')`.
54+
4455
## Sources
4556

4657
1. [npm `name` field documentation][1]
4758
2. [npm _Scope_ concept documentation][2]
4859
3. [npm `install` command documentation][3]
60+
4. [node `packages` documentation][4]
4961

5062
[1]: <https://docs.npmjs.com/cli/configuring-npm/package-json#name>
5163
[2]: <https://docs.npmjs.com/cli/using-npm/scope>
5264
[3]: <https://docs.npmjs.com/cli/commands/npm-install>
65+
[4]: <https://nodejs.org/api/packages.html#packages_self_referencing_a_package_using_its_name>

0 commit comments

Comments
 (0)