Node.js: Difference between revisions

From NixOS Wiki
imported>Zauberpony
introduce direnv as antoher possibility
imported>Makefu
add packaging with yarn2nix
Line 1: Line 1:
__TOC__
__TOC__
{{expansion}}
{{expansion}}
== Packaging ==
=== Package with yarn2nix ===
yarn2nix uses the yarn nodejs tool to create a file called yarn.lock, which in return can be used by yarn2nix to generate a usable yarn expression.
This is what was needed to convert a small application server  [https://git.shackspace.de/rz/muellshack/tree/3be09715911628b164fa1cf346387555ca26a5b1 shackspace muellshack]:
<syntaxHighlight lang=console>
$ nix-shell -p yarn yarn2nix
$ yarn install
# creates yarn.lock
$ yarn2nix > yarn.nix
$ vim package.json
# add:    "bin": "app.js",
$ cat > default.nix <<EOF
with (import <nixpkgs> {});
rec {
  muellshack = mkYarnPackage {
    name = "muellshack";
    src = ./.;
    packageJSON = ./package.json;
    yarnLock = ./yarn.lock;
    yarnNix = ./yarn.nix;
  };
}
EOF
$ sed -i '1i#!/usr/bin/env node' app.js
$ chmod +x app.js
$ nix-build
$ result/bin/muellshack
</syntaxHighlight>
The complete diff can be found at [https://git.shackspace.de/rz/muellshack/commit/f5e498acd47695c4947dc1b5ddebfad2eee8d653 the respective diff]


== FAQ ==
== FAQ ==

Revision as of 20:02, 10 July 2019

Packaging

Package with yarn2nix

yarn2nix uses the yarn nodejs tool to create a file called yarn.lock, which in return can be used by yarn2nix to generate a usable yarn expression. This is what was needed to convert a small application server shackspace muellshack:

$ nix-shell -p yarn yarn2nix
$ yarn install
# creates yarn.lock
$ yarn2nix > yarn.nix
$ vim package.json
# add:    "bin": "app.js",
$ cat > default.nix <<EOF
with (import <nixpkgs> {});
rec {
  muellshack = mkYarnPackage {
    name = "muellshack";
    src = ./.;
    packageJSON = ./package.json;
    yarnLock = ./yarn.lock;
    yarnNix = ./yarn.nix;
  };
}
EOF
$ sed -i '1i#!/usr/bin/env node' app.js
$ chmod +x app.js
$ nix-build

$ result/bin/muellshack

The complete diff can be found at the respective diff

FAQ

Using npm install -g fails

The following errors are to be expected.

npm ERR! Error: EACCES: permission denied, access '/nix/store/00000000000000000000000000000000-nodejs-6.14.3/lib/node_modules'
npm ERR!     at Error (native)
npm ERR!  { Error: EACCES: permission denied, access '/nix/store/00000000000000000000000000000000-nodejs-6.14.3/lib/node_modules'
npm ERR!     at Error (native)
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/nix/store/00000000000000000000000000000000-nodejs-6.14.3/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

The store is read-only as it should be. Purity in Nix and NixOS makes it right not to allow installation using -g.

There are a couple solutions, none of them are strictly wrong. You can either configure npm so it installs globally to your home, or avoid using -g entirely. It is also possible, for node versions 8 and greater, to use npx.

Install to your home

This is done through configuring npm and amending your PATH.[1]

 $ npm set prefix ~/.npm-global

Then, amend your PATH so it looks into $HOME/.npm-global.

Avoid using -g

This is a bit harder to implement, but creates a bit more strictness in your environment; it will be impossible accidentally make use of what would have been a globally installed package. The idea is to install it to either a temporary transitory folder or to the project folder, then run the locally installed instance of the package, the binaries are found under node_packages/.bin/.[2]

 $ npm install uglify-es
[ ... ]

 $ ls -l node_modules/.bin/
total 0
lrwxrwxrwx 1 user users 25 Jul 17 15:34 uglifyjs -> ../uglify-es/bin/uglifyjs

 $ node_modules/.bin/uglifyjs --help
  Usage: uglifyjs [options] [files...]
direnv

If calling the executables with full path is too cumbersome, another elegant solution is to leverage direnv. Direnv is a shell-hook that can set directory-specific environment-variables. You basically add a file ".envrc" next to your "package.json" with the following content:

layout node

direnv then adds your "node_modules/.bin" to your path whenever you enter the directory. Please follow direnv's setup instructions on how to activate setup direnv in general.

Using npx

 $ nix-shell -p nodejs-8_x

 $ npx create-react-app --help
npx: installed 67 in 1.671s
  Usage: create-react-app <project-directory> [options]
[...]

External Links

References