我是node和javascript的新手,我正在尝试利用以下库:https://github.com/Applifting/stv
我无法弄清楚为什么我总是得到以下“错误:找不到模块'./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 [email protected] No description
npm WARN [email protected] No repository field.
+ [email protected]
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();
正如表达似乎很好,我不确定如何进一步解决此问题。我可以看到我正在尝试使用的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;
,然后返回原始的“错误:找不到模块'./stv'” 最佳答案
该软件包未正确发布。只有一个编译文件dist/index.js
。应该有更多的文件。
examples index.d.ts index.js model.d.ts model.js stv.d.ts stv.js stv.spec.d.ts stv.spec.js utils.d.ts utils.js
一种解决方案是-stv
tsc
然后
const stv = require('./stv/dist')
对于永久解决方案,请在回购中提出问题。它将仅由图书馆所有者/维护者修复。