Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export multiple modules from NPM package

I have a rather large project A using Node and Typescript. In project A I have a lot of different modules that I would like to reuse in another project B.

Therefore I have built the project A with this tsconfig.json:

{
    "compilerOptions": {
        "target": "es2017",
        "module": "commonjs",
        "declaration": true,
        "outDir": "./dist",
        "sourceMap": true,
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "typeRoots": ["./node_modules/@types", "./modules/@types"]
    },
    "exclude": ["node_modules"]
}

So all the files are built into the /dist folder this way:

  • dist
    • moduleA.js
    • moduleA.map
    • moduleA.d.ts
    • moduleB.js
    • moduleB.map
    • moduleB.d.ts
    • ....

To use these moduleA and moduleB in another project, I add the following to the package.json in Project A:

    "name": "projectA",
    "version": "1.0.0",
    "description": "...",
    "main": "dist/moduleA.js",
    "typings": "dist/moduleA.d.ts",

I use yarn workspaces to access Project A as a package in Project B. But problem is that I can only access moduleA, when using import {ModuleA} from 'projectA' in my new project B? So how can I access more modules from ProjectA?

like image 558
Jolle Avatar asked Aug 15 '19 17:08

Jolle


People also ask

Can I have 2 module exports?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

How do I export multiple classes?

Use named exports to export multiple classes in JavaScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file. js' . You can have as many named exports as necessary in a file.

Can you export with module exports?

Declaring a module. exports object in a file specifies the values to be exported from that file. When exported, another module can import this values with the require global method.

How do I export multiple functions?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.


2 Answers

Would simply consolidating all exports in one index.ts do the trick for you?

package.json (projectA):

{
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  ...
}

index.ts (projectA):

// Adjust the relative import paths 
// and identifiers to your structure
export { ModuleA } from "./moduleA";
export { ModuleB } from "./moduleB";

Some module in projectB:

import {ModuleA, ModuleB} from 'projectA'
like image 149
ford04 Avatar answered Oct 02 '22 01:10

ford04


I believe what you are looking for is: https://nodejs.org/api/packages.html#packages_package_entry_points

Not clear if TypeScript currently supports it or not: https://github.com/microsoft/TypeScript/issues/33079

It does appear you can work around it though with something like this in your package.json:

{
    ...

    "main": "./dist/index.js",
    "types": "./dist/index.d.ts",
    "typesVersions": {
        "*": {
          "dist/index.d.ts": [ "dist/index.d.ts" ],
          "*": [ "dist/*" ]
        }
      },
    "exports": {
        ".": "./dist/index.js",
        "./": "./dist/"
    },

    ...
}
like image 27
Michael - Plaudit Design Avatar answered Oct 02 '22 00:10

Michael - Plaudit Design