我是 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 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
如果我然后将 "type": "module"添加到根目录中的 package.json,我将得到:
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 以 从 'stv' 导入 pkg; const { stv } = pkg;
然后返回原来的“Error: Cannot find module './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
一个解决方案是-
- fork repo
- 将其克隆到您的项目中
- cd 进入
stv
- 安装模块
- 运行
tsc
然后
const stv = require('./stv/dist')
对于永久解决方案,请在 repo 中提出问题。它将仅由库所有者/维护者修复。
关于javascript - Node 找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63969188/