Node must be install prior to these steps. Check online for installation steps for your choice of OS.
Open Powershell in Adminstrator mode
-
Run command:
-
Set-ExecutionPolicy RemoteSigned
-
npm install -g typescript
- mkdir <project_folder_name>
- cd <project_folder_name>
- mkdir src //for source ts files
- npm init -y //for initilizing package.json
- tsc -init //for initilizing typescript configuration
- Open package.json and add
-
"type": "module",
-
"bin": { "<package_name>": "./bin/<output_js_file>.js" }//executable path for js file for NPX
- Open tsconfig.json and update the following parameters
- "target": "ES2022",
- "module": "NodeNext",
- "moduleResolution": "NodeNext",
- "outDir": "./bin",//this will output all the transpiled JS files in outdir folder.
- Create a new TS file under src folder.
- Add the following line at tha start of your TS file, if you are intended to use the packege as a self executed NPX application.
- #! /usr/bin/env node
- Start coding your application
- npm i inquirer
- npm i @types/inquirer
- npm i chalk
- npm i @types/chalk
- Install npx “npm install -g npx”
- Create a new account on https://www.npmjs.com/
- Create your application using “npm init -y”.
- Configure TSC Config using “tsc –init”
- Open package.json and change the following parameters
- name:.
- Add a new parameter "type" and set its value to "module".
- Add a new parameter "bin": {"module_name":"bin"}. //.js
- Add required Node dependencies in Dependency and DevDependency sections.
- Create a new folder named bin in project root directory.
- Open TSConfig:
-
set "target": "ES2022".
-
set "module": "NodeNext".
-
set "moduleResolution": "NodeNext".
-
set "outDir": "./bin",//This will automatically create all the js filed in bin folder.
- Create the required TS files and wrote the code. Besure to add the following line in the start of every ts file “#! /usr/bin/env node”.
- Compile the code using tsc command.
- Install the generated module in npm global directory using command "npm i -g"
- Test the project using command "npx ".
- After successfull execution prepare it to move it to NPMJS environment to be available to everyone using npx command.
-
Login to NPMJS remotely using "npm login".
-
After a successful login , execute "npm publish". This will upload the newly built Node Module to NPMJS environment using your provided credentials.
-
Try testing is using command using "npx <module_name>".
-