Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export * from 'dependency' not working when { "type": "module" } #32613

Closed
marcofranssen opened this issue Apr 2, 2020 · 9 comments
Closed
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@marcofranssen
Copy link

Node Version: v12.16.1
dependency esm version: 3.2.25

When "type": "module" is defined in package.json the following is not allowed in a file.

export * from './dep-a'
export * from './dep-b'
export * from './dep-c'
export * from './dep-d'

It gives the following error.

/Users/marco/code/my-project/lib/commands/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/marco/code/my-project/lib/commands/index.js
require() of ES modules is not supported.
require() of /Users/marco/code/my-project/lib/commands/index.js from /Users/marco/code/my-project/bin/my-project is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/marco/code/my-project/package.json.

I would expect this code to completely valid.

The idea to define type module is to be able to use the node 13 feature out of the box when on node 13 and use esm in node 12.

Is this a bug or missing feature in the implementation? Or is it me doing something I'm not supposed to do?

@MylesBorins MylesBorins added the esm Issues and PRs related to the ECMAScript Modules implementation. label Apr 3, 2020
@MylesBorins
Copy link
Contributor

@marcofranssen do you still have the same problem if you stop using the esm module and switch to using the built in --experimental-modules on Node.js 12.x? The implementation is keeping track with 13, with a big update coming next week in 12.16.2

We are aiming to unflag ESM in 12.x in 12.17.0 which is scheduled to come out May 26th

@marcofranssen
Copy link
Author

@MylesBorins Do you mean by unflag ESM means the experimental flag shouldn't be required anymore?

@MylesBorins
Copy link
Contributor

@marcofranssen the flag is currently requried but we plan to remove the need for the flag in 12.17.0

@marcofranssen
Copy link
Author

That results in the following when loading for example a package.json via import.

node:22538) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:57
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /Users/marco/code/my-project/package.json
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.resolve (internal/modules/esm/loader.js:98:42)
    at async Loader.getModuleJob (internal/modules/esm/loader.js:188:29)
    at async ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:44:17) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

See here an example of what I'm using.

#!/bin/sh
':' //# http://sambal.org/?p=1014 ; exec /usr/bin/env node --experimental-modules "$0" "$@"

'use strict'

import program from 'commander'
import pkg from '../package.json'

program
  .version(pkg.version)
  .description(pkg.description)
  .parse(process.argv)

When using with esm like this it works just fine as long my package.json doesn't include "type": "module"

#!/bin/sh
':' //# http://sambal.org/?p=1014 ; exec /usr/bin/env node -r esm "$0" "$@"

import program from 'commander'
import pkg from '../package.json'

program
  .version(pkg.version)
  .description(pkg.description)
  .parse(process.argv)

we run this app via

my-project/bin/my-proj

@MylesBorins
Copy link
Contributor

@marcofranssen our loader does not support loading JSON by default. It is an experimental feature which can be enabled with --experimental-json-modules

@MylesBorins
Copy link
Contributor

Another possibility is to make your own require function inside of your module and use that to require JSON

import { createRequire } from 'module';

const require = createRequire(import.meta.url);
const pkg = require('./package.json');

@MylesBorins
Copy link
Contributor

I'm going to close this issue as there isn't a problem in Node.js core. Please feel free to continue the discussion though

@marcofranssen
Copy link
Author

@MylesBorins when will the experimental-json-modules become default? Also in 12.17.0?

@MylesBorins
Copy link
Contributor

MylesBorins commented Apr 14, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

2 participants