I am trying to use Postgresql in a Node project. I am using modular imports, so I am having issues importing 'pg':
import * as pg from 'pg'
const { Client } = pg
let client = new Client()
leading to this error
let client = new Client()
^
TypeError: Client is not a constructor
I've looked at a couple other questions similar to this, but still have issues:
import { native as pg } from 'pg';
let client = new pg.Client()
leading to this error:
import { native as pg } from 'pg';
^^^^^^
SyntaxError: Named export 'native' not found. The requested module 'pg' is a CommonJS module, which may not support all module.exports as named exports.
Does anyone know what I can try to make this import correctly?
From the error suggestion, pg is a CommonModule which may not support all module.exports as named exports.
change the import from
import * as pg from 'pg'
to
import pg from 'pg'
will solve the import problem.
Related
I have a naive question regarding importing npm modules on Node.js server.
I have installed 'ml-random-forest' module via npm and have been trying to import the package.
I can import it via import { RandomForestClassifier as RFClassifier } from 'ml-random-forest'; but I cannot import like var RFClassifier = require('ml-random-forest');
How can I import that package using 'require' ?
It seems that when you call :
import {existingVariableFromTheLib as
yourOwnNameForIt} from 'that_library'
You're actually importing an element of this library.
But when you're calling :
const anAlias = require('this_lib')
You're putting inside of the constant "anAlias" the totality of the library.
What you could try is then to call :
anAlias.existingVariableFromTheLib
Or, another way of dealing with this is to import using this way :
const {existingVariableFromTheLib}
=require('lib')
//or :
const anAlias =
require('lib').existingVar
//maybe
const anAlias =
require('lib').existingFunction()
Like this problem, same error is occurring while using svelte kit.
here is the error:
import { remove } from "ts-invariant/process/index.js";
^^^^^^
SyntaxError: Named export 'remove' not found. The requested module 'ts-invariant/process/index.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'ts-invariant/process/index.js';
const { remove } = pkg;
I'm using the current version(^3.5.10) of #apollo/client, and using the beta version(^3.6.0-beta.6) gets other error; while using import { ApolloClient, InMemoryCache, gql } from '#apollo/client/core/index.js', it gets:
Directory import '/node_modules/#apollo/client/core' is not supported resolving ES modules imported from /node_modules/svelte-apollo/dist/svelte-apollo.js
or if I use import { ApolloClient, InMemoryCache, gql } from '#apollo/client/core', it gets:
Directory import 'node_modules/#apollo/client/core' is not supported resolving ES modules imported from node_modules/svelte-apollo/dist/svelte-apollo.js
Did you mean to import #apollo/client/core/core.cjs?
or if I use import { ApolloClient, InMemoryCache, gql } from '#apollo/client/core/core.cjs' it just says
exports is not defined
I'm having troubles with import of Inquirer using modules in Node 13.12.0. Any other import works well. As long as I've been using Node 12.x with require() it worked well.
My use-case of anything.mjs
import fs from "fs"; // works well
import inquirer from 'inquirer'; // undefined
So I've tried to import only one exported module
import {prompt} from 'inquirer'; // The requested module 'inquirer' does not provide an export named 'prompt'
also tried:
import * as inquirer from 'inquirer'; // [Module] { default: undefined }
I've also tried to require() but it is not defined in modules anymore.
How should I properly import Inquirer in Node 13.12.0 using modules?
According to the docs, you can use require in ESM in Node 13 as follows:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const inquirer = require('inquirer');
inquirer just released v9.0 and migrated to ESM modules.
So now this will simply work:
import inquirer from 'inquirer';
const response = await inquirer.prompt([
{
type: 'input',
name: 'question',
message: 'Want to answer?'
}
]);
console.log(response.question);
Using ES Modules and enquirer 2.3.6 I am using it this way. We could pass types to prompt object.
import enquirer from 'enquirer';
const enquirerObj = new enquirer();
const response = await enquirerObj.prompt({
type:'confirm',
name: 'question',
message: 'Want to answer?'
});
console.log(response);
I currently use postgresql-node in my lambda with
import { Client } from 'pg'
I want to instrument the Postgresql lib with AWS X-ray. The Nodejs example has this line:
var AWSXRay = require('aws-xray-sdk');
var pg = AWSXRay.capturePostgres(require('pg'));
How would I convert the second line in that to proper Typescript. All the variations I come up with produce some errors or warnings. For example I would guess this would work:
const pg = AWSXRay.capturePostgres(require('pg'))
but not only you get ESlint warning for require being used without import but after that pg.Client says pg namespace not found.
Well, it's slightly ugly but this seems to work:
import * as pg from 'pg'
const patchedPg = AWSXRay.capturePostgres(pg)
index.js
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
global.myTest = true
/models/user.js
import Sequelize from 'sequelize'
console.log('test:' + global.myTest)
Anyone knows how to set global variables with Apollo Server Express ? The example above returns undefined.
Due to es6 import module hoisted. All the dependent Modules will be loaded before running any code. You need to make sure to access the global.myTest after it has been defined and assigned. You can use nodejs require keyword to require your user model after assign value to global.myTest.
E.g.
index.js:
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
global.myTest = true;
require('./models/user');
./models/user.js:
console.log('test:' + global.myTest);
The output of the console:
$ npx ts-node ./index.js
test:true