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 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.
"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'"