Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Can't require('@vladmandic/face-api') and can't import #6

Closed
ndtreviv opened this issue Oct 15, 2020 · 9 comments
Closed

Can't require('@vladmandic/face-api') and can't import #6

ndtreviv opened this issue Oct 15, 2020 · 9 comments

Comments

@ndtreviv
Copy link

ndtreviv commented Oct 15, 2020

I switched to using this repo from face-api.js, but there are differences not noted in your README.

In particular this one:
Can't require('@vladmandic/face-api') and can't import either:

[1] /Users/me/app/electron/face-extraction.js:1
[1] import * as faceapi from '@vladmandic/face-api';
[1] ^^^^^^
[1]
[1] SyntaxError: Cannot use import statement outside a module

I used to:

faceapi = require("face-api.js");

But I cannot:

const faceapi = require('@vladmandic/face-api');

Error when require-ing:

[1] Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/me/app/node_modules/@vladmandic/face-api/dist/face-api.esm.js
[1] require() of ES modules is not supported.
[1] require() of /Users/me/app/node_modules/@vladmandic/face-api/dist/face-api.esm.js from /Users/me/app/electron/face-extraction.js 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.
[1] Instead rename face-api.esm.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/me/app/node_modules/@vladmandic/face-api/package.json.
[1] 

Version: "@vladmandic/face-api": "^0.7.4"
NodeJS Version: 12.16.3

@vladmandic
Copy link
Owner

Can you describe your environment?

Are you using FaceAPI from NodeJS or is this a Browser bundler?
Original FaceAPI uses UMD format for the output bundle, I'm using ESM.

@ndtreviv
Copy link
Author

I'm using it from NodeJS as part of an electron application.

@ndtreviv
Copy link
Author

To be clearer: The code that extracts face descriptors operates on the backend of the electron application - ie: on the main process, so in NodeJS-land.

@ndtreviv
Copy link
Author

I tried this:

require = require("esm")(module)
var faceapi = require('@vladmandic/face-api');

And get this error:

[1] /Users/me/app/node_modules/@vladmandic/face-api/dist/face-api.esm.js:1
[1] Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/me/app/node_modules/@vladmandic/face-api/dist/face-api.esm.js
[1] require() of ES modules is not supported.
[1] require() of /Users/me/app/node_modules/@vladmandic/face-api/dist/face-api.esm.js from /Users/me/app/electron/face-extraction.js 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.
[1] Instead rename face-api.esm.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/me/app/node_modules/@vladmandic/face-api/package.json.

@ndtreviv
Copy link
Author

Could be related to: standard-things/esm#868

@ndtreviv
Copy link
Author

yeah, bingo. This fixed it:

require('module').Module._extensions['.js'] = function (module, filename) { 
  module._compile(require('fs').readFileSync(filename, 'utf8'), filename);
};
require = require("esm")(module)
var faceapi = require('@vladmandic/face-api');

@vladmandic
Copy link
Owner

Yup, you were on the right track, need to figure out how to best package it so it works out-of-the-box.

Issue is that original Face-API was published as UMD format bundle - it works out-of-the-box, but it's "ugly* and doesn't support tree shaking (meaning bundlers cannot just import what they need, you always end up with entire bundle that bloats the project).

I'm using ESM format which is great for Browser, but NodeJS hates it - and the hack you've mentioned above makes NodeJS work with ESM format.

I've added CJS format for Node (preferred one), just need to make sure correct one is imported by default for each case.

@vladmandic vladmandic reopened this Oct 15, 2020
@vladmandic
Copy link
Owner

I'd like to keep this issue open until it works out-of-the-box.

@vladmandic
Copy link
Owner

new version 0.8.1 is published and it should solve your issue (docs on how to use faceapi are updated).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants