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.jsonwithnpm init - install TypeScript compiler with
npm install typescript - decide if you're keeping
package-lock.jsonin the repo (there are pros and cons) - create a
srcdir where you will keep TypeScript files - add
hello.tstosrc - add
tsconfig.jsonfile 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 thedistdirectory to.gitignoreso that compiled files are not in git a. add"declaration": trueso you have*.d.tsfiles 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
distor its contents before the compilation because otherwise if you remove or rename files insrcyou will have redundant files indistwhich are not only not needed but they can cause errors andtsc --build --cleandoesn'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 401you 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 403with "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-hellowas 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.jsonbut 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.jsonto readme, issues etc.
- if you're lazy like me just change the
- publish the package with
npm publishthis time with success - logout from npm with
npm logout - see your
~/.npmrcand 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.jsonfile 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.tsfile 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 publishignores files that are present in.gitignore(the most importantdistdirectory 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_moduleslike.gitignorebut not thedistdirectory! - 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-helloversion in the directory withhi.ts - get the same error again, this time because I forgot to change the
"main": "index.js"inpackage.jsonofnode-ts-hello - change
"main": "index.js"to"main": "dist/hello.js"inpackage.jsonofnode-ts-hello - update version
npm loginnpm publishnpm logout- go back to
hi.tsand update thenode-ts-hellodependency again - remove the
node-ts-hellowithnpm remove node-ts-hello - install
node-ts-helloagain withnpm install node-ts-hello(because it was cached and didn't really update before) - now, it works!