Node.js – npm ERR! missing script: build:universal

angularnode.jsnpm

I am trying to run my Angular application on the server side and I am facing these errors:

npm ERR! missing script: build:universal

npm ERR! A complete log of this run can be found in: npm ERR!
/home/training/.npm/_logs/2018-10-03T11_50_40_593Z-debug.log

I used the official Angular application for doing this

https://angular.io/guide/universal

The universal build is causing errors shown below:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli 'run',
1 verbose cli 'build:universal' ]
2 info using npm@6.2.0
3 info using node@v10.8.0
4 verbose stack Error: missing script: build:universal
4 verbose stack at run (/usr/local/lib/node_modules/npm/lib/run-script.js:155:19)
4 verbose stack at /usr/local/lib/node_modules/npm/lib/run-script.js:63:5
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:115:5
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:418:5
4 verbose stack at checkBinReferences_ (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:373:45)
4 verbose stack at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:416:3)
4 verbose stack at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:160:5)
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:280:12
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)
5 verbose cwd /home/training/Desktop/vishnu/TemplateAppv6
6 verbose Linux 4.4.0-134-generic
7 verbose argv "/usr/bin/nodejs" "/usr/local/bin/npm" "run" "build:universal"
8 verbose node v10.8.0
9 verbose npm v6.2.0
10 error missing script: build:universal
11 verbose exit [ 1, true ]

Best Answer

As implied in my comment, you may have forgotten to add the build-script to your project:

From the universal docs

Build and run with universal: Now that you've created the TypeScript and Webpack config files and configured the Angular CLI, you can build and run the Universal application.

First add the build and serve commands to the scripts section of the package.json:

"scripts": {
    ...
    "build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
    "serve:ssr": "node dist/server",
    "build:client-and-server-bundles": "ng build --prod && ng run angular.io-example:server",
     "webpack:server": "webpack --config webpack.server.config.js --progress --colors"
    ...
}

So check you package.json and try the build again..