Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building NPM module fails to find local module #452

Open
rajinder-yadav opened this issue Feb 7, 2025 · 0 comments
Open

Building NPM module fails to find local module #452

rajinder-yadav opened this issue Feb 7, 2025 · 0 comments

Comments

@rajinder-yadav
Copy link

rajinder-yadav commented Feb 7, 2025

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/[email protected]/mod.ts:450:17)
    at build (https://deno.land/x/[email protected]/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/[email protected]/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");
  },
});
@marvinhagemeister marvinhagemeister transferred this issue from denoland/deno Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant