简体   繁体   中英

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module when running script

I'm trying to test imagemin & imagemin-mozjpeg , so I wrote a small project to do so.

index.ts

import imagemin from 'imagemin';
import imageminMozjpeg from 'imagemin-mozjpeg';

(async () => {
  const files = await imagemin(["your-image.jpg"], {
    destination: "compressed-images",
    plugins: [
      imageminMozjpeg({quality: 50})
    ]
  });
  console.log(JSON.stringify(files, null, 1));
})();

main.ts

require = require('esm')(module/* , options*/);
module.exports = require('./index');

Package.json

{
"name": "1234",
"version": "1.0.0",
"revision": {
  "build_ts": 0,
  "git_commit": "dummy"
},
"description": "",
"main": "dist/main.js",
"author": "",
"license": "ISC",
"repository": {
  "type": "git",
  "url": ""
},
"scripts": {
  "start": "node ./dist/main.js",
  "build": "yarn build:js",
  "build:prod": "yarn run build:js",
  "build:types": "tsc --emitDeclarationOnly",
  "build:js": "babel . --out-dir ./dist --extensions \".ts\" --ignore node_modules --ignore dist --source-maps",
  "build-ts": "tsc",
  "test": "jest --coverage --verbose",
  "lint-test": "npm run lint && npm run test",
  "lint": "eslint -c .eslintrc.js --ext .ts ./src",
  "lint:fix": "eslint --fix -c .eslintrc.js --ext .ts ./src",
  "tslint": "tslint -c tslint.json --project tsconfig.json",
  "prettier": "prettier --write ./src/**/*.ts"
},
"dependencies": {
  "esm": "^3.2.25",
  "imagemin": "^8.0.0",
  "imagemin-mozjpeg": "^9.0.0"
},
"devDependencies": {
  "@babel/cli": "7.13.10",
  "@babel/core": "7.13.10",
  "@babel/plugin-proposal-class-properties": "7.13.0",
  "@babel/plugin-proposal-decorators": "7.13.5",
  "@babel/plugin-proposal-object-rest-spread": "7.13.8",
  "@babel/plugin-transform-runtime": "7.13.10",
  "@babel/preset-env": "7.13.10",
  "@babel/preset-typescript": "7.13.0",
  "@babel/runtime": "7.13.10",
  "@types/imagemin": "^7.0.1",
  "@types/imagemin-mozjpeg": "^8.0.1",
  "@typescript-eslint/eslint-plugin": "4.17.0",
  "@typescript-eslint/eslint-plugin-tslint": "4.17.0",
  "@typescript-eslint/parser": "4.17.0",
  "babel-plugin-transform-typescript-metadata": "0.3.2",
  "eslint": "7.21.0",
  "eslint-config-prettier": "^6.11.0",
  "eslint-plugin-jest": "^23.18.0",
  "eslint-plugin-jsdoc": "32.2.0",
  "prettier": "2.0.5",
  "ts-jest": "26.5.3",
  "ts-node": "^9.1.1",
  "ts-node-dev": "1.1.6",
  "tslint": "^6.1.3",
  "typescript": "4.2.3"
}


}

I run:

yarn run build
yarn run start

and get the following error:

$ node ./dist/main.js
/testing-project/node_modules/imagemin/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /testing-project/node_modules/imagemin/index.js
require() of ES modules is not supported.
require() of /testing-project/node_modules/imagemin/index.js from /testing-project/dist/index.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 /testing-project/node_modules/imagemin/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /testing-project/node_modules/imagemin/package.json.

Please advise on how I can resolve this, thank you.

The issue was a mix of various typescript configurations. Started a new project and issue was resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM