Skip to content

Instantly share code, notes, and snippets.

@rsp
Last active April 26, 2025 13:00
Show Gist options
  • Select an option

  • Save rsp/f7d6aec4f2bbac3de4bc3f88d871cc70 to your computer and use it in GitHub Desktop.

Select an option

Save rsp/f7d6aec4f2bbac3de4bc3f88d871cc70 to your computer and use it in GitHub Desktop.

Revisions

  1. rsp revised this gist Apr 10, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion node-ts-hello-adventures.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ node-ts-hello adventures
    =
    This is what I was really doing while creating an example from this answer on Stack Overflow:

    [deno vs ts-node : what's the difference](https://stackoverflow.com/questions/53428120/deno-vs-ts-node-whats-the-difference/55609763#55609763)
    - **[deno vs ts-node : what's the difference](https://stackoverflow.com/questions/53428120/deno-vs-ts-node-whats-the-difference/55609763#55609763)**

    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:

  2. rsp revised this gist Apr 10, 2019. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions node-ts-hello-adventures.md
    Original file line number Diff line number Diff line change
    @@ -18,27 +18,27 @@ Creating library:
    1. create a `src` dir where you will keep TypeScript files
    1. add `hello.ts` to `src`
    1. 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 the `dist` directory to `.gitignore` so that compiled files are not in git
    a. add `"declaration": true` so you have `*.d.ts` files generated
    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 the `dist` directory to `.gitignore` so that compiled files are not in git
    a. add `"declaration": true` so you have `*.d.ts` files generated
    1. add `"main": "dist/hello.js"` in `package.json` (note the "js" suffix)
    1. add `"types": "dist/hello.d.ts"` in `package.json` (note the "ts" suffix)
    1. add `"build": "tsc"` to `package.json`
    - make sure to remove the entire `dist` or its contents before the compilation because otherwise if you remove or rename files in `src` you will have redundant files in `dist` which are not only not needed but they can cause errors and `tsc --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 `dist` or its contents before the compilation because otherwise if you remove or rename files in `src` you will have redundant files in `dist` which are not only not needed but they can cause errors and `tsc --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)
    1. compile the project with `npm run build`
    1. publish the package with `npm publish`
    - when you get `npm ERR! publish Failed PUT 401` you need to login with `npm 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](https://www.theregister.co.uk/2018/07/12/npm_eslint/)
    - when you get `npm ERR! publish Failed PUT 401` you need to login with `npm 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](https://www.theregister.co.uk/2018/07/12/npm_eslint/)
    1. 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 if `ts-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 `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 if `ts-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
    1. rename your package name
    - if you're lazy like me just change the `"name"` in `package.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 in `package.json` to readme, issues etc.
    - if you're lazy like me just change the `"name"` in `package.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 in `package.json` to readme, issues etc.
    1. publish the package with `npm publish` this time with success
    1. logout from npm with `npm logout`
    1. see your `~/.npmrc` and make sure you have nothing like this left:
    - `//registry.npmjs.org/:_authToken=...`
    - `//registry.npmjs.org/:_authToken=...`

    Using the library in other project using `ts-node`

    @@ -47,14 +47,14 @@ Using the library in other project using `ts-node`
    1. install our library with `npm install node-ts-hello`
    1. optionally install ts-node with `npm install typescript ts-node` (unless it's installed globally)
    1. add `hi.ts` file that imports our library with:
    - `import { hello } from 'node-ts-hello';`
    - `import { hello } from 'node-ts-hello';`
    1. run it with `npx ts-node hi.ts` (if ts-node was installed locally) or `ts-node hi.ts` (if ts-node was installed globally)
    1. 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 important `dist` directory in this case!)
    - seriously I got this error because I forgot that `npm publish` ignores files that are present in `.gitignore` (the most important `dist` directory in this case!)
    1. 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](https://medium.com/@jdxcode/for-the-love-of-god-dont-use-npmignore-f93c08909d8d)) that contains `node_modules` like `.gitignore` but not the `dist` directory!
    1. login to npm again with `npm login`
    1. publish new version to npm: `npm publish`
    - remember to update the version first
    - remember to update the version first
    1. logout from npm with `npm logout`
    1. upgrade the `node-ts-hello` version in the directory with `hi.ts`
    1. get the same error again, this time because I forgot to change the `"main": "index.js"` in `package.json` of `node-ts-hello`
  3. rsp created this gist Apr 10, 2019.
    71 changes: 71 additions & 0 deletions node-ts-hello-adventures.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    node-ts-hello adventures
    =
    This is what I was really doing while creating an example from this answer on Stack Overflow:

    [deno vs ts-node : what's the difference](https://stackoverflow.com/questions/53428120/deno-vs-ts-node-whats-the-difference/55609763#55609763)

    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:

    1. find a name that is free on npm (no longer enough, see below)
    1. create repo on GitHub
    1. create `package.json` with `npm init`
    1. install TypeScript compiler with `npm install typescript`
    1. decide if you're keeping `package-lock.json` in the repo (there are pros and cons)
    1. create a `src` dir where you will keep TypeScript files
    1. add `hello.ts` to `src`
    1. 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 the `dist` directory to `.gitignore` so that compiled files are not in git
    a. add `"declaration": true` so you have `*.d.ts` files generated
    1. add `"main": "dist/hello.js"` in `package.json` (note the "js" suffix)
    1. add `"types": "dist/hello.d.ts"` in `package.json` (note the "ts" suffix)
    1. add `"build": "tsc"` to `package.json`
    - make sure to remove the entire `dist` or its contents before the compilation because otherwise if you remove or rename files in `src` you will have redundant files in `dist` which are not only not needed but they can cause errors and `tsc --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)
    1. compile the project with `npm run build`
    1. publish the package with `npm publish`
    - when you get `npm ERR! publish Failed PUT 401` you need to login with `npm 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](https://www.theregister.co.uk/2018/07/12/npm_eslint/)
    1. 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 if `ts-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
    1. rename your package name
    - if you're lazy like me just change the `"name"` in `package.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 in `package.json` to readme, issues etc.
    1. publish the package with `npm publish` this time with success
    1. logout from npm with `npm logout`
    1. 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`

    1. create a new directory
    1. create a `package.json` file with `npm init`
    1. install our library with `npm install node-ts-hello`
    1. optionally install ts-node with `npm install typescript ts-node` (unless it's installed globally)
    1. add `hi.ts` file that imports our library with:
    - `import { hello } from 'node-ts-hello';`
    1. run it with `npx ts-node hi.ts` (if ts-node was installed locally) or `ts-node hi.ts` (if ts-node was installed globally)
    1. 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 important `dist` directory in this case!)
    1. 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](https://medium.com/@jdxcode/for-the-love-of-god-dont-use-npmignore-f93c08909d8d)) that contains `node_modules` like `.gitignore` but not the `dist` directory!
    1. login to npm again with `npm login`
    1. publish new version to npm: `npm publish`
    - remember to update the version first
    1. logout from npm with `npm logout`
    1. upgrade the `node-ts-hello` version in the directory with `hi.ts`
    1. get the same error again, this time because I forgot to change the `"main": "index.js"` in `package.json` of `node-ts-hello`
    1. change `"main": "index.js"` to `"main": "dist/hello.js"` in `package.json` of `node-ts-hello`
    1. update version
    1. `npm login`
    1. `npm publish`
    1. `npm logout`
    1. go back to `hi.ts` and update the `node-ts-hello` dependency again
    1. remove the `node-ts-hello` with `npm remove node-ts-hello`
    1. install `node-ts-hello` again with `npm install node-ts-hello` (because it was cached and didn't really update before)
    1. now, it works!