Node.js: Difference between revisions
Move packaging contents to "Packaging" section |
m update link to homepage-dashboard package |
||
| (4 intermediate revisions by 4 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 218: | Line 261: | ||
</syntaxhighlight>You can take a look at what fonts are available in the Nix <code>google-fonts</code> package by calling:<syntaxhighlight lang="shell"> | </syntaxhighlight>You can take a look at what fonts are available in the Nix <code>google-fonts</code> package by calling:<syntaxhighlight lang="shell"> | ||
ls -ahl $(nix build --no-link --print-out-paths nixpkgs#google-fonts)/share/fonts/truetype/ | ls -ahl $(nix build --no-link --print-out-paths nixpkgs#google-fonts)/share/fonts/truetype/ | ||
</syntaxhighlight>Take a look at https://github.com/NixOS/nixpkgs/blob/ | </syntaxhighlight>Take a look at [https://github.com/NixOS/nixpkgs/blob/8358fd43a66594d8b3445d87006185fa76d4be6e/pkgs/by-name/ho/homepage-dashboard/package.nix homepage-dashboard package in nixpkgs] for further workarounds for Nextjs in Nix. | ||
== 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 === | ||
| Line 311: | Line 356: | ||
=== References === | === References === | ||
[[Category:JavaScript]] | |||