diff --git a/incubator/nodejs/README.md b/incubator/nodejs/README.md index 809ef90f8..aac408bfd 100644 --- a/incubator/nodejs/README.md +++ b/incubator/nodejs/README.md @@ -40,6 +40,20 @@ Templates are used to create your local project and start your development. When **NOTE:** Currently the `appsody deploy` command only works for deploying web applications. +## Customizing the build + +Simple Node projects do not require a 'build' process: they are executed directly from source. However, some technologies such as TypeScript require commands to be invoked to prepare the project ready to be run. + +If your project's dependencies require an additional build step, you can do so as follows: + +- In your `package.json`, edit the `"scripts"` section and define two new scripts: + - `"build": "npm install && "` + - `"prune": " && npm prune"` + +When initially launching your app using `appsody run`, or building the production image with `appsody build`, the 'build' script is executed (if one exists). Your build script should invoke `npm install` if any dependencies specified in your `devDependencies` are required as part of the build process - such as command line tools. + +When generating the production image, the 'prune' script is executed after that, if it exists, and is run with the `--production` flag. This allows the removal of development dependencies that were required during the 'build' step. This could be as simple as running the `npm prune` command, which inherits the production flag, and restores the dependencies to a production state. You may also use this step to clean up any build artifacts that are not necessary in your production image. + ## License This stack is licensed under the [Apache 2.0](./image/LICENSE) license diff --git a/incubator/nodejs/image/Dockerfile-stack b/incubator/nodejs/image/Dockerfile-stack index a35d24f6a..ef094b516 100644 --- a/incubator/nodejs/image/Dockerfile-stack +++ b/incubator/nodejs/image/Dockerfile-stack @@ -8,7 +8,7 @@ ENV APPSODY_WATCH_DIR=/project/user-app ENV APPSODY_WATCH_IGNORE_DIR=/project/user-app/node_modules ENV APPSODY_WATCH_REGEX="^.*.js$" -ENV APPSODY_PREP="npm install" +ENV APPSODY_PREP="npm install && npm run build --if-present" ENV APPSODY_RUN="npm start --node-options --require=appmetrics-dash/attach" ENV APPSODY_RUN_ON_CHANGE="npm start --node-options --require=appmetrics-dash/attach" diff --git a/incubator/nodejs/image/project/Dockerfile b/incubator/nodejs/image/project/Dockerfile index 97313a958..89e60f2b7 100644 --- a/incubator/nodejs/image/project/Dockerfile +++ b/incubator/nodejs/image/project/Dockerfile @@ -8,7 +8,19 @@ RUN rm -rf /project/user-app/node_modules && mkdir -p /project/user-app/node_mod # Install user-app dependencies WORKDIR /project/user-app -RUN npm install --production +RUN npm install --production + +# Run a build phase. Projects that need to execute build commands can customize the +# 'build' script in their package.json. The build script should call 'npm install'. +# If no build is required, just install production dependencies. +RUN npm run build --if-present + +# Uninstall dev dependencies, leaving only production dependencies. Projects can +# customize the 'prune' script in their package.json. +# Ideally this would just be 'npm prune', but this command does not have pre/post +# hooks. Instead, the user's 'prune' script can itself call 'npm prune' in addition +# to any additional actions required. +RUN npm run prune --production --if-present # Creating a tar to work around poor copy performance when using buildah 1.9.0 RUN cd / && tar czf project.tgz project @@ -16,6 +28,9 @@ RUN cd / && tar czf project.tgz project # Copy the dependencies into a slim Node docker image FROM node:12-slim +# Install common dependencies (TLS and CA support) +RUN apt-get update && apt-get install -y libssl1.1 ca-certificates && apt-get clean + # Copy project with dependencies COPY --chown=node:node --from=0 /project.tgz . RUN tar xf project.tgz && chown -R node:node project && rm project.tgz diff --git a/incubator/nodejs/stack.yaml b/incubator/nodejs/stack.yaml index 242d9b937..7d405d790 100644 --- a/incubator/nodejs/stack.yaml +++ b/incubator/nodejs/stack.yaml @@ -1,5 +1,5 @@ name: Node.js -version: 0.3.7 +version: 0.4.0 description: Runtime for Node.js applications license: Apache-2.0 language: nodejs