If you are using Node.js and want to switch from using require()
to the ES modules import
syntax, here’s how you can do it:
- Open your
package.json
file. - Add
"type": "module",
to the JSON object like this:
{
"name": "projectname",
"type": "module",
"version": "1.0.0",
...the rest of your file
}
Save the file and you’re done! Now you can use import
to import modules in your Node.js code. For example:
import fs from 'fs'
By making this change, you can take advantage of the modern ES modules syntax in your Node.js projects. Enjoy coding!
Tags: Node.js, import, require, ES modules