Skip to content

Building NPM module fails to find local module #452

@rajinder-yadav

Description

@rajinder-yadav

Version: Deno 2.1.9

When building a NPM module with deno, it will error out and fail to find the local module.

deno run -A build_npm.ts 0.0.1"

Error

[dnt] Building project...
[dnt] Type checking ESM...
src/main.ts:6:17 - error TS2339: Property 'main' does not exist on type 'ImportMeta'.

6 if (import.meta.main) {
                  ~~~~
src/main_test.ts:2:30 - error TS2307: Cannot find module '@std/assert' or its corresponding type declarations.

2 import { assertEquals } from "@std/assert";
                               ~~~~~~~~~~~~~

error: Uncaught (in promise) Error: Had 2 diagnostics.
          throw new Error(`Had ${diagnostics.length} diagnostics.`);
                ^
    at getProgramAndMaybeTypeCheck (https://deno.land/x/dnt@0.37.0/mod.ts:450:17)
    at build (https://deno.land/x/dnt@0.37.0/mod.ts:343:17)
    at eventLoopTick (ext:core/01_core.js:177:7)
    at async file:///tmp/npm/myadd/build_npm.ts:5:1

deno.json

{
  "tasks": {
    "dev": "deno run --watch main.ts",
    "npm": "deno run -A build_npm.ts 0.0.1"
  },
  "imports": {
    "@std/assert": "jsr:@std/assert@1"
  }
}

main_test.ts

import { assertEquals } from "@std/assert";
import { myAdd } from "./main.ts";

Deno.test(function addTest() {
  assertEquals(myAdd(2, 3), 5);
});

main.ts

export function myAdd(a: number, b: number): number {
  return a + b;
}

// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
  console.log("Add 2 + 3 =", myAdd(2, 3));
}

build_npm.ts

import { build, emptyDir } from "https://deno.land/x/dnt@0.37.0/mod.ts";

await emptyDir("./npm");

await build({
  entryPoints: ["./main.ts"],
  outDir: "./npm",
  shims: {
    deno: true,
  },
  package: {
    name: "myapp",
    version: Deno.args[0],
    description:
      "Boolean function that returns whether or not parameter is the number 42",
    license: "MIT",
    repository: {
      type: "git",
      url: "git+https://github.com/lambtron/is-42.git",
    },
    bugs: {
      url: "https://github.com/lambtron/is-42/issues",
    },
  },
  postBuild() {
    Deno.copyFileSync("README.md", "npm/README.md");
  },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions