Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1415 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Directory import is not supported resolving ES modules' with Node.js

#1
I'm using Node.js **v14.13.0**.

**app.js** file:

import database from './database';

database();

**database/index.js** file:

import mongoose from 'mongoose';

export default connect = async () => {
try {
await mongoose.connect('...', { });
} catch (error) {}
};

In **package.json** I added `"type": "module"`.

After running the app I get the following error:

> Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import
> '/Users/xx/Desktop/Projects/node-starter/src/database' is not
> supported resolving ES modules imported from
> /Users/xx/Desktop/Projects/node-starter/src/app.js
Reply

#2
With ES6 modules you can not (yet?) import directories. Your import should look like this:

import database from "./database/index.js"
Reply

#3
According to nodejs [documentation][1], directory imports doesn't work


[1]:

[To see links please register here]


Reply

#4
What happens here is that Node [mandates](

[To see links please register here]

) using an extension in `import` statements and also states,

> Directory indexes (e.g. `'./startup/index.js'`) must also be fully specified.

Your `import database from './database';` statement doesn't specify the `index.js`. You can add `index.js` as suggested in another answer, but that approach doesn't look elegant [in TypeScript projects](

[To see links please register here]

), when you'd end up importing a `.js` file from a `.ts` one.

You can change this Node extension resolution behavior by passing the `--experimental-specifier-resolution=node` [flag](

[To see links please register here]

). This will work and will keep your code unchanged:

**app.js**
```js
import database from './database';
database();
```

Run as: `node --experimental-specifier-resolution=node app.js`.


A Node developer admitted that the documentation [wasn't that clear](

[To see links please register here]

).
Reply

#5
TValidator's answer is somewhat right, you can use babel to resolve this issue.<br>
However, you must install @babel/core, @babel/node, @babel/preset-env.<br>
And, you must make [babel.config.json][1] like belows;

{
"presets": ["@babel/preset-env"]
}
You should stay babel.config.json in the root folder of your project.<br>
Now, you don't have to set "type": "module" to the package.json.<br>


[1]:

[To see links please register here]

Reply

#6
You can use babel. I used following babel library in node project and every ES6 features work properly.

"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1"
}

Although it may be old version but you can use latest version.
Reply

#7
With ES6 modules you can not (yet?) import directories.

Here you can see I made this changes and it worked for me this are my two files below and I needed to write require to import user for it but in ES6 it doesn't support require so in my solution I created the export default of users and it comes with sequelize and datatypes values so now I created import of users above in index.js file so I can use those values without using require.

Also you have to add (.js) extension in the imported files path just like I did in index.js files second line.

Your import should look like this:



Index.js

import { DataTypes, Sequelize } from "sequelize";
**import Users from "./users.js";**

const sequelize = new Sequelize("Users", "root", "", {
host: "localhost",
dialect: "mysql",
pool: { max: 5, min: 0, idle: 10000 },
});
sequelize
.authenticate()
.then(() => {
console.log("Connected !");
})
.catch((err) => {
console.log("Error", err);
});

const db = {};

db.sequelize = Sequelize;
db.sequelize = sequelize;

**db.users = Users(sequelize, DataTypes);**

db.sequelize.sync().then(() => {
console.log("YES Re-Sync !");
});

export default sequelize;


Users.js

const Users = (sequelize, DataTypes) => {
sequelize.define(
"Users",
{
name: {
type: DataTypes.STRING,
},
email: {
type: DataTypes.STRING,
defaultValue: "[email protected]",
},
gender: {
type: DataTypes.STRING,
},
},
{
// timestamps: false,
// updatedAt: false,
// createdAt: false,
// createdAt: created_at,
// updatedAt: modified_at,
// engine: "Kishan",
}
`);
};


export default Users;







Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through