0
votes

Im new to node - I created a service.ts and im trying to use nodemon build

but im getting error

does anyone has a solution ?

my code :

nodemon.json

{   "ignore": [
    "src/**/*.spec.ts",
    "**/*.test.ts",
    "**/*.spec.ts",
    ".git",
    "node_modules"   ],   "watch": [],   "exec": "ts-node",   "ext": "ts-node" }

pakage json :

  "scripts": {

    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "sprite": "svg-sprite --config src/sprite-config.json src/svg/*.svg",
    "start:ssr": "npm run build:ssr && npm run serve:ssr",
    "dev:ssr": "ng run stroyka:serve-ssr",
    "serve:ssr": "node dist/stroyka/server/main.js",
    "serve:start": "nodemon server.ts",
    "build:ssr": "ng build --prod && ng run stroyka:server:production",
    "prerender": "ng run stroyka:prerender"   },

server.ts

import 'zone.js/dist/zone-node';

import { ngExpressEngine } from '@nguniversal/express-engine'; import
* as express from 'express'; import { join } from 'path';

import { AppServerModule } from './src/main.server'; import { APP_BASE_HREF } from '@angular/common'; import { existsSync } from 'fs'; import { Express } from 'express';

// The Express app is exported so that it can be used by serverless Functions. export function app(): Express {
    const server = express();
    const distFolder = join(process.cwd(), 'dist/stroyka/browser');
    const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' :  'index';

    // Our Universal express-engine (found @  https://github.com/angular/universal/tree/master/modules/express-engine)
    server.engine('html', ngExpressEngine({
        bootstrap: AppServerModule,
    }));

    server.set('view engine', 'html');
    server.set('views', distFolder);

    // Example Express Rest API endpoints
    // server.get('/api/**', (req, res) => { });
    // Serve static files from /browser
    server.get('*.*', express.static(distFolder, {
        maxAge: '1y'
    }));

    // All regular routes use the Universal engine
    server.get('*', (req, res) => {
        res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
    });

    return server; }

function run(): void {
    const port = process.env.PORT || 4000;

    // Start up the Node server
    const server = app();
    server.listen(port, () => {
        console.log(`Node Express server listening on http://localhost:${port}`);
    }); }

// Webpack will replace 'require' with '__webpack_require__' // '__non_webpack_require__' is a proxy to Node 'require' // The below code is to ensure that the server is run only when not requiring the bundle. declare const __non_webpack_require__: NodeRequire; const mainModule = __non_webpack_require__.main; const moduleFilename = mainModule && mainModule.filename || ''; if (moduleFilename ===
__filename || moduleFilename.includes('iisnode')) {
    run(); }

export * from './src/main.server';

the error : S C:\Users\amika\Desktop\project> npm run serve:start

[email protected] serve:start C:\Users\amika\Desktop\project nodemon server.ts

[nodemon] 2.0.4 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching extensions: ts-node [nodemon] starting ts-node server.ts

C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:245 return new TSError(diagnosticText, diagnosticCodes) ^ TSError: ⨯ Unable to compile TypeScript: src/app/app.server.module.ts:14:14 - error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

14 export class AppServerModule {} ~~~~~~~~~~~~~~~

at createTSError (C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:245:12)
at reportTSError (C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:249:19)
at getOutput (C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:362:34)
at Object.compile (C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:395:32)
at Module.m._compile (C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:473:43)
at Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\amika\Desktop\project\node_modules\ts-node\src\index.ts:476:12)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)

[nodemon] app crashed - waiting for file changes before starting...

1
Node does not run typescript out of the box. For that to work you will need something to compile your typescript into valid javascript that node (and thus nodemon) can then run. Just google using typescript with node for pointers on the missing config needed. - tijs

1 Answers

0
votes

you are trying to start typescript application directly with node. you should use ts-node package instead. run the following command to install it:

npm install --save-dev ts-node

then in your package.json scripts section, change nodemon server.ts to:

nodemon --exec ts-node server.js