Why does this js import fail in a nix build but succeed in a nix devshell?

I have a flake.nix with this derivation in the outputs:

packages.svg = pkgs.stdenv.mkDerivation rec {
  name = "generate-svg";
  src = self;
  buildInputs = [ pkgs.nodePackages.nodejs ];
  buildPhase = ''
    export NODE_PATH=${src}/node_modules:${src}
    node ${src}/generate.js
  '';

  installPhase = ''
    mkdir -p $out
    cp output.svg $out/
  '';
};

You can see the whole file (and others) here: https://github.com/MatrixManAtYrService/buildsvg

My goal is that the following command should run generate.js which will use d3.js to build a svg file defined by the contents of generate.js.

$ nix build 'github:MatrixManAtYrService/buildsvg#svg'
$ ls result
    output.svg

Instead, the nix build command fails with this error:

Error: Cannot find package '/nix/store/a76p6qgrxlx0safvxrqx5aknhszvg703-source/node_modules/opentype.js/package.json' imported from /nix/store/a76p6qgrxlx0safvxrqx5aknhszvg703-source/generate.js
    at legacyMainResolve (node:internal/modules/esm/resolve:215:26)
    at packageResolve (node:internal/modules/esm/resolve:841:14)
    at moduleResolve (node:internal/modules/esm/resolve:927:18)
    at defaultResolve (node:internal/modules/esm/resolve:1157:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
Node.js v20.12.2

The top of generate.js looks like this:

import fs from 'fs';
import * as d3 from 'd3';
import opentype from 'opentype.js';  // <--- failure is here
import D3Node from 'd3-node';

So I know that some of my modules are being imported correctly.
Also, If I use a nix devshell, it works fine:

devShells.default = pkgs.mkShell {
  buildInputs = [
    pkgs.nodePackages.nodejs
  ];
  shellHook = ''
    export NODE_PATH=$PWD/node_modules
  '';
};

$ nix develop
$ node generate.js
SVG generated successfully.

My package.json indicates these dependencies, and I’ve run npm install which caused them to be downloaded into node_modules (pardon if this is obvious, I’m a nodejs newbie).

I have sprinkled ls statements into the buildPhase and verified that /nix/store/a76p6qgrxlx0safvxrqx5aknhszvg703-source/node_modules/opentype.js/package.json does indeed exist, despite what the error says.

Why is my nix build failing, while the same build works in a devshell?

I have a habit of copying .gitignore files from other projects. In this case I mistakenly copied a .gitignore from a python project which included these lines:

dist/
bin/

This caused nix to omit certain files when it copied my project into the nix store for the build:

node_modules/opentype.js/bin/ot
node_modules/opentype.js/bin/server.js
node_modules/opentype.js/bin/test-render
node_modules/opentype.js/dist/opentype.js
node_modules/opentype.js/dist/opentype.js.map
node_modules/opentype.js/dist/opentype.min.js
node_modules/opentype.js/dist/opentype.min.js.map
node_modules/opentype.js/dist/opentype.module.js
node_modules/opentype.js/dist/opentype.module.js.map

So opentype.js‘s package.json was present in the nix store, but the above parts of that package were missing. This caused the import to fail.

It’s unfortunate that the error message did not indicate the files which were actually missing, but instead indicates package.json which was present.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật