Suppose you’re developing an addition library called my-lib and trying it from an adjacent app. Initially you import add from the library’s source directory. Press F12 in the editor and you see how the addition works. Later, to try the package the way someone else would install it, you change the import to the package name. The addition still works, but F12 may now open a file with just one function declaration. What did each import let the editor find?
Local source development: how do I import from the project next door?
Start with two files under the same empty directory. This example uses Node.js 22, npm, and TypeScript 5.7.3. Both projects compile to Node.js CommonJS modules.
my-lib/src/index.ts
app/src/demo.ts
Write this function in my-lib/src/index.ts. export lets other files import it, and : number says both parameters and the return value are numbers.
export function add(a: number, b: number): number {
return a + b;
}
In app/src/demo.ts, import the file you just wrote directly:
import { add } from "../../my-lib/src/index";
console.log(add(1, 2));
Starting in app/src/, where demo.ts lives, the first .. returns to app/ and the second reaches the projects’ common parent. The path then enters my-lib/src/. Under this configuration, TypeScript finds index.ts. Open the common parent in VS Code and press F12 (“Go to Definition”) on add at the call site. You’ll see return a + b. Change the first argument to "1" and the editor can flag a type error directly from the source.
To run this local test, restore the argument to a number and run these two lines in app/. npm exec invokes the specified compiler version. --rootDir .. selects the common parent, and --outDir build-local preserves the files’ relative locations in the generated JavaScript under build-local/.
npm exec --yes --package=typescript@5.7.3 -- tsc --module Node16 --strict --rootDir .. --outDir build-local src/demo.ts
node build-local/app/src/demo.js
The terminal prints 3. Open build-local/my-lib/src/index.js to see that the adjacent library’s addition function was compiled too. At this point the library doesn’t even need a package.json. But when other people install your package, they receive a package called my-lib. They may have no copy of your source directory beside their project.
After importing by package name, what do main and types point to?
Keep the add implementation and make an installable package for it. In my-lib/package.json, write:
{
"name": "my-lib",
"version": "1.0.0",
"type": "commonjs",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["dist"],
"scripts": { "build": "tsc" },
"devDependencies": { "typescript": "5.7.3" }
}
Both entries are relative to the library’s own package.json. For a single-entry package like this, Node.js uses main to find the JavaScript that calculates 3 when executing add(1, 2). When TypeScript checks the same call, it uses types to find a declaration file describing the parameters and return value. Nick Morgan’s addition package uses this pair of entries too. To make them work, we need to generate both files.12
Tell the TypeScript compiler, tsc, how to process the source in my-lib/tsconfig.json:
{
"compilerOptions": {
"module": "Node16",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"strict": true
},
"include": ["src"]
}
Run these three commands in my-lib/. The first installs the compiler, the second compiles src/ into dist/, and the third previews the files other people will receive when they install the published package.3
npm install
npm run build
npm pack --dry-run
Look for dist/index.js and dist/index.d.ts in the listing. The latter is generated by declaration. Open it and you’ll see just the following line. declare describes how callers can use the function. The implementation is in the adjacent .js file.1
export declare function add(a: number, b: number): number;
Now run npm init -y and npm install ../my-lib in app/, then replace the first line of demo.ts with:
import { add } from "my-lib";
The ../my-lib in the install command tells npm to fetch the package from the adjacent directory. The "my-lib" in the code lets the caller use the installed package. For how npm remembers the location on a later install, see the local dependency example in the publishing guide.
Restore the arguments to 1, 2. In app/, use the compiler you installed in the library directory to check and run the caller:
../my-lib/node_modules/.bin/tsc --module Node16 --strict --outDir build src/demo.ts
node build/demo.js
The terminal prints 3. If you change add(1, 2) to add("1", 2) and rerun the first command, the compiler points out that you cannot pass a string to a number parameter. There’s no src in the application’s import anymore, yet it still knows what add accepts.
Local source development: how can F12 open the .ts implementation after importing by name?
Paul Ehikhuemen’s tutorial gives another way to try a package across two projects. Run npm link in the library, then npm link my-lib in the app, so the app’s installed package location links to the library’s development directory. The application still imports by package name. We can continue here with the local dependency installed in the previous section.4
After importing by name, VS Code can find the declaration of add, but F12 may show only export declare function add(…). Viktor Hedefalk described the problem while developing two packages together with npm link: “I want to go into the source file to be able to edit in the dep in parallel.” He had tried pointing types straight at the source but worried that the published package would omit src. Look again at the package manifest: if types becomes ./src/index.ts, that path must appear in the packaged files for other users to find it after installation.5
Keep "types": "./dist/index.d.ts", add "declarationMap": true under compilerOptions in the library’s tsconfig.json, and run npm run build again. Now dist/ also contains index.d.ts.map. This declaration map records which source file corresponds to a location in the declaration. Open it and you’ll see ../src/index.ts in sources. Back in demo.ts, press F12 on add at the call site. The editor can now follow the map to the editable line of addition code.6
Now change the implementation and try the call again. An app importing by name still loads dist/index.js, so rebuild the library after editing the source. During local development, run npm run build -- --watch in another terminal to keep compiling. To let people installing the released package reach the source with F12 too, set files to ["dist", "src"]. Use npm pack --dry-run to confirm the declarations, maps, and the source those maps refer to all ship in the package.63
Footnotes
-
Microsoft. (n.d.). Publishing, “Including declarations in your npm package”. TypeScript Handbook. https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#including-declarations-in-your-npm-package ↩ ↩2
-
Morgan, N. (2023, November 1). Creating an npm package with TypeScript, “5. Prepare for Publishing” [Blog post]. Medium. https://medium.com/@the_nick_morgan/creating-an-npm-package-with-typescript-c38b97a793cf ↩
-
npm. (n.d.). npm pack, “dry-run”. npm CLI v10 Documentation. https://docs.npmjs.com/cli/v10/commands/npm-pack/#dry-run ↩ ↩2
-
Ehikhuemen, P. (2023, June 12). TypeScript NPM Package Publishing: A Beginner’s Guide, “Publishing your NPM package locally” [Blog post]. Medium. https://pauloe-me.medium.com/typescript-npm-package-publishing-a-beginners-guide-40b95908e69c ↩
-
Hedefalk, V. (2018, February 23). What to put in package.json types field for typescript based libs, paragraph beginning “I want to go into the source file” [Question]. Stack Overflow. https://stackoverflow.com/questions/48952313/what-to-put-in-package-json-types-field-for-typescript-based-libs#question ↩
-
Microsoft. (n.d.). declarationMap. TSConfig Reference. https://www.typescriptlang.org/tsconfig/declarationMap.html ↩ ↩2