Error [ERR_MODULE_NOT_FOUND] importing { Metaplex } from "@metaplex-foundation/js-next" #63
-
Hi! Hope all is well. I'm building a node + express server application that's trying to call the new API. I don't have any issues when importing things like @solana/web3.js and bip39, but unfortunately I'm getting an error at node initialization when importing Metaplex. Here's the error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/kirtd/thesyncfloor/apps/hermes/node_modules/@metaplex-foundation/js-next/dist/esm/index.js' imported from /Users/kirtd/thesyncfloor/apps/hermes/solana.js** And here's the import line: import { Metaplex } from "@metaplex-foundation/js-next"; Any ideas? Thanks in advance, Kirt. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hey 👋 Thanks for raising this. We've tested with ESM on the browser and CJS with Node.js but I don't think we've tried ESM with Node.js yet. What version of the SDK are you using? 🙂 |
Beta Was this translation helpful? Give feedback.
-
Thanks again for providing the steps to reproduce this issue. I've been able to make it work using an express app. Since express apps use "CommonJS", we need to use - import { Metaplex } from '@metaplex-foundation/js-next';
+ const { Metaplex } = require('@metaplex-foundation/js-next'); Here's a quick example of how to fetch an NFT using its mint address on an express app. Note that you'll first need to install web3.js using const express = require('express')
const app = express()
const port = 3000
const { Connection, clusterApiUrl, PublicKey} = require('@solana/web3.js');
const { Metaplex } = require('@metaplex-foundation/js-next');
const mx = Metaplex.make(new Connection(clusterApiUrl('devnet')));
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
const mint = new PublicKey("K9iFUvDYK1wsN2ERSU65jUE3mrBesr5UZvrjsvPozj3");
mx.nfts().findNftByMint(mint).then(console.log);
}) |
Beta Was this translation helpful? Give feedback.
Thanks again for providing the steps to reproduce this issue. I've been able to make it work using an express app.
Since express apps use "CommonJS", we need to use
require
to import libraries (as you do to importexpress
in the first place). That means you need to make the following change in yourindex.js
file and it will work as expected.Here's a quick example of how to fetch an NFT using its mint address on an express app. Note that you'll first need to install web3.js using
npm install @solana/web3.js
.