1.0.0 • Published 2 years ago

@parijatmishra/node-commonjs-es2021-hello-notest v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

A simple hello-world program that demonstrates:

  • How to import a module using CommonJS.
    • See how src/main.ts imports the greet function. (Note: it DOES NOT HAVE TO specify .js file extension when importing files containing modules.)
    • Since src/lib/greet.ts is a CommonJS module, it support NodeJS-specific __filename variable (https://nodejs.org/api/modules.html#__filename).
    • See how package.json defines the type value to be commonjs, to let NodeJS know it treat '.js' files as CommonJS modules.
    • See how tsconfig.json inherits the compilerOptions.module variable's value as commonjs, so TypeScript generates JS code for exporting and importing CommonJS modules.
      • See dist/lib/greet.js and dist/main.js for the generated module code.
  • How to define a package "binary" so that it becomes available as a command when package is installed (see package.json:bin)
  • How to define a script that can be run by npm or pnpm (see package.json:scripts)
  • How to configure Typescipt to read source files from a directory and write output files in another directory, preseving the source directory's structure (see tsconfig.json:compilerOptions)

Build

pnpm update # IMP: don't use 'install'
pnpm build # Or, pnpm run build

This will create a dist/ folder will compiled JS files.

Clean

rm -rf dist/

Run (in-place)

Run in-place:

node dist/main.js

Output:

error: missing required argument 'name'

Usage: hello [options] <name>
...

Or

node dist/main.js foobar

Output:

Hello, foobar!

Install (make available system-wide)

Make the current package available system-wide (add package pnpm global node_modules and make binaries available in $PNPM_PATH):

pnpm link --global # IMP: don't add '.' at the end

If $PNPM_HOME is available in $PATH, the myhello command can be run:

myhello foobar

Output:

Hello, foobar!

Uninstall (remove from system-wide availability)

Remove from system-wide (reverse of pnpm link):

pnpm unlink
pnpm remove -g hello # Must do