首页 > 解决方案 > 节点找不到模块

问题描述

我是 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 似乎可以处理这个问题。

任何有关解决此错误的帮助将不胜感激。

编辑: 我也尝试过import { stv } from 'stv';按照 git 说明使用,但出现另一个错误:

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 更新为import pkg from 'stv'; const { stv } = pkg;然后返回原始“错误:找不到模块 './stv'”

标签: javascriptnode.jsnpm

解决方案


软件包未正确发布。只有一个编译文件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')

对于永久解决方案,请在回购中提出问题。它将仅由图书馆所有者/维护者修复。


推荐阅读