我想在项目中使用 browser-image-hash npm 包。
为了设置我所做的项目:
然后我使用以下代码添加了一个index.ts文件:
import { Hash } from "browser-image-hash";
const srcHash = new Hash(
"0111011001110000011110010101101100110011000100110101101000111000"
).toString();
console.log(srcHash);
我向 package.json 添加了一些脚本,使其如下:
{
"name": "image_matching_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc -b",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "npm run build && npm run start"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"browser-image-hash": "^0.0.5",
"typescript": "^5.6.3"
}
}
执行
npm run dev
后,我收到以下错误:
export { default as DifferenceHashBuilder } from './DifferenceHashBuilder';
^^^^^^
SyntaxError: Unexpected token 'export'
以下是我的
tsconfig.json
文件:
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
收到此错误后,我在 package.json 中添加了
"type": "module"
字段,但仍然收到以下错误:
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
我还将 tsconfig.json 中的模块键更改为
es2015
而不是 commonjs
,但随后模块本身变得可识别:
Cannot find module 'browser-image-hash'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
1 import { Hash } from "browser-image-hash";
将其设置为
es6
也给出了相同的错误。将其设置为 nodenext
也没有帮助。
我在这里缺少什么?
您需要将
moduleResolution
添加到 compilerOptions
数组中,并将其设置为 nodenext
,还要确保将 commonjs
(您为“模块”定义的)值更改为 nodenext
。添加后,该块应该看起来像,
{
"compilerOptions": {
"target": "es2016",
"module": "nodenext",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}