1.0.0 • Published 2 years ago
@parijatmishra/node-commonjs-es2021-hello-notest v1.0.0
A simple hello-world program that demonstrates:
- How to import a module using CommonJS.
- See how
src/main.ts
imports thegreet
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 thetype
value to becommonjs
, to let NodeJS know it treat '.js' files as CommonJS modules. - See how
tsconfig.json
inherits thecompilerOptions.module
variable's value ascommonjs
, so TypeScript generates JS code for exporting and importing CommonJS modules.- See
dist/lib/greet.js
anddist/main.js
for the generated module code.
- See
- See how
- 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
orpnpm
(seepackage.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
1.0.0
2 years ago