node2nix
node2nix copied to clipboard
How to get node_modules?
Hi,
I read the docs and looked at nix files generated by node2nix and still didn't get it working.
My steps:
cat<<EOF | > node-packages.json
[ { "jquery": "3.6.0" } ]
EOF
nix-shell --pure -p pkgs.nodePackages.node2nix
node2nix -i node-packages.json
Following files are generated:
default.nix node-env.nix node-packages.nix
Further step is not very clear to me.
nix-build does nothing - node_modules folder doesn't appear.
So I am trying to play in repl with node-env functions, but I was not able to achieve the goal:
pkgs = import <nixpkgs> {}
nodeEnv = import ./node-env.nix { inherit (pkgs) stdenv lib python2 runCommand writeTextFile; inherit (pkgs) nodejs; inherit pkgs; libtool = null; }
mypacks = import ./node-packages.nix { inherit nodeEnv; inherit (pkgs) fetchurl nix-gitignore stdenv lib fetchgit; }
:b nodeEnv.buildNodeDependencies { name="myName"; packageName="myPackage"; version="1.1.1"; src=./src; dependencies=builtins.attrValues mypacks; }
:b nodeEnv.buildNodeDependencies { name="myName"; packageName="myPackage"; version="1.1.1"; src=./src; dependencies=builtins.attrValues mypacks; }
builder for '/nix/store/h8zdfxzv349mma0p5nb4ifablj8vnldb-node-dependencies-myName-1.1.1.drv' failed with exit code 1; last 8 log lines:
unpacking sources
patching sources
configuring
no configure script, doing nothing
building
installing
unpacking source archive /nix/store/635qclw2zmq0vw31wa8zaw67bhrmdx5f-jquery-3.6.0.tgz
cp: cannot stat '/nix/store/45g8xj78xq5j9z43wz6k7c18f64qvb6p-src/package.json': No such file or directory
error: build of '/nix/store/h8zdfxzv349mma0p5nb4ifablj8vnldb-node-dependencies-myName-1.1.1.drv' failed
Does node2nix take care of package.json?
There is an example in README file below, but I have error shell attribute is missing and I don't see a way how it could be there.
let
nodeDependencies = (pkgs.callPackage ./default.nix {}).shell.nodeDependencies;
in
stdenv.mkDerivation {
name = "my-webpack-app";
src = ./my-app;
buildInputs = [nodejs];
buildPhase = ''
ln -s ${nodeDependencies}/lib/node_modules ./node_modules
export PATH="${nodeDependencies}/bin:$PATH"
# Build the distribution bundle in "dist"
webpack
cp -r dist $out/
'';
}
Following config going in right direction, but jquery is removed at the end
with import <nixpkgs> { };
# pkgs = import <nixpkgs> {}
let
nodeEnv = callPackage ./node-env.nix {
nodejs = pkgs.nodejs-14_x;
};
mypacks = import ./node-packages.nix { inherit nodeEnv; inherit (pkgs) fetchurl nix-gitignore stdenv lib fetchgit; };
drv = nodeEnv.buildNodeShell { name="myName3"; packageName="myPackage"; version="1.1.1"; src=./.; dependencies=builtins.attrValues mypacks; };
in
drv.override {
buildInputs =[ which ];
shellHook = ''
echo node path = $NODE_PATH
'';
}
Log:
$ nix-shell
these derivations will be built:
/nix/store/6rbdsb9gqaqsfcyqbrvv8hfjpdz0ixr0-node-dependencies-myName3-1.1.1.drv
building '/nix/store/6rbdsb9gqaqsfcyqbrvv8hfjpdz0ixr0-node-dependencies-myName3-1.1.1.drv'...
unpacking sources
patching sources
configuring
no configure script, doing nothing
building
installing
unpacking source archive /nix/store/635qclw2zmq0vw31wa8zaw67bhrmdx5f-jquery-3.6.0.tgz
pinpointing versions of dependencies...
patching script interpreter paths in .
[email protected] /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1/myPackage/node_modules/jquery
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN myPackage No description
npm WARN myPackage No repository field.
npm WARN myPackage No license field.
removed 1 package in 0.115s
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1
the symlink /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1/bin is broken, it points to /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1/lib/node_modules/.bin (which is missing)
rewriting symlink /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1/bin to be relative to /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1
patching script interpreter paths in /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1
checking for references to /build/ in /nix/store/7n9sca2hs044kpk9lnx4sj9d06iyr9zp-node-dependencies-myName3-1.1.1...
I figured out how to generate node_modules in shell. I was naming npm dependency file in a wrong way. Instead of node-packages.json - package.json with regular npm format.
nix-shell -p pkgs.nodePackages.node2nix
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$ node2nix
fetching local directory: ./. from .
info attempt registry request try #1 at 12:25:23 PM
http request GET https://registry.npmjs.org/jquery
http 200 https://registry.npmjs.org/jquery
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$ ls
default.nix node-env.nix node-packages.nix package.json
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$ exit
dan@diehard:~/demo/halogen/counter-node2nix/sub2$ nix-shell -A shell --pure
these derivations will be built:
/nix/store/4avqarb3rb4hznaynbrqnbi1rvw9y2hd-node-dependencies-sub-1.0.0.drv
building '/nix/store/4avqarb3rb4hznaynbrqnbi1rvw9y2hd-node-dependencies-sub-1.0.0.drv'...
unpacking sources
patching sources
configuring
no configure script, doing nothing
building
installing
unpacking source archive /nix/store/635qclw2zmq0vw31wa8zaw67bhrmdx5f-jquery-3.6.0.tgz
pinpointing versions of dependencies...
patching script interpreter paths in .
No package-lock.json file found, reconstructing...
Adding metadata fields to: node_modules/jquery/package.json
[email protected] /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0/sub/node_modules/jquery
npm WARN [email protected] No description executeActionsf0363a7f48
npm WARN [email protected] No repository field.
up to date in 0.11s
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0
the symlink /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0/bin is broken, it points to /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0/lib/node_modules/.bin (which is missing)
rewriting symlink /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0/bin to be relative to /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0
patching script interpreter paths in /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0
checking for references to /build/ in /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0...
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$ ls
default.nix node-env.nix node-packages.nix package.json
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$ echo $NODE_PATH
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$ ls /nix/store/sds8agirgsg8yxj7k3czi4dd6awfkdwn-node-dependencies-sub-1.0.0/lib/node_modules
jquery
[nix-shell:~/demo/halogen/counter-node2nix/sub2]$
cat package.json
{
"name": "sub",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.6.0"
}
}
Perhaps the docs should clarify that nodeDependencies = (pkgs.callPackage ./default.nix {}).shell.nodeDependencies;
doesn't work when you use node-packages.json
(I'm not really even sure that's true though).