1.0.0 • Published 3 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.tsimports thegreetfunction. (Note: it DOES NOT HAVE TO specify.jsfile extension when importing files containing modules.) - Since
src/lib/greet.tsis a CommonJS module, it support NodeJS-specific__filenamevariable (https://nodejs.org/api/modules.html#__filename). - See how
package.jsondefines thetypevalue to becommonjs, to let NodeJS know it treat '.js' files as CommonJS modules. - See how
tsconfig.jsoninherits thecompilerOptions.modulevariable's value ascommonjs, so TypeScript generates JS code for exporting and importing CommonJS modules.- See
dist/lib/greet.jsanddist/main.jsfor 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
npmorpnpm(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 buildThis will create a dist/ folder will compiled JS files.
Clean
rm -rf dist/Run (in-place)
Run in-place:
node dist/main.jsOutput:
error: missing required argument 'name'
Usage: hello [options] <name>
...Or
node dist/main.js foobarOutput:
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 endIf $PNPM_HOME is available in $PATH, the myhello command can be run:
myhello foobarOutput:
Hello, foobar!Uninstall (remove from system-wide availability)
Remove from system-wide (reverse of pnpm link):
pnpm unlink
pnpm remove -g hello # Must do1.0.0
3 years ago