Skip to content

Latest commit

 

History

History
 
 

helia-typescript

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Helia logo

Building Helia with TypeScript


Explore the docs · Report Bug · Request Feature/Example

About

ECMAScript Modules are how JavaScript applications organise code into multiple files and import those files into other parts of the application. They became part of the language in 2015 and are now supported on all platforms.

Prior to this the de facto standard for JavaScript modules was CommonJS though this scheme was never adopted as a standard so remains a userland attempt to solve the problems of code modularisation.

As part of the standard, an import of an ES module returns a Promise whereas a require of a CJS module returns the module itself so unfortunately these two schemes are incompatible with each other. Most transpilation engines allow the use of import with CJS, however the reverse is not true.

The normal way to load one TypeScript module from another is to use the ESM-looking import statement, however by default it will output CJS code:

Config

{
  "compilerOptions": {
    "target": "ES2015",

    "moduleResolution": "node",
    "skipLibCheck": true,
    "outDir": "./dist"
  }
}

Source code

import { createHelia } from 'helia'

createHelia()
  .then(() => {
    console.info('Helia is running')
    console.info('PeerId:', helia.libp2p.peerId.toString())
  })

Compiled code

Object.defineProperty(exports, "__esModule", { value: true });
const helia_1 = require("helia");
(0, helia_1.createHelia)().then(() => {
    console.info('Helia is running');
    console.info('PeerId:', helia.libp2p.peerId.toString());
});

Helia is bundled using ESM and not CJS so this will fail at runtime:

% node dist/index.js
node:internal/modules/cjs/loader:544
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /path/to/project/node_modules/helia/package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:361:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:641:13)
    at resolveExports (node:internal/modules/cjs/loader:538:36)
    at Module._findPath (node:internal/modules/cjs/loader:607:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1033:27)
    at Module._load (node:internal/modules/cjs/loader:893:27)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/path/to/project/index.js:4:17) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

To fix this error, "module" and "target" must be set to at least "ES2015".

These are the very minimum versions that will build ESM and not CJS - your application may require something more recent depending on which JavaScript features you are using.

If in doubt, look at the compiled code your application generates. If you see use of the require function, you are building CJS and need to change your "module" setting.

Config

{
  "compilerOptions": {
    "module": "ES2015",
    "target": "ES2015",

    "moduleResolution": "node",
    "skipLibCheck": true,
    "outDir": "./dist"
  }
}

Source code

import { createHelia } from 'helia'

createHelia()
  .then(() => {
    console.info('Helia is running')
  })

Compiled code

import { createHelia } from 'helia';
createHelia()
    .then(helia => {
    console.info('Helia is running');
    console.info('PeerId:', helia.libp2p.peerId.toString());
});

Output

% node dist/index.js
Helia is running
PeerId: 12D3KooWNj6PKy8boHWnDi39NTEJKomw4u6gW9umfpfRNzJPCv7e

Usage

You can use tsc to build the project and then node to run it with:

% npm start

About The Project

Documentation

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the IPFS Project
  2. Create your Feature Branch (git checkout -b feature/amazing-feature)
  3. Commit your Changes (git commit -a -m 'feat: add some amazing feature')
  4. Push to the Branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Want to hack on IPFS?

The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:

Read the Code of Conduct and JavaScript Contributing Guidelines.

  • Check out existing issues The issue list has many that are marked as 'help wanted' or 'difficulty:easy' which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
  • Look at the Helia Roadmap This are the high priority items being worked on right now
  • Perform code reviews More eyes will help a. speed the project along b. ensure quality, and c. reduce possible future bugs
  • Add tests. There can never be enough tests