Skip to content
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

Help with how to use music-metadata in ts-node with a NextJS source base... #2250

Closed
1 task done
eedeebee opened this issue Sep 24, 2024 · 2 comments
Closed
1 task done
Labels
bug Bug, will addressed with high priority

Comments

@eedeebee
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

music-metadata version

No response

Current Behavior

Context:

I am not sure this is a music-metadata issue as I can use music-metadata no problem in my NextJS app. But, I could really use some help or advice and this may be an actual issue too. It might also be an enhancement request.

I've read docs and I am far from an expert in the different module loading possibilities/configurations between NextJS and ts-node. I'm just looking for some combination of configs or changes to how music-metadata is packaged so I can use it in ts-node as well as NextJS. I understand it supports esm, but perhaps unlike other packages, there is some compatibility with commonjs that I need as well to work with ts-node?

I run some ts-node command line utilities using some of the same TS library code I wrote for the NextJS app. That normally works fine. But, no matter how I configure ts-node to run, I can't figure out how to get music-metadata to resolve. I do this with no problem with other packages (prisma, etc).

I can attach my package.json (includes the script for ts-node) and tsconfig.json.

tsconfig.json
package.json

I have a simple code that is

(async () => {
  try {
    const { parseStream } = await import('music-metadata');
    console.log(typeof parseStream); // Should print "function"
  } catch (err) {
    console.error('Error:', err);
  }
})();

If I use ts to run it I get:

npm run ts content-tools/parseStream.ts 

> [email protected] ts
> /usr/local/bin/dotenv -e .env -- ts-node -r tsconfig-paths/register -O {\"module\":\"CommonJS\"} content-tools/parseStream.ts

undefined

My real desire is to run something like this:

/**
 * Retrieve metadata from Amazon S3 object
 * @param objRequest S3 object request
 * @return Metadata
 */

(async () => {

    const { getAudioMetaData } = await import('../src/app/lib/aws.ts');

    console.log(`bucket ${process.env.AWS_DIST_BUCKET} key: ${process.env.AWS_PROCESS_ACCESS_KEY}`)
    const metadata = await getAudioMetaData({
        Bucket: process.env.AWS_DIST_BUCKET as string,
        Key: 'p/1/cdb4a806-3e1f-43c9-ad7b-a0ed79d2795c.mp3'
    });

    console.log(metadata);
})();

And use my internal lib there which calls parseStream. I use this function getAudioMetaData today inside NextJS and it works fine.

Below is a copy of my next.config.js file for completeness but I suspect not relevant...

/** @type {import('next').NextConfig} */
const nextConfig = {
    images: {
        remotePatterns: [
            {
                protocol: 'https',
                hostname: process.env.NOTEWIZE_IMAGES_CDN,
                pathname: '/**'
            },
            {
              protocol: 'https',
              hostname: process.env.IMGIX_CDN,
              pathname: '/**'
            }, 
            {
                protocol: 'https',
                hostname: "image.mux.com",
                pathname: '/**'
            },     
        ],
    },   
    env: {
        imagesBucket: process.env.AWS_IMAGES_BUCKET,
        uploadsBucket: process.env.AWS_UPLOADS_BUCKET,
        companionUrl: process.env.COMPANION_URL,
        mediaCDN: process.env.NOTEWIZE_MEDIA_CDN,
        imageCDN: process.env.NOTEWIZE_IMAGES_CDN,
        imgixCDN: process.env.IMGIX_CDN,
        environment: process.env.VERCEL_ENV,
    },
    transpilePackages: ['react-syntax-highlighter', 'swagger-client', 'swagger-ui-react'],
    experimental: {
        outputFileTracingIncludes: {
          '/api-doc': ['./src/app/api/**/*'],
        },
    }
}

module.exports = nextConfig


// Injected content via Sentry wizard below

const { withSentryConfig } = require("@sentry/nextjs");

module.exports = withSentryConfig(
  module.exports,
  {
    // For all available options, see:
    // https://github.com/getsentry/sentry-webpack-plugin#options

    // Suppresses source map uploading logs during build
    silent: true,
    org: "notewize-inc",
    project: "javascript-nextjs",
  },
  {
    // For all available options, see:
    // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

    // Upload a larger set of source maps for prettier stack traces (increases build time)
    widenClientFileUpload: true,

    // Transpiles SDK to be compatible with IE11 (increases bundle size)
    transpileClientSDK: true,

    // Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers. (increases server load)
    // Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
    // side errors will fail.
    tunnelRoute: "/monitoring",

    // Hides source maps from generated client bundles
    hideSourceMaps: true,

    // Automatically tree-shake Sentry logger statements to reduce bundle size
    disableLogger: true,

    // Enables automatic instrumentation of Vercel Cron Monitors.
    // See the following for more information:
    // https://docs.sentry.io/product/crons/
    // https://vercel.com/docs/cron-jobs
    automaticVercelMonitors: true,
  }
);


### Expected Behavior

I'd like the above sample to print 'function' :). Let me know if you'd like a more concise example of a project with all the source files, etc...  and I'll try to cons one up.

### Attached audio sample?

- [X] I have provided sufficient information to reproduce the issue
@eedeebee eedeebee added the bug Bug, will addressed with high priority label Sep 24, 2024
@eedeebee
Copy link
Author

OK I finally found the docs about legacy CommonJS usage and that works for me :)

@Borewit
Copy link
Owner

Borewit commented Sep 25, 2024

Best to convert your project to ESM, short summary how to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug, will addressed with high priority
Projects
None yet
Development

No branches or pull requests

2 participants