diff --git a/packages/create-redwood-app/build.mjs b/packages/create-redwood-app/build.mjs index 5b7640812720..9cdd1a069063 100644 --- a/packages/create-redwood-app/build.mjs +++ b/packages/create-redwood-app/build.mjs @@ -9,6 +9,7 @@ const result = await esbuild.build({ bundle: true, minify: true, + format: 'esm', platform: 'node', target: ['node18'], packages: 'external', diff --git a/packages/create-redwood-app/package.json b/packages/create-redwood-app/package.json index 223a584c6d3d..d935b0b6b415 100644 --- a/packages/create-redwood-app/package.json +++ b/packages/create-redwood-app/package.json @@ -1,6 +1,7 @@ { "name": "create-redwood-app", "version": "5.0.0", + "type": "module", "repository": { "type": "git", "url": "/~https://github.com/redwoodjs/redwood.git", diff --git a/packages/create-redwood-app/src/create-redwood-app.js b/packages/create-redwood-app/src/create-redwood-app.js index 057841d2568f..1dbcd6640e72 100644 --- a/packages/create-redwood-app/src/create-redwood-app.js +++ b/packages/create-redwood-app/src/create-redwood-app.js @@ -1,6 +1,7 @@ #!/usr/bin/env node -import path from 'path' +import path from 'node:path' +import { fileURLToPath } from 'node:url' import { trace, SpanStatusCode } from '@opentelemetry/api' import chalk from 'chalk' @@ -14,14 +15,22 @@ import yargs from 'yargs/yargs' import { RedwoodTUI, ReactiveTUIContent, RedwoodStyling } from '@redwoodjs/tui' -import { name, version } from '../package' - +// In ESM, for relative paths, the extension is important. +// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#mandatory-file-extensions. import { UID, startTelemetry, shutdownTelemetry, recordErrorViaTelemetry, -} from './telemetry' +} from './telemetry.js' + +// JSON modules aren't stable yet, but if they were we could use them instead. +// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#json-modules. +const __dirname = path.dirname(fileURLToPath(import.meta.url)) + +const { name, version } = fs.readJSONSync( + path.resolve(__dirname, '../package.json') +) // Telemetry const { telemetry } = Parser(hideBin(process.argv)) @@ -168,7 +177,7 @@ async function executeCompatibilityCheck(templateDir) { */ function checkNodeAndYarnVersion(templateDir) { return new Promise((resolve) => { - const { engines } = require(path.join(templateDir, 'package.json')) + const { engines } = fs.readJSONSync(path.join(templateDir, 'package.json')) checkNodeVersionCb(engines, (_error, result) => { return resolve([result.isSatisfied, result.versions]) diff --git a/packages/create-redwood-app/src/telemetry.js b/packages/create-redwood-app/src/telemetry.js index 973f1889e105..ffb648447410 100644 --- a/packages/create-redwood-app/src/telemetry.js +++ b/packages/create-redwood-app/src/telemetry.js @@ -1,3 +1,6 @@ +import path from 'node:path' +import { fileURLToPath } from 'node:url' + import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api' import opentelemetry, { SpanStatusCode } from '@opentelemetry/api' import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http' @@ -9,10 +12,15 @@ import { import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' import ci from 'ci-info' import envinfo from 'envinfo' +import fs from 'fs-extra' import system from 'systeminformation' import { v4 as uuidv4 } from 'uuid' -import { name as packageName, version as packageVersion } from '../package' +// JSON modules aren't stable yet, but if they were we could use them instead. +// See https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#json-modules. +const { name: packageName, version: packageVersion } = fs.readJSONSync( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../package.json') +) /** * @type NodeTracerProvider