ip-address
ip-address copied to clipboard
Address4 Not Found
I understand this library works with both ESM and CommonJS module format.
I just cannot make it work as ESM from node14, here is a minimal test:
//test.mjs
import {describe, it} from "mocha";
import chai from "chai";
const {assert}=chai;
import {Address4} from "ip-address"
describe("Will test IP Address",function(){
it("Will test lookback", function(){
const loopback=new Address4("127.0.0.0");
assert(loopback.isCorrect());
})
});
This is what I get:
import {Address4} from "ip-address"
^^^^^^^^
SyntaxError: Named export 'Address4' not found. The requested module 'ip-address' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'ip-address';
const {Address4} = pkg;
There must be something I am missing.
however it does work with mocha imports... I am not sure what is wrong.
For this module to work when imported from ESM environments it needs:
- A
package.json
in/dist/esm
that just contains{ "type": "module" }
- Every import under
/dist/esm
to that references a local file to use the".js"
extension
e.g. in /dist/esm/ip-address.js
:
import { Address4 } from './lib/ipv4';
// change to
import { Address4 } from './lib/ipv4.js';
cc @beaugunderson
That first point is almost addressed by running .fix-package-types.sh
during the build, but "dist/**/*.json"
or something similar needs adding to the "files"
array in package.json
otherwise it the package.json
files don't make it into the npm tarball.
this should work in 9.0.5 now