As of 2019-10-31 ECMAScript Module (ESM) support in Node.js is still experimental and there is not yet a stable way of offering an ESM build for Node.js. The authors of the Node.js module working group even urge developers not to release dual CommonJS/ESM packages to npm yet, see: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md#phase-4-further-improvements-after-unflagging which states:
- Dual CommonJS/ESM packages: Either support packages with both CommonJS and ESM sources that can be used in either environment; or decide to specifically not support dual CommonJS/ESM packages.
- Status quo (at time of possible unflagging): "main" points to exactly one file, and all file extensions are mandatory (by default), so there is no possibility of an import specifier pointing to different files in ESM versus CommonJS. Recommended practice for dual packages is to have "main" point to the CommonJS entry point and have users use a deep import, e.g. /module.mjs, to access ESM entry point.
There is ongoing work within Node.js to support multiple exports for npm packages which will eventually solve this issue. We will support this feature once it becomes stable in node (i.e. once it's no longer hidden behind an experimental flag).
Corresponding Node.js issues:
Current status: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md#phase-4-further-improvements-after-unflagging
Status: --experimental-conditional-exports will unflag by the end of January 2020 unless consensus is reached on an alternative approach. The flag may be dropped sooner if a consensus is reached, either for an alternative solution or for --experimental-conditional-exports.
@ctavan Just curious: are you planning on enabling the ES module version for Node? I'm considering doing the same in my libraries, and would love to hear your thoughts there.
@dstaley TL;DR: Yes, but not immediately.
I am planning to do this. However given the whole thing has only recently landed in Node.js, it's still flagged as experimental and there has been a lot of back and forth with the implementation details, I will likely wait a bit longer until ESM support is more stable in Node.js
Also, it will be a bit of a challenge to enable ESM for Node.js with this module since we have different code to be executed when running in Node.js vs. in Browser environments. This has to be solved with ESM as well…
What is the correct way currently to require the module in node.js? Didn't find an answer from the docs. Or should I just ignore the deprecation warning?
@tapz No correct way until conditional exports (https://nodejs.org/docs/latest-v12.x/api/esm.html#esm_conditional_exports) are landed in node. Currently they are under feature flag.
@TrySound Then I really don't understand why this change was done. Does not make any sense at all.
@tapz what are you looking for?
Do you want to use this module as a native ECMAScript Module (ESM) with Node.js' experimental ESM support? Then, as this issue describes and as mentioned by @TrySound, this is not yet supported (because this is all still experimental in Node.js).
Do you want to use this module as a classic CommonJS module with Node.js (require()-Syntax)? Then everything should be documented in https://github.com/uuidjs/uuid#deep-requires-now-deprecated
Does this answer your question? If it is still unclear it would be great to learn how we can make the docs clearer.
@tapz This change was done to allow bundlers like rollup and webpack build more efficient code. And later native node support can be added with less steps.
I'm trying to figure out how to use this in my sequelize seed file. I'm getting the deprecation warning and it is causing it to fail seeding.
This is my relevant code:
'use strict';
const uuidv4 = require('uuid/v4');
const bcrypt = require('bcryptjs');
const saltRounds = 10;
async function hash(password) {
return await bcrypt.hash(password, saltRounds);
}
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('users', [
{
id: uuidv4(),
If I switch to import, I get ERROR: Cannot use import statement outside a module
This will work too
const { v4: uuidv4 } = require('uuid');
For everyone in this thread: we have just released [email protected] which comes with native ESM support for Node.js! Let us know if that works for you. See CHANGELOG for more info.
Most helpful comment
This will work too