Skip to content

Commit d3e8ba5

Browse files
committed
feat: nodejs cookbook entry
1 parent 786c065 commit d3e8ba5

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

docs/cookbook/languages/nodejs.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,43 @@ description: Common questions and solutions for using Node.js with Flox
1010
Not only can you _develop_ your software with Flox, but you can _build_ it as well.
1111
See the [builds][build-concept] concept page for more details.
1212

13-
Node.js applications should have their `node_modules` directory placed under
14-
the `$out/lib` directory.
13+
### NPM
1514

16-
```toml
15+
Building Node.js packages with NPM looks similar to building docker containers or serverless functions.
16+
On a high level, builds for Node.js based projects generally follow this pattern:
17+
18+
```.toml
1719
[build.myproject]
1820
command = '''
19-
npm install --production --prefix=$out/lib
21+
# Install dependencies
22+
npm ci
23+
24+
# Build
25+
npm run build
26+
# -> assuming this yields a `dist` directory
27+
28+
# Install the build result to `out` # (1)!
29+
mkdir -p $out
30+
mv ./dist $out/
31+
32+
## If your app does not use a bundler
33+
## and needs additional node_modules at runtime,
34+
## `npm prune` and copy the node mudules to $out
35+
#
36+
# npm prune --include "prod"
37+
# cp -r ./node_modules $out
38+
39+
# Create a run script if the # (2)!
40+
mkdir -p $out/bin
41+
echo "#!/usr/bin/env sh"
42+
echo 'exec node $out/dist/index.js "$@"' >> $out/bin/myproject
43+
chmod 755 $out/bin/myproject
2044
'''
2145
```
2246

47+
1. If you expect to install multiple node applications in the same environment, we recommend putting the `dist` (and optinal `node_modules`) under an appropriate namespace, e.g. install them as `/libexec/myproject/dist`.
48+
49+
2. If your `npm build` already produces a binary that can be executed drectly, you can also copy or link that to `$out/bin`. Note that only binaries in `$out/bin` are wrapped to ensure they run within a consistent environment.
50+
51+
2352
[build-concept]: ../../concepts/manifest-builds.md

0 commit comments

Comments
 (0)