最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs - import stuff from mjs file into js file OR how to use require in mjs file? - Stack Overflow

programmeradmin5浏览0评论

I am new to JS and Node. From what I been reading it seems like ES6 import is not supported in Node so I am forced to use experiment mode (renaming my file to mjs)

This works for the most part unless the mjs file needs to use require. My current solution is to break out the chunk of code that depends on require to a seperate .js file and import that js file

foo.mjs

import readFileSync from './util.js'
... 
//orignally just do require('fs') and do fs.readFileSync()
const configFile = readFileSync(configFilePath);

util.js

const fs = require('fs');
const path = require('path');

function readFileSync(filePath) {
  console.log(__dirname)
  console.log(path.resolve(__dirname, filePath))
  return fs.readFileSync(path.resolve(__dirname, filePath), 'utf8');
}

module.exports = readFileSync

Is there a way I can avoid doing this? My app.js has lots of require and I don't want to break out all those parts into separate js files.

I tried changing require to import

// const express = require('express');
import * as express from 'express';

but when run node --experimental-modules app.mjs

I get an error

TypeError: express is not a function

I think I need to either

  • find a way to use require in mjs
  • or find a way to import stuff in mjs into a js file

I am new to JS and Node. From what I been reading it seems like ES6 import is not supported in Node so I am forced to use experiment mode (renaming my file to mjs)

This works for the most part unless the mjs file needs to use require. My current solution is to break out the chunk of code that depends on require to a seperate .js file and import that js file

foo.mjs

import readFileSync from './util.js'
... 
//orignally just do require('fs') and do fs.readFileSync()
const configFile = readFileSync(configFilePath);

util.js

const fs = require('fs');
const path = require('path');

function readFileSync(filePath) {
  console.log(__dirname)
  console.log(path.resolve(__dirname, filePath))
  return fs.readFileSync(path.resolve(__dirname, filePath), 'utf8');
}

module.exports = readFileSync

Is there a way I can avoid doing this? My app.js has lots of require and I don't want to break out all those parts into separate js files.

I tried changing require to import

// const express = require('express');
import * as express from 'express';

but when run node --experimental-modules app.mjs

I get an error

TypeError: express is not a function

I think I need to either

  • find a way to use require in mjs
  • or find a way to import stuff in mjs into a js file
Share Improve this question asked Jan 19, 2021 at 0:58 OLIVER.KOOOLIVER.KOO 5,9933 gold badges34 silver badges70 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

You have three decent options:

  • Use Babel or TypeScript to get interoperability between the two types of modules
  • Write a little wrapper to redefine require in ESM code
  • Use import() (dynamic import) in your CJS to import ESM code

The wrapper to use require in ESM code:

import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const cjsFile = require('./test') // test is regular .js file using module.exports

Dynamic imports:

// foo.mjs:
export const foo = () => true
// index.js:
import('./foo.mjs').then(x => console.log(x.foo()))

Large projects with mixed module types frequently use Babel and/or TypeScript, both of which handle the mixed types just fine. Babel would be simpler to learn and get set up. But if you don't have a ton of code to deal with, the createRequire method works just fine.

As for the issue with Express, it failed because the default export is what you want: import express from 'express', not import * as express....

发布评论

评论列表(0)

  1. 暂无评论