Skip to content

Instantly share code, notes, and snippets.

@rplaurindo
Last active December 1, 2024 20:23
Show Gist options
  • Save rplaurindo/67a9a63a255288032f7f5192a73cd707 to your computer and use it in GitHub Desktop.
Save rplaurindo/67a9a63a255288032f7f5192a73cd707 to your computer and use it in GitHub Desktop.

Using

Installing dependencies

Inside the folder that contains the package.json, run:

$ npm i

Then the package-lock.json file will be created.

Note: to refer to a local file, use this syntax:

"[@<scopename>/]<packagename>": "file:<relative path when the package.json file is>"

Note: don't use spaces after file: setting.

Run $ npm i, so the npm will create a symbolik link at node_modules/[@scopename/]<packagename>.

See more.

Updating The NPM Package

$ npm i -g npm@latest

Authenticating with npm

$ npm login

So put the Username, Password and Email.

Publishing a public package

Inside the folder that contains the package.json, run:

$ npm publish --access public

Note.:

  • You must have been previously authenticated;
  • The --access option is needed just in the first time.

Deprecating a package

$ npm deprecate [@<scopename>/]<packagename>@<version> '<message>'

See more.

Settings

Create a package.json inside your HOME folder to a default configuration. For that, run:

$ npm init

See more.

Setting proxy

$ npm config set proxy http://<user>:<password>@<domain>:<port>

$ npm config set https-proxy http://<user>:<password>@<domain>:<port>

See more.

On Ubuntu run:

$ source ~/.npmrc

Versioning Through FNM

On Windows

Install the Winget.

FNM

After install the winget, open the terminal and run:

$ winget install Schniz.fnm

Using on PowerShell

Open the PowerShell, type $ notepad $profile, so paste the line below.

fnm env --use-on-cd --version-file-strategy=recursive --shell powershell | Out-String | Invoke-Expression

Note:

  • --use-on-cd this option able the fnm changes the version when entering in a directory, if a dotfile (.node-version) is present.
  • --version-file-strategy=recursive this option makes $ fnm use and $ fnm install take parent directories into account when looking for a version file when no argument was given.

See more here.

The command $ notepad $profile only works on PowerShell. This command open the profile file (Microsoft.PowerShell_profile.ps1) of the PowerShell. This file is encountered at C:\Users\<username>\[OneDrive]\Documents\PowerShell.

To create an alias command to an application, put Set-Alias <alias> 'path\of\executable' in the PowerShell profile file.

See more here and here.

Using

The fnm commands can be seen here.

After install a version, to define it as the default version, run

$ fnm default --version-file-strategy=recursive <version>

See more about versioners.

NVS

So, open the terminal and run:

$ winget install jasongin.nvs

So close and reopen the terminal.

Adding a version

$ nvs add <version>

Setting up a global version

$ nvs link <version>

Automatic switching per directory

See.

Optional

$ echo alias node=</user/directory>/AppData/Local/nvs/node/<version>/x64/node >> ~/.bash_profile

$ echo alias npm=</user/directory>/AppData/Local/nvs/node/<version>/x64/npm >> ~/.bash_profile

$ source ~/.bash_profile

List Installed Versions

$ nvs ls

Removes a version

$ nvs rm <version>

See more.

On Ubuntu

Dependencies

  • curl

Through NVM (Node Version Manager)

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

$ source ~/.bashrc

Obs.: by default the script installs at ~/.nvm and define the command in ~/.bashrc, but you can change them.

Installing a Node.js Version

$ nvm install --lts

See more.

Installing

$ npm i -g typescript

Setting up

In devDependencies of package.json, make like below

"devDependencies": {
  "@types/node": "semver statement",
  "typescript": "semver statement"
}

Set a Scope For the Name of Package

"name": "@<scopename>/<packagename>"

Using EcmaScript Modules

Just save the files with .mts.

Note: if the library have a main file at the root path (index.mts), use: "main": "index.mjs".

Entry Points

Use exports to set a entry point different of the implicit index.js in root path, like below

"exports": {
    "./entry/point/path": {
        "types": "./entry/point/path/filename.d.mts",
        "default": "./entry/point/path/filename.mjs"
    }
}

tsconfig.json

At the root path...

package.json

{
  "extends": "./ts/tsconfig.nodeNext-module",
  "compilerOptions": {
    "outDir": "./es"
  }, 
  "references": [
    {
      "path": "./ts"
    }
  ],
  "files": [
    "package.json"
  ]
}

At the ts path...

tsconfig.nodeNext-module.json

{
  "extends": "./tsconfig.base",
  "compilerOptions": {
    "module": "NodeNext"
  }
}

tsconfig.base.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "strict": true,
    "inlineSources": true,
    "sourceMap": true,
    "target": "ES2022",
    "removeComments": true,
    "resolveJsonModule": true
  }
}

tsconfig.json

{
  "extends": "../tsconfig.nodeNext-module",
  "compilerOptions": {
    "composite": true,
    "outDir": "../es",
    "tsBuildInfoFile": "../es/ts-tsconfig.tsbuildinfo",
    "declaration": true,
    "declarationDir": "../es/types"
  }
}

This is the basic structure to build what is in ts path in es. Use references instead put the src/**/* at include.

Notes:

  • The compilerOptions.tsBuildInfoFile can be used to resolve conflict when two or more tsconfig file with "compilerOptions.composite: true points to the same path. This setting informs to compiler the relative path and the tsbuildinfo filename;
  • The compilerOptions.rootDir can be used to inform to the compiler that the root directory is in a parent path;
  • When use import with a relative path, you must to inform the .mjsextension of the file explicitly.

Building the structure

Inside the root path of the project, a folder (that contains a tsconfig.json file), run:

$ tsc -b

Note: for just emit declaration files (.d.ts), set compilerOptions.emitDeclarationOnly as true.

Publishing a Public Package

You need to authenticate with npm before, so run...

$ npm login

So put the Username or Email, and the Password.

Inside the folder that contains the built code with package.json, run:

$ npm publish --access public

Note that the --access flag is needed just in the first time, but can be used everytime.

Debugging The Source Code on CLI

Starting The Inspector

To start the inspector, run:

$ node inspect <filename>.mjs

By default the inspector starts at the first line of the source code, to start at the first debugger instruction setting up the environment variable of the OS NODE_INSPECT_RESUME_ON_START as 1.

Going to The Next Debugger Instruction

To go to the next debugger instruction, and pause at this breakpoint, type cont or c and press Enter, to instruct the inspector to continue...

debug> c

So the Node.js will stop only where the next debugger instruction or another breakpoint was defined.

Note: inside a loop or a recursive method, the continue instruction will stop at the breakpoint within this same loop, while that isn't resolved.

Inspecting The Value of a Variable or Method

To go to the next debugger instruction, type repl and press Enter, so the output will be like below...

debug> repl
Press Ctrl+C to leave debug repl

See more here, here, here, or here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment