Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Can't resolve 'dns' in pg/lib

I did create-react-app and also installed sequelize and pg. But when I do npm start, I get the following error -


./node_modules/pg/lib/connection-parameters.js

Module not found: Can't resolve 'dns' in '/Users/vedant/Web Dev/device_psql/node_modules/pg/lib'

Here is the App.js file -


import React, { Component } from 'react';
const Sequelize = require('sequelize');
const sequelize = new Sequelize('postgres', 'postgres', 'password', {
host: 'localhost',
dialect: 'postgres',

pool: {
  max: 5,
  min: 0,
  acquire: 30000,
  idle: 10000
},

// http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
operatorsAliases: false
});

class App extends Component {
render() {
  return (
    <div >
      <p>Test</p>
    </div>
  );
}
}

export default App;

Also, in the package.json file, I have sequelize and pg. What could be the problem? I have tried to delete the node_modules folder and doing npm install, but no luck.

Thanks in advance.

like image 582
Vedant Avatar asked Jul 26 '18 14:07

Vedant


1 Answers

This is happening because one of your packages uses JavaScript promises, which depend on the dns module, which is only available in Node.js (server-side) and not when your code runs in the browser. Though the implementation depends on it, it does not require it to work, so you can solve the problem by faking the existence of the dns module.

To do so, follow these steps:

Create a folder /src/mock/dns. Inside this folder, create a file index.js containing:

module.exports = {}; 
module.exports.default = {};

Also inside this folder, create a file package.json containing:

{ 
  "name": "dns", 
  "version": "3.1.0", 
  "private": true 
}

In your root package.json, add the following line to dependencies:

"dns": "file:./src/mock/dns"

Run npm install to install the fake dependency, then restart your React project with npm start.

like image 153
Alexander van Oostenrijk Avatar answered Sep 19 '22 19:09

Alexander van Oostenrijk