Issue with the import in the generated daml.js

Hi everyone, I have an issue with the import in the libraries generated whit codegen.

I’m currently doing a node app to interact with the ledger so I need to import the generated library:
const mynft = require("./daml.js/mynft-0.0.1") which correspond to daml.js/create-daml-app-0.1.0 with my smart-contract.
But when I try to launch the application I have this error:

Error: Cannot find module '@daml.js/d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662'
Require stack:
- /Users/me/trainings/NFT/server/daml.js/mynft-0.0.1/lib/NFT/module.js
- /Users/me/trainings/NFT/server/daml.js/mynft-0.0.1/lib/NFT/index.js
- /Users/me/trainings/NFT/server/daml.js/mynft-0.0.1/lib/index.js
- /Users/me/trainings/NFT/server/app.js

This is caused by the file mynft-0.0.1/lib/NFT/module.js with this line:
var pkgd14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662 = require('@daml.js/d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662');

In order to make it works I need to change it to this :
var pkgd14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662 = require('../../../d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662');

Do you have any idea why this happens and how I can avoid having to modify the file each time

Could you have a look at the file ./daml.js/mynft-0.0.1/package.json? It should contain something like

"dependencies": {
        "@daml.js/d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662": "file:../d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662"
}

The fact that you get Error: Cannot find module '@daml.js/d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662' suggests that the dependencies via the package.jsons are either not right or are not being picked up correctly.

I’d suggest:

  1. Wiping the daml.js folder and running codegen again
  2. Checking the package.json contains the correct dependencies as above
  3. Posting the versions of npm/yarn/node you are using to see whether it’s reproducible.

Thank for your answer!
The dependencies are present in the package.json. If I install them manually with npm install in mynft-0.0.1 everything works well. I don’t know if it’s normal that I need to do that or not.
I’m using node v16.13.2 and npm 8.1.2.

I’ve seen this kind of error message when the version numbers in package.json and daml.yaml don’t match. If that’s the source, wiping daml.js and/or node_modules won’t help, as the solution is to make those numbers match.

Another instance where I’ve seen this type of issue is if the dependencies of your project change (this can happen without you noticing, e.g., you start generating a dependency to a type that you didn’t have before). In those cases NPM does not refresh automatically & you sometimes need to delete package-lock.json & node_modules.