This is what I was really doing while creating an example from this answer on Stack Overflow:
It turned out that I made several stupid mistakes and wasted a lot more time than it might look like from reading the answer. I eventually removed from the answer not so much out of embarrassment but because I didn't want to be accused that I am complicating the process on purpose to make Deno look better in comparison. The truth is that below ius exactly what I did while trying to create and publish a new module on npm written in TypeScript and use it with ts-node
later:
https://github.com/rsp/node-ts-hello
Creating library:
- find a name that is free on npm (no longer enough, see below)
- create repo on GitHub
- create
package.json
withnpm init
- install TypeScript compiler with
npm install typescript
- decide if you're keeping
package-lock.json
in the repo (there are pros and cons) - create a
src
dir where you will keep TypeScript files - add
hello.ts
tosrc
- add
tsconfig.json
file and make sure to: a. add"src/**/*"
to"include"
a. add dependencies and your own types to"paths"
a. add"outDir": "dist"
to put the JS files in a known place a. add thedist
directory to.gitignore
so that compiled files are not in git a. add"declaration": true
so you have*.d.ts
files generated - add
"main": "dist/hello.js"
inpackage.json
(note the "js" suffix) - add
"types": "dist/hello.d.ts"
inpackage.json
(note the "ts" suffix) - add
"build": "tsc"
topackage.json
- make sure to remove the entire
dist
or its contents before the compilation because otherwise if you remove or rename files insrc
you will have redundant files indist
which are not only not needed but they can cause errors andtsc --build --clean
doesn't remove them for me (it caused me problems more than once so that's why I'm mentioning it here but the solution is out of the scope of this answer)
- make sure to remove the entire
- compile the project with
npm run build
- publish the package with
npm publish
- when you get
npm ERR! publish Failed PUT 401
you need to login withnpm login
- I keep logged out because of the history of malware stealing npm credentials in the past - see: Now Pushing Malware: NPM package dev logins slurped by hacked tool popular with coders
- when you get
- run
npm publish
- after logging in I now got
npm ERR! publish Failed PUT 403
with "Package name too similar to existing packages; try renaming your package to '@rsp/ts-hello' and publishing with 'npm publish --access=public' instead" which is interesting because I checked ifts-hello
was free on npm and I got "package 'ts-hello' not found" but now apparently this is not enough so I need to rename it! both the package and the repo, after I already created it. I'm adding this info here because I seriously got this problem right now which I didn't expect at all so to keep it real I'm adding the steps here
- after logging in I now got
- rename your package name
- if you're lazy like me just change the
"name"
inpackage.json
but if you're more serious that also rename the GitHub repo for consistency and update the remotes in your local git directory, plus upate all of the links inpackage.json
to readme, issues etc.
- if you're lazy like me just change the
- publish the package with
npm publish
this time with success - logout from npm with
npm logout
- see your
~/.npmrc
and make sure you have nothing like this left://registry.npmjs.org/:_authToken=...
Using the library in other project using ts-node
- create a new directory
- create a
package.json
file withnpm init
- install our library with
npm install node-ts-hello
- optionally install ts-node with
npm install typescript ts-node
(unless it's installed globally) - add
hi.ts
file that imports our library with:import { hello } from 'node-ts-hello';
- run it with
npx ts-node hi.ts
(if ts-node was installed locally) orts-node hi.ts
(if ts-node was installed globally) - get
error TS2307: Cannot find module 'node-ts-hello'.
- seriously I got this error because I forgot that
npm publish
ignores files that are present in.gitignore
(the most importantdist
directory in this case!)
- seriously I got this error because I forgot that
- go back to your library repo and add a file
.npmignore
(but watch out for gotchas, see: For the love of god, don’t use .npmignore) that containsnode_modules
like.gitignore
but not thedist
directory! - login to npm again with
npm login
- publish new version to npm:
npm publish
- remember to update the version first
- logout from npm with
npm logout
- upgrade the
node-ts-hello
version in the directory withhi.ts
- get the same error again, this time because I forgot to change the
"main": "index.js"
inpackage.json
ofnode-ts-hello
- change
"main": "index.js"
to"main": "dist/hello.js"
inpackage.json
ofnode-ts-hello
- update version
npm login
npm publish
npm logout
- go back to
hi.ts
and update thenode-ts-hello
dependency again - remove the
node-ts-hello
withnpm remove node-ts-hello
- install
node-ts-hello
again withnpm install node-ts-hello
(because it was cached and didn't really update before) - now, it works!