Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

npm run serve:ssr : package.json does not exist #1207

Closed

Description

@anuragd7

Bug Report

What is the expected behavior?

npm run build:ssr is building without errors. npm run serve:ssr is not running as expected

What is the current behavior?

npm run serve:ssr fails with the following errors

> x-y-z@0.0.0 serve:ssr /Users/NRMac/Projects/newSMT/xyz
> node dist/server

/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:173019
        throw new Error("package.json does not exist at " + package_json_path);
        ^

Error: package.json does not exist at /Users/NRMac/Projects/newSMT/xyz/dist/package.json
    at Object.PsoT.exports.find (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:173019:15)
    at Object.wPNL (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:308568:12)
    at __webpack_require__ (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:20:30)
    at Object.XpdW (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:196998:12)
    at __webpack_require__ (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:20:30)
    at Object.g1pB (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:229730:27)
    at __webpack_require__ (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:20:30)
    at Object.Ou8q (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:170377:14)
    at __webpack_require__ (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:20:30)
    at Object.BYZf (/Users/NRMac/Projects/newSMT/xyz/dist/server/main.js:99702:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! set-my-test@0.0.0 serve:ssr: `node dist/server`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the set-my-test@0.0.0 serve:ssr 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!     /Users/NRMac/.npm/_logs/2019-07-22T07_43_50_405Z-debug.log

What modules are related to this issue?

- [ ] aspnetcore-engine
- [ ] common
- [ ] express-engine
- [ ] hapi-engine
- [ ] module-map-ngfactory-loader

Minimal reproduction with instructions:

What is the use-case or motivation for changing an existing behavior?

Environment:

@nguniversal versions

  • express-engine:
  • module-map-ngfactory-loader:

Output from: ng --version.

Angular CLI: 8.1.2
Node: 10.16.0
OS: darwin x64
Angular: 8.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router

Package                                    Version
--------------------------------------------------------------------
@angular-devkit/architect                  0.801.2
@angular-devkit/build-angular              0.801.2
@angular-devkit/build-optimizer            0.801.2
@angular-devkit/build-webpack              0.801.2
@angular-devkit/core                       8.1.2
@angular-devkit/schematics                 8.1.2
@angular/fire                              5.2.1
@ngtools/webpack                           8.1.2
@nguniversal/express-engine                8.1.1
@nguniversal/module-map-ngfactory-loader   8.1.1
@schematics/angular                        8.1.2
@schematics/update                         0.801.2
rxjs                                       6.5.2
typescript                                 3.4.5
webpack                                    4.35.2

Is there anything else we should know?

Working on Angular 8.1 with Firestore
Tried modifying webpack.server.config.js, package.json and server.ts based on the issue here https://stackoverflow.com/questions/57030914/firebase-firestore-not-working-with-angular-universal-package-json-does-not-exi

webpack.server.config.js

// Work around for https://github.com/angular/angular-cli/issues/7200

const path = require("path");
const webpack = require("webpack");

const APP_NAME = "xyz"; // CHANGE ME

module.exports = {
  mode: "development",
  entry: {
    // This is our Express server for Dynamic universal
    server: "./server.ts"
  },
  resolve: { extensions: [".js", ".ts"] },
  externals: {
    "./dist/server/main": 'require("./server/main")'
    /* Firebase has some troubles being webpacked when in
       in the Node environment, let's skip it.
       Note: you may need to exclude other dependencies depending
       on your project. */
    //  /^firebase/
  },
  target: "node",
  optimization: {
    minimize: false
  },
  output: {
    // Puts the output at the root of the dist folder
    path: path.join(__dirname, `dist/${APP_NAME}`),
    library: "app",
    libraryTarget: "umd",
    filename: "[name].js"
  },
  module: {
    noParse: /polyfills-.*\.js/,
    rules: [
      { test: /\.ts$/, loader: "ts-loader" },
      {
        // Mark files inside `@angular/core` as using SystemJS style dynamic imports.
        // Removing this will cause deprecation warnings to appear.
        test: /(\\|\/)@angular(\\|\/)core(\\|\/).+\.js$/,
        parser: { system: true }
      }
    ]
  },
  plugins: [
    new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?angular(\\|\/)core(.+)?/,
      path.join(__dirname, "src"), // location of your src
      {} // a map of your routes
    ),
    new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?express(\\|\/)(.+)?/,
      path.join(__dirname, "src"),
      {}
    )
  ]
};

server.ts

/**
 * *** NOTE ON IMPORTING FROM ANGULAR AND NGUNIVERSAL IN THIS FILE ***
 *
 * If your application uses third-party dependencies, you'll need to
 * either use Webpack or the Angular CLI's `bundleDependencies` feature
 * in order to adequately package them for use on the server without a
 * node_modules directory.
 *
 * However, due to the nature of the CLI's `bundleDependencies`, importing
 * Angular in this file will create a different instance of Angular than
 * the version in the compiled application code. This leads to unavoidable
 * conflicts. Therefore, please do not explicitly import from @angular or
 * @nguniversal in this file. You can export any needed resources
 * from your application's main.server.ts file, as seen below with the
 * import for `ngExpressEngine`.
 */

import "zone.js/dist/zone-node";
import "reflect-metadata";
import { enableProdMode } from "@angular/core";

import * as express from "express";
import { join } from "path";
import { readFileSync } from "fs";

// Polyfills required for Firebase
(global as any).WebSocket = require("ws");
(global as any).XMLHttpRequest = require("xhr2");

import { renderModuleFactory } from "@angular/platform-server";
import { REQUEST, RESPONSE } from "@nguniversal/express-engine/tokens";
import { ValueProvider } from "@angular/core";

// Express server
enableProdMode();
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), "dist");
const APP_NAME = "xyz"; // CHANGE ME

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP,
  ngExpressEngine,
  provideModuleMap
} = require(`./dist/server/main`);

// index.html template
const template = readFileSync(
  join(DIST_FOLDER, "browser", "index.html")
).toString();

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
// app.engine('html', ngExpressEngine({
//   bootstrap: AppServerModuleNgFactory,
//   providers: [
//     provideModuleMap(LAZY_MODULE_MAP)
//   ]
// }));

app.engine("html", (_, options, callback) =>
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [
      provideModuleMap(LAZY_MODULE_MAP),
      {
        provide: REQUEST,
        useValue: options.req
      },
      {
        provide: RESPONSE,
        useValue: options.req.res
      }
    ]
  })(_, options, callback)
);

app.set("view engine", "html");
app.set("views", join(DIST_FOLDER, "browser"));

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

// All regular routes use the Universal engine
// app.get("*", (req, res) => {
//   res.render("index", { req });
// });

// Serve static files from /browser
app.get(
  "*.*",
  express.static(join(DIST_FOLDER, "browser"), {
    maxAge: "1y"
  })
);

// All regular routes use the Universal engine
app.get("*", (req, res) => {
  res.render(join(DIST_FOLDER, "browser", "index.html"), { req });
});

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

Package.json

{
  "name": "x-of-y",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "compile:server": "webpack --config webpack.server.config.js --progress --colors",
    "serve:ssr": "node dist/server",
    "build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
    "build:client-and-server-bundles": "ng build --prod && ng run xyz:server:production --bundleDependencies all"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.1.2",
    "@angular/common": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/platform-server": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@nguniversal/express-engine": "^8.1.1",
    "@nguniversal/module-map-ngfactory-loader": "8.1.1",
    "animate.css": "^3.7.2",
    "core-js": "^2.5.4",
    "express": "^4.15.2",
    "rxjs": "~6.5.2",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.801.2",
    "@angular/cli": "~8.1.2",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/fire": "^5.2.1",
    "@angular/language-service": "~8.1.2",
    "@fortawesome/fontawesome-free": "^5.9.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "chart.js": "^2.8.0",
    "codelyzer": "^5.0.1",
    "easy-pie-chart": "^2.1.7",
    "firebase": "^6.3.0",
    "hammerjs": "^2.0.8",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "ngx-quill": "^7.1.0",
    "protractor": "~5.4.0",
    "quill": "^1.3.6",
    "quill-emoji": "^0.1.7",
    "screenfull": "^4.2.0",
    "tinify": "^1.6.0-beta.2",
    "ts-loader": "^5.2.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.4.5",
    "webpack-cli": "^3.1.0"
  }
}

Activity

  1. bdairy commented on Jul 25, 2019

    @bdairy

    Did u get to solve this issue??

  2. anuragd7 commented on Jul 26, 2019

    @anuragd7
    Author

    Not yet, hoping for a response from the community.

  3. kpsrinivas commented on Jul 27, 2019

    @kpsrinivas

    The problem is "build:client-and-server-bundles": "ng build --prod && ng run your-project:server:production --bundleDependencies all" in package.json . When you change it to ng run your-project:server:production --bundleDependencies none it works. But if you try to package it, the externals are getting messed up.

  4. vikerman commented on Jul 30, 2019

    @vikerman
    Contributor

    Thanks @kpsrinivas . We don't currently allow configuring what libraries should be made external. Can you tell me which library you are trying to use that is causing this issue?

  5. kpsrinivas commented on Jul 30, 2019

    @kpsrinivas

    Not the library. When I try to package my server as umd and import it for firebase functions.

  6. kpsrinivas commented on Jul 30, 2019

    @kpsrinivas

    This step https://github.com/angular/angularfire2/blob/master/docs/universal/cloud-functions.md fails
    I know its not universal issue. But build fails.

  7. vikerman commented on Jul 30, 2019

    @vikerman
    Contributor

    We have multiple people using angular fire who seem to have problem with --bundleDependencies all. The current workaround is to just remove/set --bundleDependencies none.

    We are going to follow up with angularfire folks to fix this properly

  8. darshanDevrai commented on Aug 26, 2019

    @darshanDevrai

    Has anyone solved this issue yet?
    @kpsrinivas @vikerman @bdairy @anuragd7

  9. Mph1399 commented on Sep 11, 2019

    @Mph1399

    Has anyone solved this issue yet?
    @kpsrinivas @vikerman @bdairy @anuragd7
    Looks like Vikerman's response is the best fix and seems to work.

    1. Go to your package.json file, under the "scripts" section & look for "build:client-and-server-bundles".
    2. At the end of that line it has "--bundleDependencies all"
    3. set that to "--bundleDependencies none" or delete it(I set it to all and it works)
    4. re-run npm run build:ssr
  10. BeSpunky commented on Sep 11, 2019

    @BeSpunky

    This sounds like a work around rather than a solution.
    Isn't --bundleDependencies all there for a reason?

  11. zeeshanshanif commented on Sep 16, 2019

    @zeeshanshanif

    i have tried "--bundleDependencies none" but still getting error

  12. vikerman commented on Sep 16, 2019

    @vikerman
    Contributor

    @zeeshanshanif - Anyway you can create a repro github repo?

    @BeSpunky - Bundling does help in fast cold start times and so it is definitely the way we are heading towards. But there are some packages that don't bundle well - And we want to provide an escape hatch for those cases. In some cases it might just be temporary as we figure out how to bundle a particular dependency better.

  13. added
    need: repro steps
    on Sep 25, 2019
  14. unrealbato commented on Nov 10, 2019

    @unrealbato

    any Updates? "--bundleDependencies none" work to avoid "package.json" issue but i faced another issue as i guess that the SSR doesn't wait for observable to fetch data from Firestore! i don't know how to properly fix this.

    all i want is to fetch user object from Firestore & update meta tags for Facebook Crawls share debugger!!!!

    any Help Would be appreciated. Thanks

  15. BeSpunky commented on Nov 10, 2019

    @BeSpunky

    If you solved the bundling problem, your question is not related to this thread.
    However, if I understand your problem correctly, this might help you:
    https://stackoverflow.com/a/50065783/4371525

  16. 19 remaining items

  17. ritikrishu commented on Jan 17, 2020

    @ritikrishu
    "@grpc/proto-loader"

    this does resolve the build error. but when the function executes, it gives a runtime error

    ERROR Error: ENOENT: no such file or directory, open '/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/dist/gumnam-blog/server/src/protos/google/firestore/v1/firestore.proto'
    >      at Object.openSync (fs.js:440:3)
    >      at Object.readFileSync (fs.js:342:35)
    >      at fetch (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/node_modules/protobufjs/src/root.js:160:34)
    >      at Root.load (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/node_modules/protobufjs/src/root.js:194:13)
    >      at Root.loadSync (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/node_modules/protobufjs/src/root.js:235:17)
    >      at Object.loadSync (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/node_modules/@grpc/proto-loader/build/src/index.js:221:27)
    >      at loadProtos (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/dist/gumnam-blog/server/main.js:1:1597303)
    >      at NodePlatform.loadConnection (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/dist/gumnam-blog/server/main.js:1:1597460)
    >      at FirestoreClient.initializeRest (/Users/ritikrishu/Documents/Personal/gumnam-blog/functions/dist/gumnam-blog/server/main.js:1:1508557)
    >      at /Users/ritikrishu/Documents/Personal/gumnam-blog/functions/dist/gumnam-blog/server/main.js:1:1505438 {
    

    angular version 9^

  18. alan-agius4 commented on Jan 17, 2020

    @alan-agius4
    Collaborator

    @ranjitpatra you also need to exclude grpc from bundling.

    See: #1207 (comment)

  19. naingaungphyo commented on Jan 26, 2020

    @naingaungphyo

    @kuncevic yeah, I got the same error for using angularfire. current workaround is by setting grpc as externalDependencies as @alan-agius4 said.

  20. codemysoul commented on Feb 17, 2020

    @codemysoul

    If you can, you could create a separate module for Browser, and import AngularFire modules there.

    For example, create a app.browser.module.ts - and add all your AngularFire imports there.

    Then just point your main.ts file to bootstrap from that module instead.

  21. ritikrishu commented on Feb 25, 2020

    @ritikrishu

    @codemysoul there is no point of SSR, if you only include those in the browser bundle.

  22. ahelmel commented on Apr 10, 2020

    @ahelmel

    I have a similar issue.
    I added grpc and @grpc/proto-loader as externalDependencies in angular.json.
    But now on runtime when running serve:ssr I get the following error:

    Error: ENOENT: no such file or directory, open '/dist/cv/server/src/protos/google/firestore/v1/firestore.proto'
    

    I am using angular: 9.1.1, firebase: 7.14.0 and @angular/fire: 6.0.0.
    Any ideas?

  23. curiouscod3 commented on Apr 10, 2020

    @curiouscod3

    This is still happening in 9.1

    adding grpc stuff in angular.json doesn't work!

    > Error: ENOENT: no such file or directory, open 'google/protobuf/api.proto'
    >     at Object.openSync (fs.js:457:3)
    >     at Object.readFileSync (fs.js:359:35)
    
    
    Angular CLI: 9.1.1
    Node: 12.16.1
    OS: darwin x64
    
    Angular: 9.1.1
    ... animations, cli, common, compiler, compiler-cli, core, forms
    ... language-service, platform-browser, platform-browser-dynamic
    ... platform-server, router
    Ivy Workspace: Yes
    
    Package                           Version
    -----------------------------------------------------------
    @angular-devkit/architect         0.900.7
    @angular-devkit/build-angular     0.901.1
    @angular-devkit/build-optimizer   0.901.1
    @angular-devkit/build-webpack     0.901.1
    @angular-devkit/core              9.0.7
    @angular-devkit/schematics        9.1.1
    @angular/cdk                      9.2.0
    @angular/fire                     6.0.0
    @angular/flex-layout              9.0.0-beta.29
    @angular/material                 9.2.0
    @ngtools/webpack                  9.1.1
    @nguniversal/builders             9.1.0
    @nguniversal/common               9.1.0
    @nguniversal/express-engine       9.1.0
    @schematics/angular               9.1.1
    @schematics/update                0.901.1
    rxjs                              6.5.5
    typescript                        3.8.3
    webpack                           4.42.0
    
  24. hiepxanh commented on Apr 11, 2020

    @hiepxanh
    Contributor

    add this @firebase/firestore to externalDependencies

    // angular.json
    // projects.web.architect
    ...
     "server": {
              "builder": "@angular-devkit/build-angular:server",
              "options": {
                ...
                "externalDependencies": [
                  "@firebase/firestore"
                ]
                ...
              }
    
    

    I already create an PR to clarify that https://github.com/angular/angularfire/pull/2372/files?short_path=e76f33d#diff-e76f33db0439ee8e320dfa7ecf47f09b

  25. hiepxanh commented on Apr 11, 2020

    @hiepxanh
    Contributor

    example repository follow that instruction above here: https://github.com/hiepxanh/angular-fire-universal
    and official example using webpack here: https://github.com/angular/angularfire/tree/master/sample

  26. herreraLeonar commented on May 3, 2020

    @herreraLeonar

    I fixed error "Element is not defined" deleting "ng-inline-svg": "^8.5.1", in package.json, Angular 9

  27. AnriTapel commented on Jul 9, 2020

    @AnriTapel

    Adding grpc, @grpc/proto-loader, @firebase/firestore to externalDependencies helped me, so thanks to all of you! Been strugglin' with this issue for three weeks!

  28. alan-agius4 commented on Oct 14, 2022

    @alan-agius4
    Collaborator

    Thanks for reporting this issue. Luckily, it has already been fixed in one of the recent releases. Please update to the most recent version to resolve the problem.

  29. angular-automatic-lock-bot commented on Nov 14, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  30. locked and limited conversation to collaborators on Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

need: investigationtype: bug/fix

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions