Publish angular2 module / component as npm package - node.js

I would like to create my library in npm of Angular2 components (my generic grid, my generic buttons, etc.) and modules (my clients in grid module) but I can't find any working example online (I did find a few though, but it never works...
index.js :
export { PortalGridComponent } from './src/portal-grid/portal-grid.component';
Here is my package.config.json :
{
"name": "my-perso-components",
"version": "1.0.13",
"main": "index.js",
"scripts": {
"nbuild": "tsc index.ts --outDir ./lib",
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"build:e2e": "tsc -p e2e/",
"serve": "lite-server -c=bs-config.json",
"serve:e2e": "lite-server -c=bs-config.e2e.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pree2e": "npm run build:e2e",
"e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
"preprotractor": "webdriver-manager update",
"protractor": "protractor protractor.config.js",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"test:once": "karma start karma.conf.js --single-run",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"dependencies": {
"#angular/common": "~2.4.0",
"#angular/compiler": "~2.4.0",
"#angular/core": "~2.4.0",
"#angular/forms": "~2.4.0",
"#angular/http": "~2.4.0",
"#angular/platform-browser": "~2.4.0",
"#angular/platform-browser-dynamic": "~2.4.0",
"#angular/router": "~3.4.0",
"angular-in-memory-web-api": "~0.2.4",
"systemjs": "0.19.40",
"core-js": "^2.4.1",
"rxjs": "5.0.1",
"zone.js": "^0.7.4"
},
"devDependencies": {
"#types/core-js": "^0.9.35",
"#types/jasmine": "^2.5.36",
"#types/node": "^6.0.46",
"canonical-path": "0.0.2",
"concurrently": "^3.1.0",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"lite-server": "^2.2.2",
"lodash": "^4.16.4",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"tslint": "^3.15.1",
"typescript": "~2.0.10"
},
"license": "ISC"
}
my tsconfig.json :
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"outDir": "./lib",
"suppressImplicitAnyIndexErrors": true,
"typeRoots": [
"./node_modules/#types"
],
"types": [
"core-js"
]
}
}
and my project architecture looks like this :
- lib
- node_modules
- src
- app
- my-perso-grid
- my-perso-grid.component.ts
- my-perso-grid.html
- my-perso-grid.scss
- .npmignore
- index.ts
- package.json
- tsconfig.json
I then run tsc index.ts --outDir ./lib and do npm version patch , then npm publish.
My code is in npm for sure, I can then install it in another folder... However, I always get a 404 issue :
Error: (SystemJS) XHR error (404 Not Found) loading
http://localhost:3000/my-perso-components Error: XHR error (404 Not Found)
Which is weird, because I do have to autocompletion for the name of my component when I do the Import inside another component and no error shown !!
Any idea ? what can I try ? Do you have a recent and working tutorial to create, publish and use an npm package ?
Thanks a lot... will help a lot

Have you checked this http://blog.mgechev.com/2017/01/21/distributing-an-angular-library-aot-ngc-types/ Distributing an Angular Library - The Brief Guide

Related

Not being able to run NestJS application

I don't know what happened but I'm not being able to run a NestJS application locally
What I've tried so far:
deleting node_modules
deleting package-lock.json
npm i --save-dev #types/node
gave up and git cloned the whole application in a different folder (that runs on my colleagues' machines but not mine, they couldn't figure it out either)
I've been losing my mind, any help is appreciated
NPM Version 8.5.4
Node.js Version v16.14.2
tsconfig.json:
{
"compilerOptions": {
"lib": ["es2020"],
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"target": "es2020",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./src/",
"paths": {
"<rootDir>/": ["."]
},
"incremental": true,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"resolveJsonModule": true
}
}
tsconfig.build.json:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "__mocks__", "dist", "**/*spec.ts"]
}
package.json:
{
"name": "REDACTED",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"check-env": "node -r ts-node/register -r tsconfig-paths/register src/checkEnvs.ts",
"prebuild": "rimraf dist && npm run check-env",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node --max-old-space-size=2500 dist/src/main.js",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --detectOpenHandles",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:noCov": "jest --watch --collectCoverage=false",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"prisma:create": "npx prisma migrate dev --create-only",
"prisma:run": "npx prisma migrate dev",
"prisma:generate": "npx prisma generate"
},
"dependencies": {
"#elastic/elasticsearch": "7.10",
"#nestjs/bull": "^0.4.2",
"#nestjs/common": "^8.0.0",
"#nestjs/config": "^1.1.0",
"#nestjs/core": "^8.0.0",
"#nestjs/elasticsearch": "^8.0.0",
"#nestjs/event-emitter": "^1.0.0",
"#nestjs/microservices": "^8.1.2",
"#nestjs/mongoose": "^9.0.3",
"#nestjs/platform-express": "^8.4.4",
"#nestjs/schedule": "^1.1.0",
"#ntegral/nestjs-sentry": "^3.0.7",
"#prisma/client": "^3.14.0",
"#sentry/node": "^6.17.3",
"#sentry/types": "^6.17.3",
"amqp-connection-manager": "^3.7.0",
"amqplib": "^0.8.0",
"axios": "^0.24.0",
"bull": "^3.29.3",
"class-validator": "^0.13.2",
"cli-progress": "^3.10.0",
"date-fns": "^2.27.0",
"fast-csv": "^4.3.6",
"lodash": "^4.17.21",
"mongoose": "^6.2.10",
"nestjs-ftp": "^0.1.13",
"nestjs-soap": "^2.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"xml-js": "^1.6.11"
},
"devDependencies": {
"#golevelup/ts-jest": "^0.3.2",
"#nestjs/cli": "^8.0.0",
"#nestjs/schematics": "^8.0.0",
"#nestjs/testing": "^8.0.0",
"#types/bull": "^3.15.7",
"#types/cli-progress": "^3.9.2",
"#types/cron": "^1.7.3",
"#types/express": "^4.17.13",
"#types/jest": "^27.0.1",
"#types/lodash": "^4.14.179",
"#types/node": "^16.0.0",
"#types/sax": "^1.2.3",
"#types/supertest": "^2.0.11",
"#types/validator": "^13.7.2",
"#typescript-eslint/eslint-plugin": "^4.28.2",
"#typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.0.6",
"leaked-handles": "^5.2.0",
"mongodb-memory-server": "^8.4.2",
"prettier": "^2.3.2",
"prisma": "^3.14.0",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
}
}
If someone finds this question pulling their hair out, it turned out to have been my tsconfig.json file, all I had to do was roll back the lib version from "lib": ["es2020"] to "lib": ["es2017"],
I wish I had seen that on my crystal ball

Cannot add Material Design to Angular 4 project

I try to add Material Design to my project following the official tutorial ( https://material.angular.io/guide/getting-started ) but I get an error:
node_modules/#angular/cdk/typings/rxjs/rx-operators.d.ts(11,10): error TS2305: Module '"path_to_project/nod
e_modules/rxjs/Scheduler"' has no exported member 'IScheduler'.
node_modules/#angular/platform-browser/animations/src/providers.d.ts(8,119): error TS2305: Module '"path_to_project/node_modules/#angular/animations/browser"' has no exported member 'ɵDomAnimationEngine'.
I cannot find solution in the forums.
My package.json:
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"build:e2e": "tsc -p e2e/",
"serve": "lite-server -c=bs-config.json",
"serve:e2e": "lite-server -c=bs-config.e2e.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pree2e": "npm run build:e2e",
"e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
"preprotractor": "webdriver-manager update",
"protractor": "protractor protractor.config.js",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"test:once": "karma start karma.conf.js --single-run",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"#angular/animations": "^4.2.6",
"#angular/cdk": "github:angular/cdk-builds",
"#angular/common": "~4.0.0",
"#angular/compiler": "~4.0.0",
"#angular/core": "~4.0.0",
"#angular/forms": "~4.0.0",
"#angular/http": "~4.0.0",
"#angular/material": "github:angular/material2-builds",
"#angular/platform-browser": "~4.0.0",
"#angular/platform-browser-dynamic": "~4.0.0",
"#angular/router": "~4.0.0",
"angular-in-memory-web-api": "~0.3.0",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"rxjs": "5.0.1",
"systemjs": "0.19.40",
"zone.js": "^0.8.4"
},
"devDependencies": {
"#types/jasmine": "2.5.36",
"#types/node": "^6.0.81",
"canonical-path": "0.0.2",
"concurrently": "^3.5.0",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"lite-server": "^2.2.2",
"lodash": "^4.16.4",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"tslint": "^3.15.1",
"typescript": "~2.2.2"
},
"repository": {}
}
and my systemjs.config.js:
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
'app': 'app',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic': 'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'#angular/material': 'npm:#angular/material/bundles/material.umd.js',
'#angular/cdk': 'npm:#angular/cdk/bundles/cdk.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
defaultExtension: 'js',
meta: {
'./*.js': {
loader: 'systemjs-angular-loader.js'
}
}
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
Newest material-2.0.0-beta.8 has a dependency on #angular/cdk which has a dependency on rxjs version 5.0.3 or greater.
So you have to change the rxjs version in your package.json:
npm install rxjs#5.0.3 --save
or
"rxjs": "~5.0.3"
Also, #angular/animations version 4.2.x has a know issue of
error TS2305: Module '"path_to_project/node_modules/#angular/animations/browser"' has no exported member 'ɵDomAnimationEngine'.
If you still get this error, you might need to fall back to version 4.1.x.
npm install '#angular/animations"#4.1.3' --save

error TS5014: Failed to parse file '': Unexpected token u in JSON at position 0. The terminal process terminated with exit code: 1

I am receiving this error.
error TS5014: Failed to parse file '/Users/Documents/tsconfig.json/tsconfig.json': Unexpected token u in JSON at position 0.
The terminal process terminated with exit code: 1
My system setup:
Visual Studio Code Version 1.14.1
Angular 2
node Version v8.0.0
tsc Version 2.4.1
package.json is as follows
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",
"e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
"lint": "tslint ./app/**/*.ts -t verbose",
"lite": "lite-server",
"pree2e": "webdriver-manager update",
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
"test-once": "tsc && karma start karma.conf.js --single-run",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"#angular/common": "^2.4.10",
"#angular/compiler": "^2.4.10",
"#angular/core": "^2.4.10",
"#angular/forms": "^2.4.10",
"#angular/http": "^2.4.10",
"#angular/platform-browser": "^2.4.10",
"#angular/platform-browser-dynamic": "^2.4.10",
"#angular/router": "^3.4.10",
"angular-in-memory-web-api": "^0.2.5",
"core-js": "^2.4.1",
"rxjs": "^5.4.2",
"systemjs": "^0.19.47",
"zone.js": "^0.7.8"
},
"devDependencies": {
"#types/jasmine": "^2.5.41",
"#types/node": "^6.0.46",
"canonical-path": "0.0.2",
"concurrently": "^3.5.0",
"http-server": "^0.9.0",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"lite-server": "^2.3.0",
"lodash": "^4.16.4",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"tslint": "^3.15.1",
"typescript": "2.1.6"
},
"repository": {}
}
tsconfig.json is as follows
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "dist"
}
}
How did you define your tasks.json? I encountered the same issue when I defined my tasks.json with default typescript type. Following changes in tasks.json worked for me based on the answer in link
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "compile",
"type": "shell",
"command": "tsc -p tsconfig.json",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}

clarity-angular not found (404 error)

json:
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"build:e2e": "tsc -p e2e/",
"serve": "lite-server -c=bs-config.json",
"serve:e2e": "lite-server -c=bs-config.e2e.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pree2e": "npm run build:e2e",
"e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
"preprotractor": "webdriver-manager update",
"protractor": "protractor protractor.config.js",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"test:once": "karma start karma.conf.js --single-run",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"#angular/common": "~4.0.0",
"#angular/compiler": "~4.0.0",
"#angular/core": "~4.0.0",
"#angular/forms": "~4.0.0",
"#angular/http": "~4.0.0",
"#angular/platform-browser": "~4.0.0",
"#angular/platform-browser-dynamic": "~4.0.0",
"#angular/router": "~4.0.0",
"#webcomponents/custom-elements": "^1.0.0-alpha.3",
"angular-in-memory-web-api": "~0.3.0",
"clarity-angular": "^0.8.12",
"clarity-icons": "^0.8.12",
"clarity-ui": "^0.8.12",
"core-js": "^2.4.1",
"rxjs": "5.0.1",
"systemjs": "0.19.40",
"zone.js": "^0.8.4"
},
"devDependencies": {
"concurrently": "^3.2.0",
"lite-server": "^2.2.2",
"typescript": "~2.1.0",
"canonical-path": "0.0.2",
"tslint": "^3.15.1",
"lodash": "^4.16.4",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"#types/node": "^6.0.46",
"#types/jasmine": "2.5.36"
},
"repository": {}
}
I was integrating clarity package with my angular 2 app. I have folloowed each steps and installed the clasrity-angular with npm install clarity-angular --save command, which is the last step. My app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { ClarityModule } from 'clarity-angular';
import { AppComponent } from './app.component';
#NgModule({
imports: [
BrowserModule,
ClarityModule.forRoot(),
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
But whenever I add the line ClarityModule.forRoot(), am getting an error saying,
Failed to load resource: the server responded with a status of 404 (Not Found)
Any idea guys? Thanks in advance
As you are using Systemjs you should configure systemjs config properly:
System.config({
...
map: {
...
'clarity-angular': 'node_modules/clarity-angular/clarity-angular.umd.js',
},
...
});

NPM INSTALL does not recreates .js files from .ts files

I am trying to compile my angular2/typescript files to javascript files.
"npm install" (without any warnings or errors)
creates node_modules, but not recreates .js files form my .ts files and angular modules does not updates.
My tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true
},
"files": [
],
"exclude": [
"node_modules",
"scripts"
],
"compileOnSave": true
}
and package.json:
{
"name": "angular-quickstart",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
"#angular/common": "2.0.0",
"#angular/compiler": "2.0.0",
"#angular/core": "2.0.0",
"#angular/forms": "2.0.0",
"#angular/http": "2.0.0",
"#angular/platform-browser": "2.0.0",
"#angular/platform-browser-dynamic": "2.0.0",
"#angular/router": "3.0.0",
"#angular/upgrade": "2.0.0",
"angular2-in-memory-web-api": "0.0.20",
"bootstrap": "^3.3.6",
"core-js": "^2.4.1",
"react-redux": "^4.4.5",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.27",
"zone.js": "^0.6.23",
"react-super-components": "^0.3.5",
"redux": "^3.5.2",
"redux-thunk": "^2.1.0",
"param-store":"^1.0.0"
},
"devDependencies": {
"concurrently": "^2.2.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.2",
"typings": "^1.3.2"
}
}
after NPM START there is
1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
[1] 16.09.23 14:05:50 404 POST /api/logging
in log and it's never ends.
What can i check?
npm install will just install the mentioned packages in package.json file.
to generate js code you need to run
npm start
which will run typescript compiler in watch mode and will transpile ts code to js code.
look here what npm start command does:
start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\"
npm install will install the packages listed in package.json
If the typings folder doesn't show up after running npm install, you'll need to install it manually with the command:
npm run typings install
You need to run npm start
This command runs the following two parallel node processes:
The TypeScript compiler in watch mode which will take of transpiling from typescript to javascript.
A static file server called lite-server that loads index.html in a browser and refreshes the browser when application files change.

Resources