Trying to use pdfjs-dist in node js app
Steps:
created new node js app (module) then:-
npm i pdfjs-dist
package.json is:-
"name": "testpdf",
"version": "1.0.0",
"description": "pdf test",
"main": "testpdf.js",
"type": "module",
"scripts": {
"start": "node testpdf.js"
},
"dependencies": {
"pdfjs-dist": "^2.13.216"
}
}
1 line app (testpdf.js) is
import pdfjs from 'pdfjs-dist'
Get this error with the import:
D:\dev\pdf\node_modules\pdfjs-dist\build\pdf.js:1707
return this._jsActionsPromise ||= this._transport.getPageJSActions(this._pageIndex);
^^^
SyntaxError: Unexpected token '||='
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at ModuleWrap. (internal/modules/esm/translators.js:199:29)
at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
at async Loader.import (internal/modules/esm/loader.js:166:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5) PS D:\dev\pdf>
If I look at node_modules\pdfjs-dist\build\pdf.js there are multiple instances of use of this operator "||="
I'm no javascript expert but from what I know this is not a valid opertaor and node v14.15.4 doesn't seem to think it is either.
Note this package version (pdfjs-dist 2.13.216) has been available for two months and can't find a similar issue on the net so guessing I am doing something wrong...
Any ideas?
As was on and old version of NodeJS I upgraded to the latest version v18.1.0 and this cleared the issue
Related
I have a cdk project that includes lambda functions, dynamodb and api gateway. To run it locally I use SAM local:
sam local start-api -t ./cdk.out/stackName.template.json
In the project I use aws-sdk v2 and everything works fine.
Now, I want to change to aws-sdk v3 so I install dynamoDB client:
npm install #aws-sdk/client-dynamodb
and import it in my lambda index file:
const { DynamoDBClient } = require("#aws-sdk/client-dynamodb");
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html
i run cdk synth and then again sam local start-api -t ./cdk.out/stackName.template.json, everything seems fine.
But when I hit the lambda function i get error this error:
undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '?'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '?'"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
time="2022-11-24T13:14:55.537" level=error msg="Init failed" InvokeID= error="Runtime exited with error: exit status 129"
My package.json:
{
"name": "aws-api-v1",
"version": "0.1.0",
"scripts": {},
"dependencies": {
"#aws-sdk/client-dynamodb": "^3.216.0",
"#aws-sdk/client-secrets-manager": "^3.216.0",
"jsonwebtoken": "^8.5.1"
}
}
Any suggestion?
Thank you.
This question already has answers here:
Error: require() of ES modules is not supported when importing node-fetch
(10 answers)
Closed 1 year ago.
I am trying to do a simple API fetch using node-fetch.
Getting the following error:
internal/modules/cjs/loader.js:1089
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\src\index.js from C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\package.json.
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:961:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:92:18)←[39m
at Object.<anonymous> (C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\index.js:2:15)
←[90m at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m {
code: ←[32m'ERR_REQUIRE_ESM'←[39m
}
My node version is v14.17.6
I have installed node-fetch
Here is my index.js:
const Fetch = require('node-fetch')
fetch("https://api.github.com/users")
.then((res) => res.json())
.then((res) => console.log(res));
Here is my package.json:
{
"name": "Fetch_PBI_Access_Token",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^3.0.0"
}
}
If I add "type": "module" I then get the following error:
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///C:/Users/raben/OneDrive/Work/PowerBI%20Embedded/Fetch_PBI_Access_Token/index.js:2:15
←[90m at ModuleJob.run (internal/modules/esm/module_job.js:170:25)←[39m
←[90m at async Loader.import (internal/modules/esm/loader.js:178:24)←[39m
←[90m at async Object.loadESM (internal/process/esm_loader.js:68:5)←[39m
The error really says it all. Since node-fetch is an ES module, you shouldn't use the require syntax to import it, but the import syntax:
import fetch from 'node-fetch';
Note:
Older versions of node-fetch are still CommonJS packages (i.e., can be used with require), so if you downgrade your dependency to some 2.x version, your code should work as-is.
i have this problem when i require DBus from the dbus package
const { app, BrowserWindow } = require("electron");
const DBus = require('dbus');
app.whenReady().then(() => {
var win = new BrowserWindow({ width: 600, height: 500 });
win.loadFile('index.html');
win.webContents.openDevTools();
});
so when i execute this code i get this error :
> electron script.js
App threw an error during load
Error: Module did not self-register: '/home/bislem/projects/test/electron/node_modules/dbus/build/Release/dbus.node'.
at process.func [as dlopen] (electron/js2c/asar.js:140:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1196:18)
at Object.func [as .node] (electron/js2c/asar.js:140:31)
at Module.load (internal/modules/cjs/loader.js:981:32)
at Module._load (internal/modules/cjs/loader.js:881:14)
at Function.Module._load (electron/js2c/asar.js:769:28)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/bislem/projects/test/electron/node_modules/dbus/lib/dbus.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:1145:30)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Module did not self-register: '/home/bislem/projects/test/electron/node_modules/dbus/build/Release/dbus.node'.
at process.func [as dlopen] (electron/js2c/asar.js:140:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1196:18)
at Object.func [as .node] (electron/js2c/asar.js:140:31)
at Module.load (internal/modules/cjs/loader.js:981:32)
at Module._load (internal/modules/cjs/loader.js:881:14)
at Function.Module._load (electron/js2c/asar.js:769:28)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/bislem/projects/test/electron/node_modules/dbus/lib/dbus.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:1145:30)
I searched for this error and tried some solutions but none of them worked (npm rebuild,npm update,delete the node_modules folder then npm install,use a diffrent version of node using the nvm command), but when runing each package by his own, (run electron using the start script electron ., and run dbus with node script.js) they works correctly.
and there was this when installing the dbus package :
console when installing dbus, console when installing dbus part 2
my package.json file :
{
"name": "electrontest",
"version": "1.0.0",
"description": "",
"main": "script.js",
"scripts": {
"start": "electron script.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bootstrap": "^4.5.3",
"dbus": "^1.0.7",
"electron": "^10.1.5"
}
}
so my question is what is the correct way to Build/Run such code ? || how to use these two packages side by side ?
You need to run
npm install --save-dev electron-rebuild && npx electron-rebuild
More on using native node modules in electron here
Versions are:
nvm --version 0.35.3
npm -v: 6.14.8
node -v: v14.10.0
important package.json parts:
{
"main": "index.js",
"scripts": {
"start": "./node_modules/mocha/bin/mocha index.js",
"install": "bash install.sh",
"clear": "rm -R -f node_modules && rm package-lock.json"
},
"dependencies": {
"chromedriver": "^84.0.1",
"dotenv": "^8.2.0",
"mocha": "^8.1.2",
"selenium-webdriver": "^4.0.0-alpha.7"
}
}
index.js:
require('dotenv').config()
const password = process.env.PASSWORD;
const selenium = require('selenium-selenium');
const chrome = require('selenium-selenium/chrome');
const chromedriver = require('chromedriver');
describe('Init driver and login', () => {
//... Should run through as an empty test but does not
});
and the error I'm getting when I'm running mocha index.js or npm run start:
Error: Cannot find module 'selenium-selenium'
Require stack:
- /home/mts/Desktop/projects/selenium/index.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/mocha.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/one-and-dones.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/options.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:893:15)
at Function.Module._load (internal/modules/cjs/loader.js:743:27)
at Module.require (internal/modules/cjs/loader.js:965:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/home/mts/Desktop/projects/selenium-moco/index.js:11:18)
at Module._compile (internal/modules/cjs/loader.js:1076:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:941:32)
at Function.Module._load (internal/modules/cjs/loader.js:782:14)
at Module.require (internal/modules/cjs/loader.js:965:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.exports.requireOrImport (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/mocha.js:427:19)
at singleRun (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run-helpers.js:156:15)
at exports.runMocha (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run-helpers.js:225:10)
at Object.exports.handler (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run.js:366:11)
at /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/node_modules/yargs/lib/command.js:241:49
As simple as possible, but the module is not found - why? Should I try the lowest versions possible? I wanted to avoid the compatibility hell so I used only the newest versions available.
I think you use incorrect package name in your code - selenium-selenium. This package doesn't available on npmjs.org.
Try change this lines in our code:
const selenium = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
Currently using react-native-cli: 1.0.0 and react-native: 0.26.3
Running react-native command in terminal gives this error.
/Users/appleuser/Desktop/asklegit-app/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229
throw err;
^
SyntaxError: /Users/appleuser/package.json: Error while parsing JSON - Unexpected end of input
at Object.parse (native)
at OptionManager.addConfig (/Users/appleuser/Desktop/asklegit-app/node_modules/babel-core/lib/transformation/file/options/option-manager.js:225:62)
at OptionManager.findConfigs (/Users/appleuser/Desktop/asklegit-app/node_modules/babel-core/lib/transformation/file/options/option-manager.js:442:30)
at OptionManager.init (/Users/appleuser/Desktop/asklegit-app/node_modules/babel-core/lib/transformation/file/options/option-manager.js:484:12)
at compile (/Users/appleuser/Desktop/asklegit-app/node_modules/babel-register/lib/node.js:112:69)
at loader (/Users/appleuser/Desktop/asklegit-app/node_modules/babel-register/lib/node.js:158:14)
at Object.require.extensions.(anonymous function) [as .js] (/Users/appleuser/Desktop/asklegit-app/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
Anyone got a clue? Couldn't find anything online :(
This is my package.json
{
"name": "asklegit",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"ios": "react-native run-ios",
"android": "react-native run-android"
},
"dependencies": {
"react": "15.0.2",
"react-native": "0.26.3"
}
}
I don't know under which circumstances but it seems sometimes there is an empty package.json created in the users home directory. A completely empty file is by default not correct syntax so calling applications will produce an error.
Solution is to just delete the empty package.json.