I am trying to run a stand-alone TypeScript script using Node.exe (v14.18.1 on Windows). The script starts by requiring jspdf and jspdf-autotable:
const { jsPDF } = require("jspdf");
require("jspdf-autotable");
So I install these modules with:
npm install --global --force jspdf@2.5.1 jspdf-autotable@3.8.2
However, where I try to run the script with
node MyScript.ts
I get this error:
Error: Cannot find module 'jspdf'
I am new to the Node development world and would appreciate any help. Thanks.
Edit:
I also tried installing the dependences with this:
npm install @types/jspdf @types/jspdf-autotable --save-dev
But I got this message:
npm WARN deprecated @types/jspdf@2.0.0: This is a stub types definition. jspdf provides its own type definitions, so you do not need this installed.
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types%2fjspdf-autotable - Not found
npm ERR! 404
npm ERR! 404 '@types/jspdf-autotable@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\ealvarez\AppData\Roaming\npm-cache\_logs\2024-07-03T14_58_18_811Z-debug.log
Any help is appreciated.
I found the solution. I needed to create a package.json file in the same folder as the script was, like this:
{
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"jspdf": "2.5.1",
"jspdf-autotable": "3.8.2"
},
"devDependencies": {
"jspdf": "2.5.1",
"jspdf-autotable": "3.8.2"
}
}
then, in that folder I ran:
npm install
After that, Node.exe was able to run the TypeScript script successfully.