Giter Site home page Giter Site logo

Comments (7)

rpastro avatar rpastro commented on May 27, 2024

@yordis To answer your first question. The extension is mandatory when loading an ES module with import.

The second issue may be related to oclif not loading the config from the proper directory. Try passing the absolute path of your project's directory as the second parameter to oclif.run.

oclif.run(process.argv.slice(2), <project path>).then(flush).catch(handle);

from core.

yordis avatar yordis commented on May 27, 2024

@rpastro let me try, also, why is that required in this particular case? I cant find information about it

from core.

yordis avatar yordis commented on May 27, 2024

Passing the absolute path works ... but how would that work once I publish the package then? I am confused about what is going on and what is the proper fix.

Do you have some documentation or implementation references?

from core.

rpastro avatar rpastro commented on May 27, 2024

@rpastro let me try, also, why is that required in this particular case? I cant find information about it

@yordis See https://nodejs.org/docs/latest-v16.x/api/esm.html#mandatory-file-extensions

from core.

rpastro avatar rpastro commented on May 27, 2024

@yordis Since there is no __dirname in ES modules, you can get the directory where your source file is located as follows:

import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

Once you have the __dirname you can then adjust it to determine the base path of your project.

from core.

typhonrt avatar typhonrt commented on May 27, 2024

Again apologies since I haven't worked on any Oclif / ESM aspects since ~May in my own projects using the new ESM functionality I helped add to Oclif. I gather the documentation was not created by the Oclif team yet.

Check out how you can bootstrap things w/ ESM depending on your Node version:
https://github.com/typhonjs-oclif-scratch/test-cli-modern
https://github.com/typhonjs-oclif-scratch/test-cli-cjs-interop

Depending on Node version you can do the following (Node 12.20+ or 14.13+):

#!/usr/bin/env node
import { run, flush, Errors } from '@oclif/core';

run(void 0, import.meta.url)
.then(flush)
.catch(Errors.handle);

or for CJS interop for named exports on Node 12.17+ / 14.0+:

#!/usr/bin/env node
import oclif from '@oclif/core';

oclif.run(void 0, import.meta.url)
.then(oclif.flush)
.catch(oclif.Errors.handle);

The above is for ./bin/run.js.

Also as of last time I checked the rest of the Oclif infrastructure wasn't updated for deployment of ESM CLIs, so this may still be the case and demo instructions are here. Requires a custom build of Oclif config v1 adding ESM support for the old dev CLI publishing step. This likely could be out of date as I don't have visibility into the current state of Oclif v2 / development progress since ~May and the additional upgrades to the dev CLI publishing step:
#130 (comment)

from core.

yordis avatar yordis commented on May 27, 2024

I am gonna close this folks, I went back to the old version. Until the official website, and official samples are not in ESM I will probably wait for things to be settled a bit.

Thank you regardless, you actually helped me to understand the problems ahead.

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.