title | description | layout |
---|---|---|
JavaScript/Node.js |
Get started creating JavaScript apps with Node.js on {{< vendor/name >}}. |
single |
Node.js is a popular asynchronous JavaScript runtime. Deploy scalable Node.js apps of all sizes on {{< vendor/name >}}. You can also develop a microservice architecture mixing JavaScript and other apps with multi-app projects.
{{% major-minor-versions-note %}}
Grid and {{% names/dedicated-gen-3 %}} | {{% names/dedicated-gen-2 %}} |
---|---|
{{< image-versions image="nodejs" status="supported" environment="grid" >}} | {{< image-versions image="nodejs" status="supported" environment="dedicated-gen-2" >}} |
{{% language-specification type="nodejs" display_name="Node.js" %}}
To use a specific version in a container with a different language, use a version manager.
{{% deprecated-versions %}}
Grid | {{% names/dedicated-gen-2 %}} |
---|---|
{{< image-versions image="nodejs" status="deprecated" environment="grid" >}} | {{< image-versions image="nodejs" status="deprecated" environment="dedicated-gen-2" >}} |
To use JavaScript with Node.js on {{< vendor/name >}}, configure your app configuration (a complete example is included at the end).
Choose a version from the list of supported versions and add it to your app configuration:
{{< readFile file="registry/images/examples/full/nodejs.app.yaml" highlight="yaml" configFile="app" >}}
Add the following to your app configuration:
dependencies:
nodejs:
sharp: "*"
These are now available as commands, the same as installing with npm install -g
.
Include any commands needed to build and setup your app in the hooks
, as in the following example:
hooks:
build: |
npm run setup-assets
npm run build
Specify a command to start serving your app (it must be a process running in the foreground):
web:
commands:
start: node index.js
Make sure your Node.js application is configured to listen over the port given by the environment.
The following example uses the platformsh-config
helper:
// Load the http module to create an http server.
const http = require('http');
// Load the {{< vendor/name >}} configuration
const config = require('platformsh-config').config();
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "application/json"});
response.end("Hello world!");
});
// Listen on the port from the {{< vendor/name >}} configuration
server.listen(config.port);
A complete basic app configuration looks like the following:
name: node-app
type: nodejs:18
disk: 512
dependencies:
nodejs:
sharp: "*"
hooks:
build: |
npm run setup-assets
npm run build
web:
commands:
start: "node index.js"
By default, {{< vendor/name >}} assumes you're using npm as a package manager.
If your code has a package.json
, the following command is run as part of the default build flavor:
npm prune --userconfig .npmrc && npm install --userconfig .npmrc
This means you can specify configuration in a .npmrc
file in your app root.
To switch to Yarn to manage dependencies, follow these steps:
-
Turn off the default use of npm:
build: flavor: none
-
Specify the version of Yarn you want:
{ ... "packageManager": "yarn@3.2.1" }
What you do next depends on the versions of Yarn and Node.js you want.
{{< codetabs >}}
+++ title=Yarn 3.x and Node.js 16+ +++
-
Use Corepack to run Yarn in your build hook:
hooks: build: | corepack yarn install
<--->
+++ title=Yarn 3.x and Node.js 14 +++
-
Enable Corepack (which is opt-in):
dependencies: nodejs: corepack: "*"
-
Use Corepack to run Yarn in your build hook:
hooks: build: | corepack yarn install
<--->
+++ title=Yarn < 3 +++
-
Add Yarn as a global dependency:
dependencies: nodejs: yarn: "1.22.19"
-
Install dependencies in the
build
hook:hooks: build: | yarn --frozen-lockfile
{{< /codetabs >}}
The following examples show how to use Node.js to access various services. To configure a given service, see the page dedicated to that service.
{{< codetabs >}}
+++ title=Elasticsearch file=static/files/fetch/examples/nodejs/elasticsearch highlight=js +++
<--->
+++ title=Memcached file=static/files/fetch/examples/nodejs/memcached highlight=js +++
<--->
+++ title=MongoDB file=static/files/fetch/examples/nodejs/mongodb highlight=js +++
<--->
+++ title=MySQL file=static/files/fetch/examples/nodejs/mysql highlight=js +++
<--->
+++ title=PostgreSQL file=static/files/fetch/examples/nodejs/postgresql highlight=js +++
<--->
+++ title=Redis file=static/files/fetch/examples/nodejs/redis highlight=js +++
<--->
+++ title=Solr file=static/files/fetch/examples/nodejs/solr highlight=js +++
{{< /codetabs >}}
{{% config-reader %}}Node.js configuration reader library{{% /config-reader%}}
{{< repolist lang="nodejs" displayName="Node.js" >}}