sequelize.js can't find pg dependency when using webpack - node.js

When requiring sequelize I get some warning about "Critical dependencies".
After running the app I get the following error: "Error: The dialect postgres is not supported. (Error: Please install 'pg' module manually)".
Both pg and pg-hstore are however installed.
I think the problem is with sequelize.js dynamically looking for postgres. I tried to use the webpack ContextReplacementPlugin, but I have no idea how.
I'm using the React Starter Kit boilerplate. The code that causes the problem is nothing more than this:
var Sequelize = require('sequelize');
//get the database info
import { dbModelLocation} from '../config';
import { dbConnectionConfig } from '../config';
var sequelize = new Sequelize(dbConnectionConfig.name,
dbConnectionConfig.user,
dbConnectionConfig.pass,
dbConnectionConfig.options);
The exact warnings I get from webpack are:
WARNING in ./~/sequelize/lib/sequelize.js Critical dependencies:
636:60-73 the request of a dependency is an expression #
./~/sequelize/lib/sequelize.js 636:60-73
WARNING in ./~/sequelize/lib/dialects/mssql/connection-manager.js
Critical dependencies: 15:15-71 the request of a dependency is an
expression # ./~/sequelize/lib/dialects/mssql/connection-manager.js
15:15-71
WARNING in ./~/sequelize/lib/dialects/mysql/connection-manager.js
Critical dependencies: 15:15-69 the request of a dependency is an
expression # ./~/sequelize/lib/dialects/mysql/connection-manager.js
15:15-69
WARNING in ./~/sequelize/lib/dialects/postgres/connection-manager.js
Critical dependencies: 16:41-92 the request of a dependency is an
expression 16:102-153 the request of a dependency is an expression #
./~/sequelize/lib/dialects/postgres/connection-manager.js 16:41-92
16:102-153
WARNING in ./~/sequelize/lib/dialects/sqlite/connection-manager.js
Critical dependencies: 19:15-71 the request of a dependency is an
expression # ./~/sequelize/lib/dialects/sqlite/connection-manager.js
19:15-71

If you're like me ran into the same issue when using Webpack with Sqlite3 or MySQL, you'll need to use externals and define the dependencies:
var config = {
externals: ['pg', 'sqlite3', 'tedious', 'pg-hstore'],
};
Further information can be found in the following:
Can't resolve pg-hstore
Webpack externals

Sorry for answer on old questions, but I got the same error when I tried to use sequelize migrations. May be it will help someone.
You have to install 'pg' package globally and it will be work:
npm install -g pg

According to the Webpack author the solution is to ignore node_modules while bundling.

Related

export '__RouterContext' (imported as 's') was not found in 'react-router'

This is my error and I can't fix it :(((
This error appeared while I
import { GuardProvider, GuardedRoute } from 'react-router-guards'
and use it.
It's not expressly limited by react-router-guard's package.json the repo and README both state there's a peer dependency on react-router-dom#5.
See Requirements.
This package has the following peer dependencies:
React v16.8.0+ (for hooks ⚓️)
React Router DOM v5.0.0+
Based on the error it seems you have react-router-dom#6 installed and it has many different exports than the previous version. To resolve you will need to revert to the v5 version of react-router-dom.
To install, run from the project's root directory:
npm i -s react-router-dom#5

Problems with 'fs' when I try to start snowpack

[15:03:25] [snowpack] Welcome to Snowpack! Because this is your first
time running this project, Snowpack needs to prepare your
dependencies. This is a one-time step and the results will be cached
for the lifetime of your project. Please wait... [15:03:25] [snowpack]
Package "fs" not found. Have you installed it?
When I try to start snowpack I got the error above...
Is 'fs' not include in node by default? When I tried to install 'fs' via npm install fs, not started as well. Some of my files(test files) use 'fs' to run some tests...
Obs: I am using to import:
import fs from 'fs';//(not require('fs'))
Thanks. André
I discovered how to deal with it in the snowpack website using the configuration in snowpack.config.js (packageOptions.external: ["fs", "path"])
module.exports = {
packageOptions: {
/* ... */
external: ["fs", "path"]
}
}

How would you fix an 'ERR_REQUIRE_ESM' error?

I am trying to use the chalk npm.
My code is:
const chalk = require('chalk');
console.log(
chalk.green('All sytems go') +
chalk.orange('until').underline +
chalk.black(chalk.bgRed('an error occurred'))
);
And I receive this error in my terminal when I type node main.js
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/ezell/Documents/CodeX/NPM/node_modules/chalk/source/index.js from /Users/ezell/Documents/CodeX/NPM/main.js not supported.
Instead change the require of index.js in /Users/ezell/Documents/CodeX/NPM/main.js to a dynamic import() which is available in all CommonJS modules.
at Object. (/Users/ezell/Documents/CodeX/NPM/main.js:1:15) {
code: 'ERR_REQUIRE_ESM'
}
I got the same 'ERR_REQUIRE_ESM' error for nanoid:^4.0.0 & there are multiple ways to resolve this error:-
1)Use fix esm https://www.npmjs.com/package/fix-esm module & import the module like this:
const someModule = require("fix-esm").require("some-module");
2)Use dynamic import as shown below:
import('nanoid')
.then((res)=>{ console.log(res) })
.catch((err)=>{ console.log(err) });
Just make sure you dont have type:"module" field in package.json in above both cases otherwise you got "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension" error
3)Downgrade the module version to a stable old version, for eg in my case it was resolved when I downgraded nanoid version to :
"nanoid": "^3.1.22"
You need to switch to using the import keyword, as Chalk 5 only supports ESM modules.
So, to fix your code to adapt these changes, you need to...
Edit your package.json file to allow ESM imports. Add the following in your package.json file:
{
"type": "module"
}
Load Chalk with the import keyword, as so:
import chalk from "chalk";
If you, however, want to use require(), you need to downgrade to Chalk 4. Follow these steps to downgrade.
Replace your existing chalk key with the following in your package.json file:
{
"dependencies": {
"chalk": "4.1.2"
}
}
Then, run the following command to install Chalk from your package.json file. Make sure to run this in the directory in which your package.json file is in!
$ npm install
Use the require() statement like normal.
const chalk = require("chalk");
In summary, these are the two things you can do.
Stay with Chalk 5, and update import statements.
Downgrade to Chalk 4, and keep require() statements.
Solution, this happens because you have to use the current stable release 2.x
first:
npm uninstall -D node-fetch
After that:
npm install node-fetch#2
This should work.
The latest version of Chalk is only compatible with ESM modules and thus wants you to load it with import, not require().
From the doc:
IMPORTANT: Chalk 5 is ESM. If you want to use Chalk with TypeScript or
a build tool, you will probably want to use Chalk 4 for now. Read more.
So, your choices are:
Switch your project to an ESM module and load the latest version of Chalk with import instead of require().
Install version 4 of Chalk which can be used with require().
With a fairly recent version of Node.JS, you can use dynamic import to load the ESM module into your CommonJS module: const chalk = await import('chalk');

Nodejs: Cannot find module `nanoid`

When I try to compile my Typescript project (NestJS) I came across the following problem.
Some packages like nanoid, cookie-parser give error Error: Cannot find module 'nanoid'
The code where they used is below:
...
import { nanoid } from "nanoid";
...
const confirmToken = nanoid(32);
...
Here are what I tried:
I checked the node_modules directory. The package is there
Tried to completely remove node_modules and package.json.lock.
Used v16.14.0, v17 versions of NodeJS
Used yarn and npm
None of these ways helped.
Again, this error happens with only certain packages.
What could be an issue?

error TS2307: Cannot find module 'bluebird'

I am currently trying to develop an app using Ionic 2 and Angular 2 with Typescript version. I decided to use the library amqp-ts to include messaging in my app. I installed the library via npm like:
npm install amqp-ts
Everything went fine and now I've got something like this:
/ app root directory
+ node_modules
- amqp-ts
- lib
- amqp-ts.d.ts
- node_modules
- amqplib
- bluebird
- winston
The problems begin now: I import the library into my component as it is done in the example of the documentation...
import * as Amqp from "amqp-ts";
... and when I try to deploy the app I get the next error messages:
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(2,26): Error TS2307: Cannot find module 'bluebird'.
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(50,12): Error TS2304: Cannot find name 'Buffer'.
1. The line related to the first error message
// exported Typescript type definition for AmqpSimple
import * as Promise from "bluebird";
[...]
2. The line related to the second error message (same file: amqp-ts.d.ts)
export class Message {
content: Buffer;
[...]
}
I hope you can help me, please.
Additionally to regular package install you need to install TypeScript typings. Typings are like header files, they contain all methods/classes/interfaces definitions.
To install typings you need a typings tool. The best way is to install it globally so you can use it in every project
npm install typings --global
Then installing new typings inside your project is pretty simple, first search for library:
typings search bluebird
Install it :
typings install --save bluebird
More info : https://github.com/typings/typings
I too faced same issue, but for me above answer is not working.
While simply running :
npm i bluebird
resolving the issue

Resources