Skip to content

Instantly share code, notes, and snippets.

@rplaurindo
Last active August 10, 2023 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rplaurindo/67a9a63a255288032f7f5192a73cd707 to your computer and use it in GitHub Desktop.
Save rplaurindo/67a9a63a255288032f7f5192a73cd707 to your computer and use it in GitHub Desktop.

Installing

Dependencies

On Ubuntu

  • curl

Specific version via APT (global)

$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

$ sudo apt install -y nodejs

See more.

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.

See more.

On Windows through NVS

Installing Via Winget

So, open the terminal and run:

$ winget install jasongin.nvs

So close and reopen the terminal.

Adding a version

$ nvs add <version>

Setting a global version

$ nvs link <version>

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.

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:

"[@<scope-name>]/<package-name>": "file:<relative path when the package.json file is>"

Note: don't use spaces after file:

So run $ npm i. The npm will create a symbolik link ($ mklink /j <path>\<destination-folder-name> <path>\<folder-to-create-a-symlink>) in node_modules/[@scope-name]/<package-name>.

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 [@<organization-name>]/<package-name>@<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

Installing

$ npm i -g typescript

Settings

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": "@<scope-name>/<package-name>"

Using EcmaScript Modules

"type": "module"

Entry point

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

"exports": {
  "./entry/point/path": "./entry/point/path/file.js"
}

tsconfig.json

In root path...

package.json

{
  "extends": "./ts/tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./es",
    "resolveJsonModule": true
  }, 
  "references": [
    {
      "path": "./ts"
    }
  ],
  "files": [
    "package.json"
  ]
}

In ts path...

`tsconfig.base.json`
```json
{
  "compilerOptions": {
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "strict": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "inlineSources": true,
    "target": "ES2020",
    "moduleResolution": "node",
  }
}

tsconfig.esnext-module.json

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

tsconfig.esnext-module.json

{
  "extends": "./tsconfig.esnext-module.json",
  "compilerOptions": {
    "composite": true,
    "outDir": "../es"
  },
  "references": [
    {
      "path": "./src"
    }
  ]
}

In ts/src path... tsconfig.json

{
  "extends": "../tsconfig.esnext-module.json",
  "compilerOptions": {
    "composite": true,
    "outDir": "../../es/src"
  },
  "references": [
    {
      "path": "./tests"
    }
  ]
}

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

Notes:

  • The src should have a tsconfig.json file with compilerOptions.composite set to true. This will generate a tsconfig.tsbuildinfo file and $ tsc -b will generate a warning if another tsconfig.json use references and point to this same src folder too;
  • With references we don't need to repeat the contents of the include, with only extends it's not possible.
  • The rootDir is used to inform where the root path starts;
  • On use import with relative path, you must inform the .js extension of file;
  • The resolveJsonModule is used to include .json files;
  • The declaration is used to generate .d.ts files, and the IntelliSense of the IDE recognize and autocomplete references.

Building the structure

Inside a folder that contains a tsconfig.json file, run:

$ tsc -b

Note: for just emit declaration files (.d.ts), set declaration as true, emitDeclarationOnly as true and isolatedModules as true. Note yet that moduleResolution and target isn't necessary anymore in this case.

Publishing a public package

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

$ npm login

So put the Username, Password and Email.

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.

  • Go to menu View > Run;
  • Click on Run and Debug;
  • Choice Node.js.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment