Home > Enterprise >  Convert require() without assignment to import
Convert require() without assignment to import

Time:12-16

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)?

CodePudding user response:

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.
  • Related