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

javascript - Inline import by ES6 modules in Node.js - Stack Overflow

programmeradmin2浏览0评论

In an "old way" of managing modules in Node.JS (CommonJS modules) you can do something like this:

Example of Express.js route: app.use('/user', require("./user"));

How to do this when I am using ES6 Modules (import, export) and transcribing by Node.JS server by babel?

I can't just do: app.use('/user', import {user} from './user');

In an "old way" of managing modules in Node.JS (CommonJS modules) you can do something like this:

Example of Express.js route: app.use('/user', require("./user"));

How to do this when I am using ES6 Modules (import, export) and transcribing by Node.JS server by babel?

I can't just do: app.use('/user', import {user} from './user');

Share Improve this question asked Mar 18, 2019 at 13:48 BaterkaBaterka 7224 gold badges11 silver badges22 bronze badges 7
  • 2 Do the import as a separate statement and then app.use("/user", user); – Pointy Commented Mar 18, 2019 at 13:49
  • Sure it is option but then its loosing point of my question becase same way I can require module into variable and then do app.use('/user', user); – Baterka Commented Mar 18, 2019 at 13:57
  • 1 Well import and export have to be done at the outer lexical level, so you really cannot do what you're asking. – Pointy Commented Mar 18, 2019 at 14:00
  • What import("./user") do? – Baterka Commented Mar 18, 2019 at 14:02
  • 1 @Baterka That is a) a proposal only, not standard syntax b) returns a promise – Bergi Commented Mar 18, 2019 at 14:13
 |  Show 2 more ments

3 Answers 3

Reset to default 4

There is a way to do dynamic inline imports in node, detailed here: https://javascript.info/modules-dynamic-imports

This code has worked for me:

let {default: foo} = await import('./foo.js');

Here is a working example of a function I wrote as part of a db migration tool in node.

const getMigrations = async (path) => {
  const migrateDir = await fs.readdirSync(path);
  const migrations = await Promise.all(migrateDir.map(async (filename) => {
    let {default: migration} = await import(`${path}/${filename}`);
    return migration;
  }));
  migrations.sort((a, b) => {
    return a.seq - b.seq;
  });
  return migrations;
};

Where an example migration is like:

export default {
  seq: 1.1,
  label: 'create user table',
  sql: `
    DROP TABLE IF EXISTS user;
    CREATE TABLE user
    (
      ...
    );
  `
};

I am using node v12.18.4 with "type": "module" in my package.json. When I run the script, I get a warning that the ESM module loader is experimental, but it works. However, there is a note on the page linked to above that says:

Dynamic imports work in regular scripts, they don’t require script type="module".

I hope this helps. I believe you should be able to apply this to your problem.

Try separating it out into multiple expressions - import (as well as export) are not available at the same lexical level as you are trying to use it the example:

import { user } from './user'

...

app.use('/user', user)

You can achieve what you want using the following syntax:

app.use('/user', (await import('./user')).user)
发布评论

评论列表(0)

  1. 暂无评论