-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Self-referencing a package using its name #38675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can this feature be revisited now? This functionality in Node.js has been out for several major releases and it's time the Node module resolution algorithm in TypeScript was updated. |
Happy to see that this has finally been implemented in TypeScript 4.5 Beta: Make sure to set the |
[edit: this was already reported https://github.com//issues/46762] I'm on TypeScript nightly 4.7.0-dev.20220427 and can't make it self-referencing work. error package.json {
"name": "@offirmo-private/json-stable-stringify",
"type": "module",
"exports": {
".": {
"import": "./dist/src.es2021/index.js",
"require": "./dist/src.es2021.cjs/index.js"
}
}, tsconfig.json "module": "nodenext",
"moduleResolution": "nodenext", From a unit test placed inside the module (sibling to index.ts): import stable_stringify from '@offirmo-private/json-stable-stringify' Technically the target file ( Any idea of what I'm doing wrong? |
Hi, is this supposed to work in TS 4.6? I had a package that was referencing itself and I ended up with |
Hey, I'm experiencing the same issue as @Offirmo with a very similar setup |
Having a similar issue on On exports field; "exports": {
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.cjs",
"types": "./dist/types/index.d.ts"
},
"./factory": { // tried both with and without `./`
"import": "./dist/esm/factory.mjs",
"require": "./dist/cjs/factory.cjs",
"types": "./dist/types/factory.d.ts"
}
} No idea what's going wrong, and I doubt it helps that I'm new to this side of Node.js Edit: I'm running |
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_namehttps://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-selfIt 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:
The text was updated successfully, but these errors were encountered: