Giter Club home page Giter Club logo

npm-start's Introduction

Paketo Buildpack for NPM Start

gcr.io/paketo-buildpacks/npm-start

The NPM Start CNB sets the start command for the given application. The start command is generated from the contents of package.json. For example, given a package.json with the following content:

{
  "scripts": {
    "prestart": "<prestart-command>",
    "poststart": "<poststart-command>",
    "start": "<start-command>"
  }
}

The start command will be <prestart-command> && <start-command> && <poststart-command>.

Enabling reloadable process types

You can configure this buildpack to wrap the entrypoint process of your app such that it kills and restarts the process whenever files change in the app's working directory in the container. With this feature enabled, copying new verisons of source code into the running container will trigger your app's process to restart. Set the environment variable BP_LIVE_RELOAD_ENABLED=true at build time to enable this feature.

Integration

This CNB sets a start command, so there's currently no scenario we can imagine that you would need to require it as dependency.

Usage

To package this buildpack for consumption:

$ ./scripts/package.sh --version <version-number>

This will create a buildpackage.cnb file under the build directory which you can use to build your app as follows:

pack build <app-name> -p <path-to-app> -b <path/to/node-engine.cnb> -b \
<path/to/npm-install.cnb> -b build/buildpackage.cnb

Specifying a project path

To specify a project subdirectory to be used as the root of the app, please use the BP_NODE_PROJECT_PATH environment variable at build time either directly (e.g. pack build my-app --env BP_NODE_PROJECT_PATH=./src/my-app) or through a project.toml file. This could be useful if your app is a part of a monorepo.

Specifying a custom start script

To specify a start script to be used instead of start, please use the BP_NPM_START_SCRIPT environment variable at build time either directly (e.g. pack build my-app --env BP_NPM_START_SCRIPT=myscript) or through a project.toml file.

Run Tests

To run all unit tests, run:

./scripts/unit.sh

To run all integration tests, run:

/scripts/integration.sh

Graceful shutdown and signal handling

You can add signal handlers in your app to support graceful shutdown and program interrupts. If running a node server is the start command, the buildpack runs the node server as the init process, and thus it ignores any signal with the default action. As a result, the process will not terminate on SIGINT or SIGTERM unless it is coded to do so. You can also use docker's --init flag to wrap your node process with an init system that will properly handle signals.

npm-start's People

Contributors

arjun024 avatar c0d1ngm0nk3y avatar dalbar avatar dependabot[bot] avatar foresteckhardt avatar joshuatcasey avatar kvedurmu avatar mhdawson avatar pacostas avatar paketo-bot avatar ryanmoran avatar samj1912 avatar sophiewigmore avatar thitch97 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

npm-start's Issues

Graceful shutdown tests seem flaky

See actions log, the graceful shutdown tests are flaky. But it looks like when you simulate the test's behavior on a terminal (docker stop <containerid> and read the container logs), they seem to consistently meet the test's expectation i.e. print out the sigterm handler log.

Enable Debug Logging

With the merging of RFC 27 buildpacks should respect the BP_LOG_LEVEL environment variable and allow for debugging logging.

A good example of how to minimally implement this feature can be found in Go Build.

Allow start command to be configurable

Describe the Enhancement

The buildpack currently only detects in the case that it sees a start script. The buildpack should allow users to have their start script be something other than start either through configuration or more permissive detection logic.

Possible Solution

Should we detect on other common script names, like serve?

Or should we include an environment variable like BP_NPM_START_SCRIPT=serve?

Motivation

Sometimes developers have good reason to not use start as the command they want their image to run at boot. For instance, the start command might be what you run locally to boot a development server while serve might be what is expected to run in production.

Investigate generating an SBOM with this buildpack

The sbom package in packit enables buildpack authors to easily generate SBOM content from the contents of an app directory. Now that this tooling exists, it's worth exploring whether this buildpack would provide value to users if it generated an SBOM.

Since this buildpack does not own/create a layer of its own, any SBOM the buildpack generates would be added to the launch SBOM.

Some initial questions to consider:

  • What use cases exist where an SBOM generated at this of the build would be valuable?
  • What performance penalties come with generating an SBOM in this buildpack?

incorrect detection of npm-start buildpacks leads to invalid entrypoint

What happened?

I am trying to build a simple nodejs application using the paketo and noticed that the npm-start buildpack sets an invalid entrypoint.

  • What were you attempting to do?

Build an image for https://github.com/shipwright-io/sample-nodejs/tree/main/source-build

  • What did you expect to happen?

Since the source directory does contain an app.js, I expect it to be the entrypoint to the application as described in the paketo documentation.

  • What was the actual behavior? Please provide log output, if possible.

The platform choses this buildpack which sets an invalid entrypoint to a non existing server.js.

===> ANALYZING
Previous image with name "node-bug-sample" not found
===> DETECTING
5 of 10 buildpacks participating
paketo-buildpacks/ca-certificates 3.0.1
paketo-buildpacks/node-engine     0.11.2
paketo-buildpacks/npm-install     0.6.2
paketo-buildpacks/node-module-bom 0.2.0
paketo-buildpacks/npm-start       0.6.1
===> RESTORING
===> BUILDING

Paketo CA Certificates Buildpack 3.0.1
  https://github.com/paketo-buildpacks/ca-certificates
  Launch Helper: Contributing to layer
    Creating /layers/paketo-buildpacks_ca-certificates/helper/exec.d/ca-certificates-helper
Paketo Node Engine Buildpack 0.11.2
  Resolving Node Engine version
    Candidate version sources (in priority order):
      package.json -> ">= 0.10.12"
      <unknown>    -> ""

    Selected Node Engine version (using package.json): 16.13.1

  Executing build process
    Installing Node Engine 16.13.1
      Completed in 6.701s

  Configuring build environment
    NODE_ENV     -> "production"
    NODE_HOME    -> "/layers/paketo-buildpacks_node-engine/node"
    NODE_VERBOSE -> "false"

  Configuring launch environment
    NODE_ENV     -> "production"
    NODE_HOME    -> "/layers/paketo-buildpacks_node-engine/node"
    NODE_VERBOSE -> "false"

    Writing profile.d/0_memory_available.sh
      Calculates available memory based on container limits at launch time.
      Made available in the MEMORY_AVAILABLE environment variable.

Paketo NPM Install Buildpack 0.6.2
  Resolving installation process
    Process inputs:
      node_modules      -> "Not found"
      npm-cache         -> "Not found"
      package-lock.json -> "Not found"

    Selected NPM build process: 'npm install'

  Executing build process
    Running 'npm install --unsafe-perm --cache /layers/paketo-buildpacks_npm-install/npm-cache'
      Completed in 845ms

  Configuring launch environment
    NPM_CONFIG_LOGLEVEL -> "error"

  Configuring environment shared by build and launch
    PATH -> "$PATH:/layers/paketo-buildpacks_npm-install/modules/node_modules/.bin"


Paketo Node Module Bill of Materials Generator Buildpack 0.2.0
  Resolving CycloneDX Node.js Module version
    Selected CycloneDX Node.js Module version: 3.0.7

  Executing build process
    Installing CycloneDX Node.js Module 3.0.7
      Completed in 254ms

  Configuring environment
    Appending CycloneDX Node.js Module onto PATH

  Running CycloneDX Node.js Module
    Running 'cyclonedx-bom -o bom.json'
      Completed in 316ms

Paketo NPM Start Buildpack 0.6.1
  Assigning launch processes
    web: node server.js

The error output when running the image:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/workspace/server.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Build Configuration

  • What platform (pack, kpack, tekton buildpacks plugin, etc.) are you
    using? Please include a version.

pack - 0.23.0

  • What buildpacks are you using? Please include versions.

paketo-buildpacks/ca-certificates 3.0.1
paketo-buildpacks/node-engine 0.11.2
paketo-buildpacks/npm-install 0.6.2
paketo-buildpacks/node-module-bom 0.2.0
paketo-buildpacks/npm-start 0.6.1

  • What builder are you using? If custom, can you provide the output from pack inspect-builder <builder>?

paketobuildpacks/builder:full

  • Can you provide a sample app or relevant configuration (buildpack.yml,
    nginx.conf, etc.)?

https://github.com/shipwright-io/sample-nodejs/tree/main/source-build and run $DIR/source-build$ pack build node-bug-sample --builder paketobuildpacks/builder:full

Checklist

  • I have included log output.
  • The log output includes an error message.
  • I have included steps for reproduction.

Further observations

The behaviour described in the paketo documentation is contributed by the node-start buildpack. The npm-start buildpack is falsely applied in this instance, because its detection criteria is the existence of a package.json file. However, that package.json file does not contain any start commands (pre-start, start...). Thus, it seems like the detection phase of this buildpack has to be extended to also check for a suitable start command. If not, then node-start should be preferred.

Add support for Reloadable Process Types

Background

Per the Reloadable Process Types RFC, we want to integrate the watchexec buildpack with npm-start to enable live reloading on k8s.

Acceptance Criteria

When BP_LIVE_RELOAD_ENABLED is set to true:

  • The buildpack should require watchexec at launch and,

  • set a start command for the container which uses watchexec to invoke the command that the buildpack would otherwise have set.

  • The watchexec start command should be configured such that it restarts the
    given process whenever files in the workspace are changed.

  • The watchexec start command should be configured such that it ignores changes
    to package.json, package-lock.json & node_modules/ so that Yarn processes which
    modify dependency management files or web frameworks which modify the
    node_modules cache on-the-fly (e.g. React, Vue) do not trigger a restart.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.