Must use import to load ES module on installed library

Viewed 1213

I've built a library, and have installed and imported it into project, and when I try to run the imported module:

import buildSitemap from 'react-build-sitemap'

I'm receiving the following error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/corey/work/CloudHospital.Front/node_modules/react-build-sitemap/index.js
require() of ES modules is not supported.
require() of /home/corey/work/CloudHospital.Front/node_modules/react-build-sitemap/index.js from /home/corey/work/CloudHospital.Front/build/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/corey/work/CloudHospital.Front/node_modules/react-build-sitemap/package.json.

I've attempted to remove "type": "module" from the package.json in the imported library, and I still receive the same error. Not really sure what to do to fix this issue with importing (the file I'm importing into is a typescript file if that makes any difference -- it shouldn't, as everything is properly typed for the library I'm importing):

From the library's index.js:

import babelParser from "@babel/parser";
import fs from "fs";
import PropTypes from "prop-types";
import { warn } from "console";

And the package.json:

{
  "name": "react-build-sitemap",
  "version": "0.1.9",
  "description": "simple library that generates a sitemap from your react router",
  "main": "./index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "babel-node index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lanierc/react-build-sitemap.git"
  },
  "keywords": [
    "react",
    "react-router",
    "sitemap",
    "build",
    "generate"
  ],
  "author": "Corey Lanier <corey@icloudhospital.com>",
  "license": "GPL-3.0-or-later",
  "bugs": {
    "url": "https://github.com/lanierc/react-build-sitemap/issues"
  },
  "homepage": "https://github.com/lanierc/react-build-sitemap#readme",
  "dependencies": {
    "fs": "0.0.1-security",
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-router": "^5.2.0"
  },
  "peerDependencies": {
    "react": "^16.13.1",
    "react-router": "^5.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.11.1",
    "@babel/node": "^7.10.5",
    "@babel/parser": "^7.11.3",
    "@babel/plugin-syntax-jsx": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "babel-plugin-jsx": "^1.2.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "jsx": "^0.9.89"
  }
}

This is obviously something I've done wrong somewhere within the library I built; it's the first time I have built my own library and thus mistakes will probably have been made.

0 Answers
Related