Closed quentinadam closed 2 years ago
See https://github.com/paulmillr/noble-hashes/releases
Use lib/esm
Hello,
Thank you very much for the quick reply, and for pushing v0.4.3, but when changing the import to lib/esm, as follows:
import { keccak_256 } from '@noble/hashes/lib/esm/sha3';
console.log(keccak_256(new Uint8Array([1, 2, 3])));
I am getting the following issue :
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/esm/sha3' is not defined by "exports" in /Users/[redacted]/node_modules/@noble/hashes/package.json imported from /Users/[redacted]/src/test.mjs
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:429:9)
at packageExportsResolve (node:internal/modules/esm/resolve:683:3)
at packageResolve (node:internal/modules/esm/resolve:853:14)
at moduleResolve (node:internal/modules/esm/resolve:910:18)
at defaultResolve (node:internal/modules/esm/resolve:1005:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:475:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:245:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
at link (node:internal/modules/esm/module_job:78:36) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
Node.js v17.2.0
@jacogr ping
So you don't need to use @noble/hashes/lib/esm/sha3
just @noble/hashes/lib/sha3
- the reason it didn't work in 0.4.2 was because of the export map issue.
However, there is an issue where it doesn't find local imports, i.e. _u64
from sha3.js
- not sure why that didn't show up for me (and doesn't show up in my project usage where I swapped to 0.4.3 this morning), however can replicate this on a brand new project. Will push a PR after I verified it works with a local build.
Ok, so I would suggest reverting this (see https://github.com/paulmillr/noble-hashes/pull/18) until we can swap to tsc 4.6 which is not out yet - so 4.5 does add the correct relative import extensions in the outputs (assuming the module is set to node12
), however from 4.5.2 this is marked unstable so there is some work remaining by the team.
Background reading here - https://github.com/microsoft/TypeScript/issues/42813
(Can add it manually via script, but rather not and let the compiler just compile correctly)
PS: Still confused as to why I don’t see this issue where I swapped to 0.4.3 this morning in my libs, but it was a certainly an issue in the esm output as it stands.
Why do you map to lib/sha3 instead of lib/sha3.js? Can't we just add an extension in pkg.json
The issue is that with local imports for esm node always expects an extension -
will see if I can find some mapping solution
I believe you can put "./_u64.js" directly in the typescript code (the typescript compiler will ignore the .js extension and will link it up with the .ts file).
You are 100% right, that slipped my mind. Will adjust.
The only other small change then needed is a package.json in the lib/esm root containing only { “type”: “module” }
I see, that makes sense !
I am running the latest version of Node (v17.2.0) on Intel Mac OS X.
I have an ES Node.js package (with the "type":"module" flag), which is equivalent to having the code in an .mjs file.
Up until version 0.4.1, I used to do the following to use this package :
With version 0.4.2, I am now getting the following error :
I tried the suggestion :
But am getting the following error :
Changing my file extension to .cjs and using the old require() syntax works, but that somewhat defeats the purpose of trying to move to the new ES Module syntax.
This works (when running from .cjs file) :
Is there any possibilty to make this module work from an .mjs Node.js file ?