Getting errors while using 'got' package - javascript

I am using 'got' version '11.8.3' and I am getting below error.
/app/node_modules/got/dist/source/core/index.js:696
throw new TypeError('The payload has been already provided');
^
TypeError: The payload has been already provided
at Request.onLockedWrite (/app/node_modules/got/dist/source/core/index.js:696:19)
at PassThrough. (node:internal/streams/pipeline:323:31)
Hence I tried to upgrade 'got' to latest (12.0.3). But after this I am getting below error:
/app/server/lib/my-api.js:8
const got_1 = __importDefault(require("got"));
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/server/lib/my-api.js not supported.
Instead change the require of index.js in /app/server/lib/my-api.js to a dynamic import() which is available in all CommonJS modules.
However, I am using this statement in my .ts file.
import got from 'got'
Can you suggest on eliminating these errors

here's a fix.
first
npm i fix-esm
then you can import got package like this
const got = require("fix-esm").require("got");
Next install got version 10.0.0
npm i got#10.0.0
Hope this helps. Cheers

Related

Jest import statement: 'TypeError: Cannot set property 'fillStyle' of null'

I type:
npm test
and I get:
It's interesting to note that the import statement works inside the BootScene.test.js file but it doesn't work in the imported file.
I focused on the import statement not working under JEst. So I thought maybe it has something to do with Ecma Script version jest uses. So I tried this solution but the error persists.
This is the repo/branch of this question.
When I type npm start. Everything flows swiftly and with no errors.
The error occurs because jestdom is not able add canvas, which we need to do. For me this happened because I was using lottiefiles for the application.
Here is how I fixed it for my react application.
Install jest-canvas-mock as below .
npm i jest-canvas-mock
Then go to setup file like setup-tests.js and import jest-canvas as below .
import "jest-canvas-mock";
Now run test, you should not get errors any more.
I solved it by following this instructions.
Thanks CherryDt.
Installing npm i jest-canvas-mock
and Importing import "jest-canvas-mock" in set-up file; works for me

"Provided module can't be loaded" error when trying to load GoogleCloud Datastore module with Nodejs 8

As part of the implementation of a Cloud Function under Google Cloud Platform, executed with Nodejs 8, I have to store information in GCP Datastore.
I use TypeScript which is compiled in Javascript.
I have the following error when I try to import the "#google-cloud/datastore" module:
Provided module can't be loaded. Is there a syntax error in your
code? Detailed stack trace: TypeError: extend must be a string
I tried 2 ways to import the module and each time I get the same error:
import {Datastore} from '#google-cloud/datastore';
const datastore = new Datastore();
or
const datastore = require('#google-cloud/datastore');
I also tried to install different versions of the module, nothing helps.
Finally, I realized that even when the module is not installed (I executed the command npm uninstall #google-cloud/datastore), I get the exact same error when logically, when we try to import a module which is not installed, we should have the following error:
Provided module can't be loaded. Did you list all required modules
in the package.json dependencies? Detailed stack trace: Error:
Cannot find module 'test'
Has anyone ever encountered this problem or would have a clue why I get this error?
I faced the same issue while deploying the functions, later I realized that I installed the packages in wrong location.
My folder structure was this:
/
functions
node_modules
index.js
...
By mistake I installed all the packages at / level:
/
functions
node_modules
index.js
...
node_modules
So all my packages were in the outer node_modules.

How to resolve error when trying to install farmhash

I want to install and use farmhash. But I'm getting the following error.
./node_modules/farmhash/index.js
Module not found: Can't resolve './build/Release/farmhash' in '/path/to/my_project/node_modules/farmhash'
Steps to recreate.
At the command line, I did
yarn add farmhash
Then imported using
import farmhash from 'farmhash';
Then tried to use with
const hash = farmhash.hash32('test');
I checked my node_modules directory to verify it installed to the correct location: node_node_modules/farmhash/index.js
What am I doing wrong and how can I correct this error?

Export Import Command Hazards in Nodejs

I am trying to use a machine learning library from Github. Using decision trees example code, I got the error message "Unexpected token declaration" on the following row
import { DecisionTreeRegression as DTRegression } from 'ml-cart';
Given that import command is not fully supported by nodejs, I changed the above line to
var DTRegression = require ('ml-cart');
Now nodejs is flagging error on the subsequent statement var reg = new DTRegression(); with message "DTRegression is not a constructor".
Looking through index.js file at ./node_modules/src/index.js, I can see the following code:
export { DecisionTreeClassifier } from './DecisionTreeClassifier';
export { DecisionTreeRegression } from './DecisionTreeRegression';
Then I tried directly referencing the ./node_modules/src/DecisionTreeRegression.js file using require command. That gave errors on 'export class' command inside DecisionTreeRegression.js.
Then I tried using the following command to make the import work.
node --experimental-modules myapp.mjs
I got the error "SyntaxError: The requested module 'ml-cart' does not provide an export named 'DecisionTreeRegression'".
Nodejs version is 10.3 on Ubuntu 16.04 LTS.
Any help would be highly appreciated.
Thanks
Best Regards,
Adeel
Try the mjs extension here
DecisionTreeRegression.mjs when importing with
node --experimental-modules myapp.mjs

NPM module throws an error when being imported

I have been trying to get the following module to work on a react/webpack application:
https://github.com/ripjar/material-datetime-picker
Whenever I import the module my server wont start and it crashes with the syntax error: Uncaught SyntaxError: Unexpected token import.
It seems that throughout the modules code it is using es6 to import/export but each one of those lines breaks the module when I try to install and export it into my own file.
I have tried looking through the source code for the module as well as the standalone version but cant get either to work - however - I know that the module works inside a standalone HTML file as their demo still works when I download it locally.
Could this have to do with incompatibility with webpack? I have been working with their code along with a senior developer and we cant seem to get the module to load at all.
Apparently you're trying to import es6 module while not using es6 compiler like Babel
If you intend to use it with CommonJS you should consider adding it using require:
var MaterialDateTimePicker = require('material-datetime-picker');
I believe you are using this module/js for front-end(browser). I will suggest using babelify in your build environment. You can check documentation in given links.
webpack-for-browserify-users
babeljs.io-installation
babel/babelify
Babel transformation--
var fs = require("fs"); var browserify = require("browserify");
var babelify = require("babelify");
browserify({ debug: true })
.transform(babelify)
.require("./script.js", { entry: true })
.bundle()
.on("error", function (err) {
console.log("Error: " + err.message);
}).pipe(fs.createWriteStream("bundle.js"));

Categories