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

Address4 Not Found #147

Closed
rvalle opened this issue Oct 5, 2021 · 4 comments
Closed

Address4 Not Found #147

rvalle opened this issue Oct 5, 2021 · 4 comments

Comments

@rvalle
Copy link

rvalle commented Oct 5, 2021

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
Copy link
Author

rvalle commented Oct 5, 2021

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

@achingbrain
Copy link

achingbrain commented Nov 22, 2021

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
Copy link

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 added a commit to achingbrain/ip-address that referenced this issue Nov 23, 2021
This fixes two problems with the ESM build of this module.

1. The `package.json` that contains `{ "type": "module" }` wasn't being included in the npm tarball
2. When running in an ESM environment, `import foo from './bar'` does not work, you have to specify the extension

The fix for the first is simple, add the cjs/esm `package.json` files to the `files`
array in the project `package.json`.

The second fix is harder.  If you just add the `.js` extension to the source files,
typescript is happy but ts-node is not, and this project uses ts-node to run the
tests without a compile step.

Typescript does not support importing `*.ts` and will not support adding `*.js` to
the transpiled output - microsoft/TypeScript#16577

ts-node thought this was a bug in Typescript but it turns out not.  Their suggestion
to use `ts-node/esm` breaks sourcemap support because `source-map-support/register`
is not esm - TypeStrong/ts-node#783

There is a PR against ts-node to add support for resolving `./foo.js` if `./foo.ts`
or `./foo` fails but it seems to have stalled - TypeStrong/ts-node#1361

Given all of the above, the most expedient way forward seemed to just be to add
a shell script that rewrites the various `import` statements in the esm output
to add the `.js` extension, then if the ts-node PR ever gets merged the script
can be backed out.

Fixes beaugunderson#147
@beaugunderson
Copy link
Owner

this should work in 9.0.5 now

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

Successfully merging a pull request may close this issue.

3 participants