【发布时间】:2021-01-06 04:27:47
【问题描述】:
我是 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 模块的一部分是用打字稿编写的,但特定的 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'”
【问题讨论】:
标签: javascript node.js npm