Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert require() without assignment to import

file1.js

console.log("foo")

file2.js

require("./file1")
➜ node file2.js
foo

How would I convert file2.js's require to import and keep the same logic (so no assignment)?

like image 813
Molten Ice Avatar asked Jul 21 '26 20:07

Molten Ice


1 Answers

You can use like below, this will import file1 to file2

import './file1';

This is useful when you want to execute the file1 code but does not want to assign it to the variable. ex:

  • create a database connection if you don't need a connection object.
  • TypeScript compiler generates JavaScript completely without a module definition.
like image 80
Rahul Sharma Avatar answered Jul 23 '26 15:07

Rahul Sharma