我是新手,对Node和JavaScript不熟悉,我试图使用以下库:https://github.com/Applifting/stv
我无法理解为什么会出现“Error: Cannot find module './stv'”错误。
我已经阅读了几个类似的问题和答案,但是没有一个建议的解决方案(例如重新安装Node和npm)能够帮助我。
以下是我用来设置package.json并安装stv模块的步骤:
C:\votecount>npm init -y
Wrote to C:\votecount\package.json:
{
"name": "votecount",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
C:\votecount>npm add stv
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN votecount@1.0.0 No description
npm WARN votecount@1.0.0 No repository field.
+ stv@0.0.2
added 1 package from 1 contributor and audited 1 package in 1.112s
found 0 vulnerabilities
package.json文件正确显示了"stv"在依赖项中。
"name": "votecount",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"stv": "0.0.2"
},
我随后创建了一个index.js文件,其中包含:
const stv = require('stv')
然后在运行index.js时,我得到了以下结果。
C:\votecount>node index.js
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module './stv'
Require stack:
- C:\votecount\node_modules\stv\dist\index.js
- C:\votecount\index.js
←[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:841:27)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:1025:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:72:18)←[39m
at Object.<anonymous> (C:\votecount\node_modules\←[4mstv←[24m\dist\index.js:3:13)
←[90m at Module._compile (internal/modules/cjs/loader.js:1137:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:985:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:1025:19)←[39m {
code: ←[32m'MODULE_NOT_FOUND'←[39m,
requireStack: [
←[32m'C:\\votecount\\node_modules\\stv\\dist\\index.js'←[39m,
←[32m'C:\\votecount\\index.js'←[39m
]
}
C:\votecount>node -v
v12.18.4
C:\votecount>npm -v
6.14.6
然而,当我尝试安装 Express 模块时,我没有遇到任何问题,可以成功地导入该模块。
const express = require('express')
const app = express();
由于 Express 看起来运行良好,我不确定该如何进一步排除故障。我可以看到我正在尝试使用的 stv 模块的一部分是用 TypeScript 编写的,但是特定的 stv package.json 似乎处理了这个问题。
非常感谢您提供有关故障排除的任何帮助。
编辑:
我也尝试按照 Git 说明使用 import { stv } from 'stv';
,但是遇到另一个错误:
C:\votecount>node index.js
(node:5432) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
C:\votecount\index.js:1
import { stv } from 'stv';
^^^^^^
SyntaxError: Cannot use import statement outside a module
如果我在根目录的package.json中添加"type": "module",那么就会得到:
C:\votecount>node index.js
(node:15736) ExperimentalWarning: The ESM module loader is experimental.
file:///C:/votecount/index.js:1
import { stv } from 'stv';
^^^
SyntaxError: The requested module 'stv' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'stv';
const { stv } = pkg;
将index.js更新为import pkg from 'stv'; const { stv } = pkg;
,然后返回原始错误信息"Error: Cannot find module './stv'"
import stv from 'stv';
来导入模块。 - fortymod