错误 [ERR_REQUIRE_ESM]:当 运行 脚本时必须使用导入来加载 ES 模块
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module when running script
我正在尝试测试 imagemin
& imagemin-mozjpeg
,所以我写了一个小项目来这样做。
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"
}
}
我运行:
yarn run build
yarn run start
并得到以下错误:
$ 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.
请指教我该如何解决,谢谢。
问题是各种打字稿配置的混合。启动了一个新项目,问题得到解决。
我正在尝试测试 imagemin
& imagemin-mozjpeg
,所以我写了一个小项目来这样做。
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"
}
}
我运行:
yarn run build
yarn run start
并得到以下错误:
$ 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.
请指教我该如何解决,谢谢。
问题是各种打字稿配置的混合。启动了一个新项目,问题得到解决。