Reputation: 43
I am new to node and javascript and I'm trying to utilise the following library: https://github.com/Applifting/stv
I am unable to work out why I keep get the following "Error: Cannot find module './stv'"
I've read several similar questions and responses but none of the suggested solutions, such as reinstalling node and npm, have helped.
Below is the setup I went through to set up the package.json and install the stv module.
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
The package.json correctly shows stv under the dependencies.
"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:
const stv = require('stv')
Then when running index.js I get the following.
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.
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. 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.
Any assistance with troubleshooting this error would be greatly appreciated.
Edit:
I've also tried using import { stv } from 'stv';
as per the git instructions but get another error:
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:
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;
then returns the original "Error: Cannot find module './stv'"
Upvotes: 3
Views: 2036
Reputation: 78
Looks like the stv
package has been done with typescript
and has not been properly transpiled to javascript
before publishing.
Check if libre-stv does what you need, or if any other package works.
Both of them have no use (1 or 2 weekly downloads) so maybe you can find a better package. As last option, copy the functions you need to use and paste them into your project, doing the change to js if needed
Upvotes: 2
Reputation: 2201
The package is not published properly. There is only one compiled file 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-
stv
tsc
Then
const stv = require('./stv/dist')
For permanent solution raise issue in the repo. It will be fixed by library owners/maintainers only.
Upvotes: 3
Reputation: 306
Instead of doing npm add. Try with npm i stv and then run your program also try this link https://www.npmjs.com/package/stv
Upvotes: 0
Reputation: 10601
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. See it here stv main property
Since there is no types": "dist/index.d.ts
too you can't use it in a non typescript environment.
Upvotes: 1