user6600549
user6600549

Reputation:

How to run .ts file script and angular 5 build using NPM

I have a file with .ts extension and I want to execute this file first and then to run npm run build to build my angular 5 project

package.json
 "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "compile": "npm-run-all myts build",
    "myts": "ts-node git.version.ts",
    "build": "ng build --prod",
    "test": "ng test",
  },

And I get this error

npm run compile

> [email protected] compile C:\Users\George35mk\Documents\Projects\gui\frontend\> 
npm-run-all myts build

'npm-run-all' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] compile: `npm-run-all myts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] compile script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\George35mk\AppData\Roaming\npm-cache\_logs\2018-03-01T14_47_01_179Z-debug.log
PS C:\Users\George35mk\Documents\Projects\gui\frontend\

If I run this is work

ts-node git.version.ts

The problem is I cant run serial scripts using npm

Upvotes: 1

Views: 4324

Answers (2)

ZiiMakc
ZiiMakc

Reputation: 37026

I'm using node/express/angular with typescript and found only one way to do all the work with one command without installing anything.

My way let me: 1. Compile node ts server files to js and watch for changes in ts files 2. Compile angular project, serve it to node server and watch for changes in angular 3. Run node server and restart it if there was changes.

All that i do is run "npm start"

My package.json

{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/server.js",
  "scripts": {
    "start": "start tsc --watch & cd ../angular & start ng build --watch & cd ../node & timeout 15 & start nodemon --watch ../angular/dist --watch dist"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3",
    "ws": "^6.0.0"
  },
  "devDependencies": {
    "@types/express": "^4.16.0",
    "@types/node": "^10.9.2",
    "@types/ws": "^6.0.0",
    "typescript": "^3.0.1"
  }
}

My project structure:

My_project
 - node
 - angular

tsconfig.json optons:
    "outDir": "dist", 
    "rootDir": "src"

Upvotes: 1

Abhijit Kar ツ
Abhijit Kar ツ

Reputation: 1732

Modify the package.json in the following way:

"scripts": {
  "ng": "ng",
  "start": "ng serve",
  "compile": "npm run myts && npm run build",
  "myts": "ts-node git.version.ts",
  "build": "ng build --prod",
  "test": "ng test",
}

Explanation

  1. && makes sure that first command runs, followed by the other.
  2. You don't need npm-run-all to do that
  3. But if you want to use npm-run-all, then do npm install npm-run-all --save-dev

Upvotes: 1

Related Questions