2

I created a new node project using

npm init
npm install apollo-server

I then added a index.js file with the following code:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server.listen().then(({ url }) => {
  console.log(` Server ready at ${url}`)
});

While I can run this with node index.js, how could I actually create a build from my index.js so it doesn't directly need the modules during runtime? (goal: deploy it on platforms like e.g. render.com)

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

2 Answers2

4

It sounds like you want to create a single executable artifact which requires no server-side configuration or setup to run.

There are a few options for this. You're probably looking for a Javascript bundler, like Rollup, Parcel or Webpack. Webpack is the most widely used, but also generally the most difficult to configure.

Using bundlers

Parcel

Install Parcel with npm i -g parcel, then, add this to your package.json:

  "main": "dist/index.js",
  "targets": {
    "main": {
      "includeNodeModules": true
    },
  },

Then run parcel build index.js. That's it!

I've added a simple demo on GitHub.

Webpack

There are a number of great answers on this SO question.

Bundler caveats

Many node packages come with binary/native addons. For example, Cypress downloads and installs a browser. Any package which uses native addons will not work with a bundler, as the bundler is unable to add the binary files. These packages will still need to be installed.

Another option: building a binary

In the processes above, your output artifact is a single Javascript file. Instead of a Javascript file, you could also produce a binary file and thus alleviate the need to install the Node runtime. Check out pkg to do this. While pkg is a fairly mature product, this is generally still considered an experimental technology, so perhaps not suited for a production environment. On the other hand, you shouldn't run into any problems with packages that include native addons. Read the documentation to see if it's appropriate for your use case.

Codebling
  • 10,764
  • 2
  • 38
  • 66
2

https://github.com/vercel/ncc this might help. it includes node_modules in the final artifact

Ahmed Nawaz Khan
  • 1,451
  • 3
  • 20
  • 42
  • Hah you added that just as I was editing to include pkg in my answer. It looks like pkg is the more mature and stable between ncc and pkg, and ncc is just the newer one. Have you used both? – Codebling Mar 25 '22 at 06:57
  • 1
    :). yeah I was thinking whats going on for a while. well haven't used pkg. but tried vercel/ncc. really easy to setup and supports typescript out of the box – Ahmed Nawaz Khan Mar 25 '22 at 07:01
  • 1
    Thank you, I accepted the other answer as it provided more informations, but that also looks like a nice tool that should work without much or any configuration. – stefan.at.kotlin Mar 26 '22 at 19:11