skip to Main Content

I’m trying to publish an npm esm module that exports two methods. When the module is installed and imported I want the user to be able to import it like:

import { convertCsvToWorkbook, createFlatfile } from 'csvtoflatfile';
console.log(convertCsvToWorkbook, createFlatfile);

// or

import csvtoflatfile from 'csvtoflatfile';
console.log(csvtoflatfile)

It works locally in my demo but when installing the module from npm I get an error: Module ‘"csvtoflatfile"’ has no exported member ‘convertCsvToWorkbook’.ts(2305)

enter image description here

This import does sort of work, but I can’t actually seem to access the methods on it:

enter image description here


I’m exporting the methods individually here in my index.ts, which looks like:

export const convertCsvToWorkbook = () => {}
export const createFlatfile= () => {}

export default {
    convertCsvToWorkbook,
    createFlatfile,
};

How can I fix my exports so that they can be properly imported when downloaded through npm?

2

Answers


  1. // you are already exported as `named export` here
    export const convertCsvToWorkbook = () => {}
    export const createFlatfile= () => {}
    
    // you don't to do `default export` here
    export default {
        convertCsvToWorkbook,
        createFlatfile,
    };
    
    Login or Signup to reply.
  2. I think the issue is from the typing setup on your "types": "./@types/index.d.ts" package.json which points to a fixed type file which is supposed to be the generated one as you build the code.

    Hence, I would suggest to remove this setup and TS will help you to look up your index.d.ts in the same your index file dist/index.js

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search