How to create an npm CLI package hosted on Gitlab - node.js

My goals is to create a CLI script to install globally (npm i -g #born05/cli).
My package.json:
{
"name": "#born05/cli",
"version": "1.0.7",
"description": "The Born05 handy-dandy development Command Line Interface",
"main": "lib/cli.js",
"author": {
"name": "Niels Wijers",
"email": ""
},
"bin": {
"born05": "lib/cli.js"
},
"engines": {
"node": ">=12.0.0"
},
"license": "ISC",
"publishConfig": {
"#born05:registry": "https://gitlab.born05.com/api/v4/projects/147/packages/npm/"
},
"bundleDependencies": false,
"deprecated": false,
"dependencies": {
"findit": "^2.0.0",
"inquirer": "^7.3.3",
"js-yaml": "^4.0.0",
"tar": "^6.1.0"
}
}
The script is created and tested locally and it works, even after npm link the script is working when typing born05 in my terminal. But after publishing and installing it globally it fails to link the cli.js to the bin dir but the package is installed successfully.
My .gitlab-ci.yml
stages:
- publish
publish:
stage: publish
image: node:15.0-slim
before_script:
- npm install
- |
{
echo "#born05:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
echo "${CI_API_V4_URL#https?}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=\${CI_JOB_TOKEN}"
} | tee --append .npmrc
script:
- npm publish
My .npmrc
#born05:registry = "https://gitlab.born05.com/api/v4/packages/npm/"
//gitlab.born05.com/api/v4/packages/npm/:_authToken = (protected)
I hope anyone can help me.

I experienced the same issue, and my solution was to export the parameter manually:
- npm install
- |
{
echo "#born05:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
echo "${CI_API_V4_URL#https?}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=\${CI_JOB_TOKEN}"
} | tee --append .npmrc
- export NPM_CONFIG_REGISTRY=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/

Related

Publishing images to be included in README.md on NPM

In this project the README.md displays the logo/pnglogo.png logo at the top of the README.md file.
We'd like the same logo to display on NPM. So we copy it into the dist/fs-validator directory where the publish files are located. This is the npm script (From package.json):
"p": "cp README.md ./projects/fs-validator/ && npm run bp && npm run b && cp -r logo ./dist/fs-validator/ && cd dist/fs-validator/ && npm publish",
If I understand correctly, this should publish the logo directory along with the other files. However the logo is not showing up at the top of the README.md within NPM. How can I fix that?
I moved the logo to the root of the repository and changed the README.md to include it like this:
![Validator](pnglogo.png)
As that works for publishing with this repository:
https://github.com/fireflysemantics/slice
However I'm still not getting any success.
Figured it out. The package.json that I was publishing did not have the Github repository fields included.
{
"name": "#fireflysemantics/validator",
"version": "0.0.9",
"peerDependencies": {
"#fireflysemantics/is": "*",
"#fireflysemantics/validatorts": "*"
},
"dependencies": {
"tslib": "^2.2.0"
}
}
Apparently these are needed in order for NPM to know where the image is located.
I changed package.json to incoulde the repository data so that it now looks like this:
{
"name": "#fireflysemantics/validator",
"version": "0.0.11",
"license": "MIT",
"bugs": {
"url": "https://github.com/fireflysemantics/validator/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fireflysemantics/validator.git"
},
"keywords": [
"Validation",
"Javascript",
"Typescript",
"Angular Package Format"
],
"peerDependencies": {
"#fireflysemantics/is": "*",
"#fireflysemantics/validatorts": "*"
},
"dependencies": {
"tslib": "^2.2.0"
}
}
And now the logo shows up.

node/npm saying "Missing script <scriptname>" even though it exists in current directory

The package.json is:
{
"scripts": {
"test": "find ./js/tests -name '*.test.js' | xargs mocha -R spec",
"start": "node ./scores.js",
"run": "node ./scores.js"
},
"dependencies": {
"aws-sdk": "^2.734.0",
"body-parser": "^1.19.0",
..
"dotenv": "^8.2.0"
},
"name": "scores",
"version": "1.0.0",
"main": "",
"repository": {
"type": "git",
"url": "something"
}
}
npm install has been done and the package-lock.json created. Why are either npm run or npm run-script working?
$npm run-script ./scores.js
npm ERR! missing script: ./scores.js
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/steve/.npm/_logs/2021-03-01T23_01_56_745Z-debug.log
15:01:56/scores $ls -l scores.js
-rw-r--r-- 1 steve staff 21417 Feb 25 06:24 scores.js
I have identically structured package.json and scripts in other sibling directories that do work e.g. the following works via cd ../keys_server; npm run keys_server
$cat ../keys_server/package.json
{
"scripts": {
"test": "find ./js/tests -name '*.test.js' | xargs mocha -R spec",
"keys_server": "node ./keys_server.js",
"start": "node ./keys_server.js"
},
"dependencies": {
"aws-sdk": "^2.734.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
..
"dotenv": "^8.2.0"
},
"name": "keys_server",
"version": "1.0.0",
"main": "keys_server.js",
"repository": {
"type": "git",
"url": "something"
},
Update #pkumar noticed that the main entry was left empty. I now updated it to :
"main": "scores.js",
and then tried both
npm run-script ./scores.js and npm run scores and npm run scores.js. However none of them work:
$npm run scores
npm ERR! missing script: scores
The 'main' field is empty in package.json. try adding scores.js there and see if it works
The 'main' field is supposed to be the entry point of the server: https://docs.npmjs.com/cli/v7/configuring-npm/package-json
EDIT: The actual reason it's not working is because "scores" had not been defined under the scripts section. After adding that definition it should work
"scripts": {
..
"scores": "node ./scores.js"
}

custom npm package not found in my service

There is a similar post here:
My custom NPM Package is not found,
but that did not resolve my issue.
Could not find a declaration file for module '#dc_microurb/common'.
'/Users//Projects/ticketing/auth/node_modules/#dc_microurb/common/build/index.js'
implicitly has an 'any' type. Try npm install #types/dc_microurb__common if it exists or add a new declaration
(.d.ts) file containing `declare module '#dc_microurb/common';
There is no #types/dc_microurb__common and I am unclear as to why it is suggesting to create a new .d.ts file, when that happens automatically during the build process.
This is the package.json file inside my published package:
{
"name": "#dc_microurb/common",
"version": "1.0.5",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"./build/**/*"
],
"scripts": {
"clean": "del ./build",
"build": "npm run clean && tsc",
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"del-cli": "^3.0.1",
"typescript": "^4.0.5"
},
"dependencies": {
"#types/cookie-session": "^2.0.41",
"#types/express": "^4.17.9",
"#types/jsonwebtoken": "^8.5.0",
"cookie-session": "^1.4.0",
"express": "^4.17.1",
"express-validator": "^6.6.1",
"jsonwebtoken": "^8.5.1"
}
}
Anybody with experience in publishing packages on npm where TypeScript is involved? I am unclear as to why this package is not being found by my auth service when it is successfully installed inside that service.
Did I mess up in the way I am exporting inside my common/src/index.ts file?
export * from './errors/bad-request-error';
export * from './errors/custom-errors';
export * from './errors/database-connection-error';
export * from './errors/not-authorized-error';
export * from './errors/not-found-error';
export * from './errors/request-validation-error';
export * from './middlewares/current-user';
export * from './middlewares/error-handler';
export * from './middlewares/require-auth';
export * from './middlewares/validate-request';
Does it all have to be inside a module.exports instead?
So after reviewing a few blogs on Medium on how this is done, I noticed a couple of things.
Inside my tsconfig.js, this was missing:
/* Advanced Options /
"forceConsistentCasingInFileNames": true / Disallow inconsistencies */
So I manually added it in, secondly, I noticed something I meant to remove earlier but forgot. So instead of this:
{
"name": "#dc_microurb/common",
"version": "1.0.5",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"./build/**/*"
],
I needed to have it like this:
{
"name": "#dc_microurb/common",
"version": "1.0.6",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"build/**/*"
],
After making those couple of corrections, now my package is available to my auth service.

Unable to run a node.js file with #babel/preset-env

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks
followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}
Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

Install node_module from npm but it doesn't install its own node_modules - package.json

I just created a node_module with a dependency with 'validator'.
But when I install it from npm, it doesn't install the 'validator' module in its own node_module directory. I don't understand why, but it's the first time I create my own node module.
package.json:
{
"name": "validator-extended",
"description": "String validation and sanitization based on validator.js",
"version": "1.1.0",
"homepage": "https://github.com/Ayolan/validator-extended",
"keywords": [
"validator",
"validation",
"validate",
"sanitization",
"sanitize",
"sanitisation",
"sanitise",
"assert",
"extend",
"extended"
],
"author": "Vadorequest <https://github.com/Vadorequest>",
"main": "app.js",
"bugs": {
"url": "https://github.com/Ayolan/validator-extended/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/Ayolan/validator-extended.git"
},
"engines": {
"node": ">= 0.8"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/Ayolan/validator-extended/raw/master/LICENSE"
}
],
"dependencies": {
"validator": "~3.1"
}
}
I also have a .npmignore file with this:
########################
# node.js / npm
########################
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
node_modules
npm-debug.log
########################
# misc / editors
########################
*~
*#
.DS_STORE
.netbeans
nbproject
.idea
Should I push the node_modules/ directory into my git repository?
You should remove your existing package.json and run
npm init
to properly initialise your project. Once that's done, add validator to the dependencies. This should allow npm install to work properly and install your dependencies. Don't commit your node_modules folder.

Resources