ip-address icon indicating copy to clipboard operation
ip-address copied to clipboard

Address4 Not Found

Open rvalle opened this issue 2 years ago • 3 comments

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.

rvalle avatar Oct 05 '21 15:10 rvalle

however it does work with mocha imports... I am not sure what is wrong.

rvalle avatar Oct 05 '21 15:10 rvalle

For this module to work when imported from ESM environments it needs:

  1. A package.json in /dist/esm that just contains { "type": "module" }
  2. 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

achingbrain avatar Nov 22 '21 18:11 achingbrain

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.

achingbrain avatar Nov 22 '21 19:11 achingbrain

this should work in 9.0.5 now

beaugunderson avatar Sep 25 '23 23:09 beaugunderson