Node.js: Difference between revisions
Turbotimon (talk | contribs) m →Example nix flake shell for Node.js development: is displayed wrong. Couldn't fix it but added this note |
|||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
[https://nodejs.org Node.js] is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code on the server side. Built on the V8 JavaScript engine, it enables the creation of scalable and high-performance applications, particularly for real-time web services. | [https://nodejs.org Node.js] is an open-source, cross-platform [[JavaScript]] runtime environment that allows developers to execute JavaScript code on the server side. Built on the V8 JavaScript engine, it enables the creation of scalable and high-performance applications, particularly for real-time web services. | ||
== Setup == | == Setup == | ||
| Line 13: | Line 13: | ||
=== Packaging with <code>buildNpmPackage</code> === | === Packaging with <code>buildNpmPackage</code> === | ||
From the [https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific Nixpkgs manual]: "<code>buildNpmPackage</code> allows you to package npm-based projects in Nixpkgs without the use of an auto-generated dependencies file (as used in node2nix). It works by utilizing npm’s cache functionality – creating a reproducible cache that contains the dependencies of a project, and pointing npm to it." | From the [https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific Nixpkgs manual]: "<code>buildNpmPackage</code> allows you to package npm-based projects in Nixpkgs without the use of an auto-generated dependencies file (as used in node2nix). It works by utilizing npm’s cache functionality – creating a reproducible cache that contains the dependencies of a project, and pointing npm to it." | ||
To better understand what happens under the hood and see the latest features [https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/node/build-npm-package/default.nix see the build-npm-package source]. | |||
Here's a <code>flake.nix</code> example to build a node package from the current directory. | |||
<syntaxhighlight lang="nix"> | |||
{ | |||
inputs = { | |||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; | |||
}; | |||
outputs = { | |||
self, | |||
nixpkgs, | |||
}: let | |||
pkgs = nixpkgs.legacyPackages."x86_64-linux"; | |||
in { | |||
packages."x86_64-linux".default = pkgs.buildNpmPackage { | |||
pname = "my-node-script"; | |||
version = "0.1.0"; | |||
src = ./.; | |||
npmDepsHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; | |||
}; | |||
}; | |||
} | |||
</syntaxhighlight> | |||
By default, the build phase runs the <code>build</code> script defined in <code>package.json</code>. | |||
<syntaxhighlight lang="json"> | |||
{ | |||
"scripts": { | |||
"build": "npm install" | |||
} | |||
} | |||
</syntaxhighlight> | |||
The binaries created in this package are defined by the <code>bin</code> key in <code>package.json</code>. This example will result in a <code>my-node-script</code> binary being created that basically runs <code>node main.js</code>. | |||
<syntaxhighlight lang="json"> | |||
{ | |||
"bin": { | |||
"my-node-script": "main.js" | |||
} | |||
} | |||
</syntaxhighlight> | |||
==== Packaging electron applications ==== | ==== Packaging electron applications ==== | ||
| Line 222: | Line 265: | ||
== Tips and tricks == | == Tips and tricks == | ||
=== Example nix flake shell for Node.js development === | === Example nix flake shell for Node.js development === | ||
`flake.nix | [[Flake]] example: (Note: the `${&lt;nixpkgs&gt;}` needs to be replaced by `${<nixpkgs>}` | ||
< | {{file|flake.nix|nix| | ||
<nowiki> | |||
{ | { | ||
description = "example-node-js-flake"; | description = "example-node-js-flake"; | ||
| Line 261: | Line 305: | ||
} | } | ||
</ | </nowiki> | ||
}} | |||
=== Using nodePackages with a different node version === | === Using nodePackages with a different node version === | ||