Description
Search Terms
Self reference package name node exports
Suggestion
The Node.js documentation for ECMAScript Modules specifies that you should be able to self-reference the package that the module is in by the package's name:
https://nodejs.org/api/esm.html#esm_self_referencing_a_package_using_its_name
https://nodejs.org/api/packages.html#packages_self_referencing_a_package_using_its_name
I know this feature is still considered experimental by Node, but I think TypeScript should support this behavior in their Node-style module resolution algorithm.
No longer experimental as of Node.js v13.6.0
Use Cases
This is helpful for writing tests that reference the package instead of directly to the files.
That way you can also test that your package.json is in the correct format to allow your package to be consumed by ES modules and CommonJS code.
There is a work-around available that involves symlinking your project into the project's own node_modules.
You can do this by executing:
npm link && npm link foo
(where foo
is the name of your package)
OR
You can use a library like install-self
: https://www.npmjs.com/package/install-self
It would be nice if you didn't have to perform these extra steps or install extra dev dependencies.
Examples
I ran into this issue when I was trying to do the following:
https://github.com/BTOdell/unknown/blob/719727457ad1dca745789d05bcbb1ba98c7a78e4/test/unknown.spec.ts
Even though TypeScript fails to compile, it still emits the correct JavaScript and I can run it on Node.js v14 without errors.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.