-
Notifications
You must be signed in to change notification settings - Fork 318
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
Unable to import/use "write-pkg" package #906
Comments
@prakashraman Potentially Related
DeepDiveI tried to replicate this as follows: Details⇒ npx oclif generate fooCli
_-----_
| | ╭──────────────────────────╮
|--(o)--| │ Time to build an oclif │
`---------´ │ CLI! Version: 3.1.2 │
( _´U`_ ) ╰──────────────────────────╯
/___A___\ /
| ~ |
__'.___.'__
´ ` |° ´ Y `
Cloning into '/Users/devalias/dev/tmp/fooCli'...
..snip..
Created fooCli in /Users/devalias/dev/tmp/fooCli
```shell
⇒ cd fooCli ⇒ npm i write-pkg
..snip..
⇒ npm ls write-pkg
[email protected] /Users/devalias/dev/tmp/fooCli
└── [email protected] diff --git a/src/commands/hello/index.ts b/src/commands/hello/index.ts
index 2b551c9..7fd49f5 100644
--- a/src/commands/hello/index.ts
+++ b/src/commands/hello/index.ts
@@ -1,4 +1,5 @@
import {Command, Flags} from '@oclif/core'
+import { writePackage } from "write-pkg";
export default class Hello extends Command {
static description = 'Say hello'
@@ -19,5 +20,7 @@ hello friend from oclif! (./src/commands/hello/index.ts)
const {args, flags} = await this.parse(Hello)
this.log(`hello ${args.person} from ${flags.from}! (./src/commands/hello/index.ts)`)
+
+ writePackage("package.json", {foo: "bar"})
}
} ⇒ ./bin/dev hello
(node:64842) [ERR_REQUIRE_ESM] Error Plugin: fooCli [ERR_REQUIRE_ESM]: require() of ES Module /Users/devalias/dev/tmp/fooCli/node_modules/write-pkg/index.js from /Users/devalias/dev/tmp/fooCli/src/commands/hello/index.ts not supported.
Instead change the require of index.js in /Users/devalias/dev/tmp/fooCli/src/commands/hello/index.ts to a dynamic import() which is available in all CommonJS modules.
module: @oclif/[email protected]
task: toCached
plugin: fooCli
root: /Users/devalias/dev/tmp/fooCli
See more details with DEBUG=*
(Use `node --trace-warnings ...` to show where the warning was created)
Say hello to the world and others
USAGE
$ fooCli hello COMMAND
COMMANDS
hello world Say hello world Which gave me the following more specific error:
Looking in my generated CLI's
Adding a ⇒ ./bin/dev.js hello
file:///Users/devalias/dev/tmp/fooCli/bin/dev.js:3
const oclif = require('@oclif/core')
^
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/devalias/dev/tmp/fooCli/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///Users/devalias/dev/tmp/fooCli/bin/dev.js:3:15
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12) Changing it to a ⇒ ./bin/dev.cjs hello
(node:66429) [ERR_REQUIRE_ESM] Error Plugin: fooCli: Must use import to load ES Module: /Users/devalias/dev/tmp/fooCli/src/commands/hello/index.ts
require() of ES modules is not supported.
require() of /Users/devalias/dev/tmp/fooCli/src/commands/hello/index.ts from /Users/devalias/dev/tmp/fooCli/node_modules/@oclif/core/lib/module-loader.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/devalias/dev/tmp/fooCli/package.json.
module: @oclif/[email protected]
task: toCached
plugin: fooCli
root: /Users/devalias/dev/tmp/fooCli
See more details with DEBUG=*
(Use `node --trace-warnings ...` to show where the warning was created)
(node:66429) [ERR_REQUIRE_ESM] Error Plugin: fooCli: Must use import to load ES Module: /Users/devalias/dev/tmp/fooCli/src/commands/hello/world.ts
require() of ES modules is not supported.
require() of /Users/devalias/dev/tmp/fooCli/src/commands/hello/world.ts from /Users/devalias/dev/tmp/fooCli/node_modules/@oclif/core/lib/module-loader.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/devalias/dev/tmp/fooCli/package.json.
module: @oclif/[email protected]
task: toCached
plugin: fooCli
root: /Users/devalias/dev/tmp/fooCli
See more details with DEBUG=*
Say hello to the world and others
USAGE
$ fooCli hello COMMAND Going back to the drawing board.. I removed Diffdiff --git a/src/commands/hello/index.ts b/src/commands/hello/index.ts
index 2b551c9..93b2342 100644
--- a/src/commands/hello/index.ts
+++ b/src/commands/hello/index.ts
@@ -1,5 +1,7 @@
import {Command, Flags} from '@oclif/core'
+const writePackagePromise = import('write-pkg').then(({ writePackage }) => writePackage);
+
export default class Hello extends Command {
static description = 'Say hello'
@@ -19,5 +21,8 @@ hello friend from oclif! (./src/commands/hello/index.ts)
const {args, flags} = await this.parse(Hello)
this.log(`hello ${args.person} from ${flags.from}! (./src/commands/hello/index.ts)`)
+
+ const writePackage = await writePackagePromise
+ writePackage("package.json", {foo: "bar"})
}
} But this still gave me errors:
It was suggested in #695 that perhaps this is related to how |
I tried following along with the esm instructions from the following:
But ended up with the following errors: Details
Based on this error, I suspect that changes would need to be made in
|
Fixed by oclif/core#759 (currently available in |
Hi,
While attempting to import and use the
write-pkg
package in a command I see the following errorThe code
Run
Any thoughts?
The text was updated successfully, but these errors were encountered: