I am trying to do something very simple - add Rapier to a basic ThreeJS project.
I built my ThreeJS project as per the documents, making an index.html and main.js file like they suggest and then running:
npm install --save three
npm install --save-dev vite
This all worked. I can make ThreeJS scenes easily now. However, I want to add Rapier and can’t.
As per this tutorial: https://www.youtube.com/watch?v=4Ly4_-KRNiQ
I added the “rapier3d-compat” package here via command line in my folder with npm i @dimforge/rapier3d-compat
This added to my packages.json so it says:
{
"dependencies": {
"@dimforge/rapier3d-compat": "^0.12.0",
"three": "^0.163.0"
},
"devDependencies": {
"vite": "^5.2.8"
}
}
This seems fine up to now.
But then the instructions as per that video are then to in any js file write import RAPIER from '@dimforge/rapier3d-compat';
. However, adding this line then gives me the error:
Uncaught TypeError: The specifier “@dimforge/rapier3d-compat” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “…/” or “/”.
So what am I missing? I note that in index.html I am supposed to have for local tests:
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@v0.163.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@v0.163.0/examples/jsm/",
"rapier": ""
}
}
</script>
And if I take out those things for three and three/addons/ my project stops working. Why is this? Does this mean the project must download ThreeJS every time it runs from those web addresses? Is there any way to take out the web addresses so the project is not Internet-dependent?
And is there something I need to add there for Rapier to work? I am presuming that is the problem and skipped in the tutorial.
Isn’t the point of installing them that they should be accessible locally? How do I specify this?
If I try changing the import map to "three": "./node_modules/three/build/three.module.js",
this also doesn’t work so it seems I can’t reference the package locally?
I see according to this the lack of a map for Rapier is probably the problem, but I can’t get it to work with unpkg as they suggest.
Loading in my browser https://unpkg.com/@dimforge/rapier3d-compat
redirects to: https://unpkg.com/@dimforge/rapier3d-compat@0.12.0/rapier.cjs.js
but using this in my map of index.html as "rapier": https://unpkg.com/@dimforge/rapier3d-compat@0.12.0/rapier.cjs.js
and then going import RAPIER from 'rapier';
doesn’t work either.
I am running the tests using Visual Studio code Live Server addon.
Thanks for any help.