我刚刚安装了:npm i node-id3
,来自:https://www.npmjs.com/package/node-id3。讽刺的是,我无法使用这个包。我正在运行Node.js v18.14.0.
根据链接:
const NodeID3 = require('node-id3')
NodeID3.read(file, function(err, tags) {})
结果如下:
const NodeID3 = require('node-id3')
^
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:UsersrootDocumentsGitHubDatabasepackage.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
错误信息告诉您Node正在使用ES模块,因为在您的package.json
中type
属性设置为module
Node.js有两个模块系统:CommonJS模块和ECMAScript模块。
CommonJS使用require
, ES模块使用import
// commmonjs
const NodeID3 = require('node-id3');
// ES modules
import NodeID3 from 'node-id3';
你有几个选择:
- 如果你想继续使用ESM,把你的
require
改成import
- 要使用CommonJS,要么从包中删除
type: "module"
行。或更改您的javascript文件的扩展名从.js
到.cjs
指示节点将其视为CommonJS脚本