0

I'm trying to make an npm package written in deno usable in a react app built using create-react-app.

I have been able to build and publish it, but when using create-react-app, and then npm i my-package, running npm start gives many errors related to missing modules.

Module not found: Error: Can't resolve 'fs' in ...
Module not found: Error: Can't resolve 'ne' in ...
Module not found: Error: Can't resolve 'os' in ...
...

There is also this message:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

But I'm not really using these APIs, at least not that I'm aware of.

The full error log can be seen here:

https://gist.github.com/uriva/07680cdf3a560a6c6f1b2b2053d66a32

My npm package (written in deno) can be found here

https://github.com/uriva/portal

its build script:

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

const outDir = "./dist";

await emptyDir(outDir);

await build({
  typeCheck: false,
  entryPoints: ["./client/src/index.ts"],
  outDir,
  shims: {
    undici: true,
    webSocket: true,
    timers: true,
    deno: true,
    crypto: true,
  },
  package: {
    name: "message-portal",
    version: Deno.args[0],
    description: "Move messages without environment configuration.",
    license: "MIT",
    repository: {
      type: "git",
      url: "git+https://github.com/uriva/portal.git",
    },
    bugs: {
      url: "https://github.com/uriva/portal/issues",
    },
  },
});

after running my script, the resulting npm package.json looks like this:

{
  "module": "./esm/client/src/index.js",
  "main": "./script/client/src/index.js",
  "types": "./types/client/src/index.d.ts",
  "name": "message-portal",
  "version": "0.0.4",
  "description": "Move messages without environment configuration.",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/uriva/portal.git"
  },
  "bugs": {
    "url": "https://github.com/uriva/portal/issues"
  },
  "exports": {
    ".": {
      "import": {
        "types": "./types/client/src/index.d.ts",
        "default": "./esm/client/src/index.js"
      },
      "require": {
        "types": "./types/client/src/index.d.ts",
        "default": "./script/client/src/index.js"
      }
    }
  },
  "scripts": {
    "test": "node test_runner.js"
  },
  "dependencies": {
    "@deno/shim-crypto": "~0.3.1",
    "@deno/shim-timers": "~0.1.0",
    "undici": "^5.14.0",
    "ws": "^8.12.0"
  },
  "devDependencies": {
    "@types/node": "^18.11.9",
    "chalk": "^4.1.2",
    "@types/ws": "^8.5.4"
  }
}
Uri
  • 25,622
  • 10
  • 45
  • 72

1 Answers1

0

Your package.json needs to include any dependencies that your lib uses, as using the library also requires an application to import from those libraries.

I'd suggest to read https://stackoverflow.com/a/69671222/251961.

paddotk
  • 1,359
  • 17
  • 31
  • but `dnt` is creating the package.json... – Uri Mar 16 '23 at 11:00
  • Oh sorry, I didn't realize that. I don't see it in your repository though? What does the outputted package.json look like? – paddotk Mar 16 '23 at 11:39
  • I've added it to the question – Uri Mar 16 '23 at 13:21
  • I'm not sure, but I would guess that there are one or more packages missing in your dependencies. I suggest to create another create-react-app app (without deno) to see what package.json lists there. As for the polyfills error, it looks like one or more `babel` pacakges are missing. – paddotk Mar 19 '23 at 16:21
  • Perhaps you could also find out what package throws those "Can't resolve" errors by debugging, if you haven't already, that might help – paddotk Mar 19 '23 at 16:22