[name]
While importing three.js via script tags is a great way to get up and running fast, it has a few drawbacks for longer lived projects, for example:
- You have to manually fetch and include a copy of the library as part of your project's source code
- Updating the library's version is a manual process
- When checking in a new version of the library your version control diffs are cluttered by the many lines of the build file
Using a dependency manager like npm avoids these caveats by allowing you to simply download and import your desired version of the library onto your machine.
Installation via npm
Three.js is published as an npm module, see: [link:https://www.npmjs.com/package/three npm]. This means all you need to do to include three.js into your project is run "npm install three"
Importing the module
Assuming that you're bundling your files with a tool such as [link:https://webpack.github.io/ Webpack] or [link:https://github.com/substack/node-browserify Browserify], which allow you to "require('modules')" in the browser by bundling up all of your dependencies.
You should now be able to import the module into your source files and continue to use it as per normal.
var THREE = require('three');
var scene = new THREE.Scene();
...
You're also able to leverage [link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import ES6 import syntax]:
import * as THREE from 'three';
const scene = new THREE.Scene();
...
or if you wish to import only select parts of three.js library, for example Scene:
import { Scene } from 'three';
const scene = new Scene();
...
Importable Examples
The core of three.js is focused on the most important components of a 3D engine. Many other components like loaders or controls are part of the
examples directory. three.js ensures that these files are kept in sync with the core but users have to import them separately if they are required
for their project. However, not all files are modules which makes their usage in certain cases inconvenient. In order to address this issue,
we are working to provide all the examples as modules in the [link:https://github.com/mrdoob/three.js/tree/master/examples/jsm examples/jsm] directory.
If you install three.js via npm, you can import them like so:
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
The following examples files are already available as modules:
- animation
- AnimationClipCreator
- CCDIKSolver
- MMDAnimationHelper
- MMDPhysics
- TimelinerController
- cameras
- controls
- DeviceOrientationControls
- DragControls
- EditorControls
- FirstPersonControls
- FlyControls
- MapControls
- OrbitControls
- OrthographicTrackballControls
- PointerLockControls
- TrackballControls
- TransformControls
- curves
- CurveExtras
- NURBSCurve
- NURBSSurface
- NURBSUtils
- effects
- AnaglyphEffect
- AsciiEffect
- OutlineEffect
- ParallaxBarrierEffect
- PeppersGhostEffect
- StereoEffect
- exporters
- ColladaExporter
- DRACOExporter
- GLTFExporter
- MMDExporter
- OBJExporter
- PLYExporter
- STLExporter
- TypedGeometryExporter
- geometries
- BoxLineGeometry
- ConvexGeometry
- DecalGeometry
- ParametricGeometries
- TeapotBufferGeometry
- interactive
- SelectionBox
- SelectionHelper
- lines
- Line2
- LineGeometry
- LineMaterial
- LineSegments2
- LineSegmentsGeometry
- Wireframe
- WireframeGeometry2
- loaders
- 3MFLoader
- AMFLoader
- AWDLoader
- AssimpJSONLoader
- AssimpLoader
- BabylonLoader
- BVHLoader
- ColladaLoader
- DDSLoader
- DRACOLoader
- EXRLoader
- FBXLoader
- GCodeLoader
- GLTFLoader
- HDRCubeTextureLoader
- KMZLoader
- KTXLoader
- LWOLoader
- MD2Loader
- MTLLoader
- NRRDLoader
- OBJLoader
- PCDLoader
- PDBLoader
- PlayCanvasLoader
- PLYLoader
- PRWMLoader
- PVRLoader
- RGBELoader
- STLLoader
- SVGLoader
- TDSLoader
- TGALoader
- TTFLoader
- VRMLLoader
- VTKLoader
- XLoader
- math
- ColorConverter
- ConvexHull
- ImprovedNoise
- Lut
- SimplexNoise
- misc
- CarControls
- ConvexObjectBreaker
- GPUComputationRenderer
- Gyroscope
- MD2Character
- MD2CharacterComplex
- MorphAnimMesh
- MorphBlendMesh
- Ocean
- RollerCoaster
- Volume
- VolumeSlice
- modifiers
- ExplodeModifier
- SimplifyModifier
- SubdivisionModifier
- TessellateModifier
- objects
- Fire
- Lensflare
- Reflector
- Refractor
- ReflectorRTT
- ShadowMesh
- Sky
- Water
- Water2
- pmrem
- PMREMCubeUVPacker
- PMREMGenerator
- postprocessing
- AdaptiveToneMappingPass
- AfterimagePass
- BloomPass
- BokehPass
- ClearPass
- CubeTexturePass
- DotScreenPass
- EffectComposer
- FilmPass
- GlitchPass
- HalftonePass
- MaskPass
- OutlinePass
- RenderPass
- SAOPass
- SavePass
- ShaderPass
- SMAAPass
- SSAARenderPass
- SSAOPass
- TAARenderPass
- TexturePass
- UnrealBloomPass
- renderers
- CSS2DRenderer
- CSS3DRenderer
- Projector
- SoftwareRenderer
- SVGRenderer
- RaytracingRenderer
- WebGLDeferredRenderer
- shaders
- AfterimageShader
- BasicShader
- BleachBypassShader
- BlendShader
- BokehShader
- BokehShader2
- BrightnessContrastShader
- ColorCorrectionShader
- ColorifyShader
- ConvolutionShader
- CopyShader
- DepthLimitedBlurShader
- DigitalGlitch
- DOFMipMapShader
- DotScreenShader
- FilmShader
- FocusShader
- FreiChenShader
- FresnelShader
- FXAAShader
- GammaCorrectionShader
- GodRaysShader
- HalftoneShader
- HorizontalBlurShader
- HorizontalTiltShiftShader
- HueSaturationShader
- KaleidoShader
- LuminosityHighPassShader
- LuminosityShader
- MirrorShader
- NormalMapShader
- OceanShaders
- ParallaxShader
- PixelShader
- RGBShiftShader
- SAOShader
- SepiaShader
- SkinShader
- SMAAShader
- SobelOperatorShader
- SSAOShader
- TechnicolorShader
- TerrainShader
- ToneMapShader
- ToonShader
- TranslucentShader
- TriangleBlurShader
- UnpackDepthRGBAShader
- VerticalBlurShader
- VerticalTiltShiftShader
- VignetteShader
- VolumeShader
- WaterRefractionShader
- utils
- BufferGeometryUtils
- GeometryUtils
- MathUtils
- SceneUtils
- ShadowMapViewer
- SkeletonUtils
- TypedArrayUtils
- UVsDebug
Note: When using code from the examples directory, it's important that all files match the version of
your three.js main file. For example, it's not acceptable to use *GLTFLoader* and *OrbitControls* from R96 together
with three.js R103. You can easily keep your files in sync by using the modules from the JSM directory. If the file
is not available as a module, you can still use third-party npm packages or convert the file to a module by yourself.
In both cases, ensure the code is compatible with your three.js main file.