简体   繁体   English

节点找不到模块

[英]Node Cannot Find Modules

I am new to node and javascript and I'm trying to utilise the following library: https://github.com/Applifting/stv我是 node 和 javascript 的新手,我正在尝试使用以下库: https : //github.com/Applifting/stv

I am unable to work out why I keep get the following "Error: Cannot find module './stv'"我无法弄清楚为什么我一直收到以下“错误:找不到模块 './stv'”

I've read several similar questions and responses but none of the suggested solutions, such as reinstalling node and npm, have helped.我已经阅读了几个类似的问题和回复,但是建议的解决方案(例如重新安装 node 和 npm)都没有帮助。

Below is the setup I went through to set up the package.json and install the stv module.下面是我用来设置 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

The package.json correctly shows stv under the dependencies. 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"
  },

I then created an index.js file containing:然后我创建了一个 index.js 文件,其中包含:

const stv = require('stv')

Then when running index.js I get the following.然后在运行 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

However when I try this process installing the express module I don't have any problems and can import the module succesfully.但是,当我尝试安装 express 模块的这个过程时,我没有任何问题并且可以成功导入模块。

const express = require('express')
const app = express();

As express seems to work fine I'm not sure how to go about troubleshooting this any further.由于 express 似乎工作正常,我不确定如何进一步解决此问题。 I can see that part of the stv module I am trying to use has been written in typescript but the specific stv package.json seems to handle this.我可以看到我尝试使用的 stv 模块的一部分是用打字稿编写的,但特定的 stv package.json 似乎可以处理这个问题。

Any assistance with troubleshooting this error would be greatly appreciated.任何帮助解决此错误的帮助将不胜感激。

Edit: I've also tried using import { stv } from 'stv';编辑:我也试过使用import { stv } from 'stv'; as per the git instructions but get another error:按照 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

If I then add "type": "module" to the package.json in the root directory I then get:如果我然后将 "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;

Updating index.js to import pkg from 'stv'; const { stv } = pkg;更新 index.js 以import pkg from 'stv'; const { stv } = pkg; import pkg from 'stv'; const { stv } = pkg; then returns the original "Error: Cannot find module './stv'"然后返回原来的“错误:找不到模块'./stv'”

The package is not published properly.包未正确发布。 There is only one compiled file dist/index.js .只有一个编译文件dist/index.js There should have been more files.应该有更多的文件。

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

One solution is-一种解决方案是——

  • Fork the repo分叉回购
  • Clone it into your project将其克隆到您的项目中
  • cd into stv cd到stv
  • install modules安装模块
  • run tsc运行tsc

Then然后

const stv = require('./stv/dist')

For permanent solution raise issue in the repo.对于永久解决方案,在回购中提出问题。 It will be fixed by library owners/maintainers only.它将仅由库所有者/维护者修复。

Looks like the stv package has been done with typescript and has not been properly transpiled to javascript before publishing.看起来stv包已经用typescript完成了,并且在发布之前没有正确地转换为javascript

Check if libre-stv does what you need, or if any other package works.检查libre-stv是否满足您的需求,或者任何其他软件包是否有效。

Both of them have no use (1 or 2 weekly downloads) so maybe you can find a better package.它们都没有用(每周下载 1 或 2 次)所以也许您可以找到更好的软件包。 As last option, copy the functions you need to use and paste them into your project, doing the change to js if needed作为最后一个选项,复制您需要使用的函数并将它们粘贴到您的项目中,如果需要,对 js 进行更改

I think there is a bug in this package.我认为这个包中有一个错误。 The main property in package.json points to dist/index.js but there is no dist directory. package.json 中的main属性指向dist/index.js但没有dist目录。 See it here stv main property在这里看到它stv 主要财产

Since there is no types": "dist/index.d.ts too you can't use it in a non typescript environment.因为没有types": "dist/index.d.ts你也不能在非打字稿环境中使用它。

Instead of doing npm add.而不是做 npm add。 Try with npm i stv and then run your program also try this link https://www.npmjs.com/package/stv尝试使用 npm i stv 然后运行你的程序也试试这个链接https://www.npmjs.com/package/stv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM