Configure where files are stored on your server by setting the fileStorage.mount option in your Nuxt configuration.
Basic Configuration
Add the configuration to nuxt.config.ts:
export default defineNuxtConfig ({
modules: [ 'nuxt-file-storage' ] ,
fileStorage: {
mount: '/absolute/path/to/storage'
}
})
The mount path must be an absolute path . Relative paths are not supported.
Using Environment Variables (Recommended)
For better flexibility across different environments, use environment variables:
Create .env file
Add the mount path to your .env file: FILE_STORAGE_MOUNT = /home/user/myapp/storage
Reference in config
Use process.env in your Nuxt config: export default defineNuxtConfig ({
modules: [ 'nuxt-file-storage' ] ,
fileStorage: {
mount: process . env . FILE_STORAGE_MOUNT
}
})
Using environment variables allows you to use different storage paths for development, staging, and production without changing your code.
Environment-Specific Configuration
Configure different paths for different environments:
export default defineNuxtConfig ({
modules: [ 'nuxt-file-storage' ] ,
fileStorage: {
mount: process . env . NODE_ENV === 'production'
? '/var/www/storage'
: '/home/dev/storage'
}
})
Environment Variable Fallback
The library automatically checks for environment variables if the config is not set:
.env (Option 1)
.env (Option 2)
FILE_STORAGE_MOUNT = /path/to/storage
Both FILE_STORAGE_MOUNT and NUXT_FILE_STORAGE_MOUNT are supported.
Path Configuration Examples
Development Setup
import { join } from 'path'
export default defineNuxtConfig ({
modules: [ 'nuxt-file-storage' ] ,
fileStorage: {
// Store in project directory during development
mount: join ( process . cwd (), 'storage' )
}
})
Production Setup
.env.production
.env.production (Windows)
# Linux/Unix
FILE_STORAGE_MOUNT = /var/www/myapp/storage
# Or use user home directory
FILE_STORAGE_MOUNT = /home/appuser/storage
Docker Setup
FROM node:20-alpine
# Create storage directory
RUN mkdir -p /app/storage
# Set environment variable
ENV FILE_STORAGE_MOUNT=/app/storage
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
EXPOSE 3000
CMD [ "npm" , "start" ]
With docker-compose:
version : '3.8'
services :
app :
build : .
environment :
- FILE_STORAGE_MOUNT=/app/storage
volumes :
# Mount volume to persist files
- ./storage:/app/storage
ports :
- "3000:3000"
Absolute vs Relative Paths
Absolute Paths (Required)
The mount path must be absolute:
Valid absolute paths
Invalid relative paths
// Linux/Unix
'/home/user/storage'
'/var/www/storage'
'/opt/myapp/files'
// Windows
'C: \\ Users \\ User \\ storage'
'D: \\ www \\ files'
Converting Relative to Absolute
If you have a relative path, convert it to absolute:
import { join , resolve } from 'path'
export default defineNuxtConfig ({
modules: [ 'nuxt-file-storage' ] ,
fileStorage: {
// Convert relative path to absolute
mount: resolve ( process . cwd (), 'storage' )
}
})
Directory Structure
Once configured, organize your storage with subdirectories:
/your/mount/path/
├── uploads/ # General uploads
├── images/ # Image files
│ ├── avatars/
│ └── products/
├── documents/ # Documents
│ ├── invoices/
│ └── contracts/
├── temp/ # Temporary files
└── users/ # User-specific files
├── 123/
└── 456/
Subdirectories are created automatically when you use them:
// This creates /your/mount/path/users/123/ if it doesn't exist
await storeFileLocally ( file , 8 , '/users/123' )
Permissions
Ensure the Node.js process has read/write permissions for the mount directory:
Linux/Unix
# Create directory
mkdir -p /var/www/storage
# Set ownership (replace 'appuser' with your user)
chown -R appuser:appuser /var/www/storage
# Set permissions
chmod -R 755 /var/www/storage
Checking Permissions
# Check current permissions
ls -la /var/www/storage
# Should show something like:
# drwxr-xr-x 2 appuser appuser 4096 Mar 15 10:30 storage
Configuration Validation
Verify your configuration is correct:
server/api/config/check.ts
export default defineEventHandler (( event ) => {
const config = useRuntimeConfig ( event )
const mount = config . public . fileStorage . mount
if ( ! mount ) {
throw createError ({
statusCode: 500 ,
message: 'fileStorage.mount is not configured'
})
}
return {
configured: true ,
mount ,
isAbsolute: mount . startsWith ( '/' ) || / ^ [ A-Z ] : \\ / . test ( mount )
}
} )
Test Upload Endpoint
Create a test endpoint to verify everything works:
server/api/test/upload.ts
import { writeFile } from 'fs/promises'
import { join } from 'path'
export default defineEventHandler ( async ( event ) => {
const config = useRuntimeConfig ( event )
const mount = config . public . fileStorage . mount
if ( ! mount ) {
throw createError ({
statusCode: 500 ,
message: 'Storage not configured'
})
}
try {
// Try to write a test file
const testPath = join ( mount , 'test.txt' )
await writeFile ( testPath , 'test content' )
return {
success: true ,
message: 'Storage is configured correctly' ,
path: testPath
}
} catch ( error : any ) {
throw createError ({
statusCode: 500 ,
message: `Storage error: ${ error . message } `
})
}
} )
Common Issues
Error: fileStorage.mount is not configured
EACCES: permission denied
The Node.js process doesn’t have permission to write to the directory. Solutions:
Check directory ownership: ls -la /path/to/storage
Change ownership: chown -R $USER:$USER /path/to/storage
Set proper permissions: chmod -R 755 /path/to/storage
Run Node.js with appropriate user permissions
ENOENT: no such file or directory
The mount directory doesn’t exist. Solutions:
Create the directory: mkdir -p /path/to/storage
Ensure the path in your config is correct
Check for typos in the path
Verify the path is absolute, not relative
You’re using a relative path instead of an absolute path. Solution:
Convert relative to absolute:import { resolve } from 'path'
fileStorage : {
mount : resolve ( process . cwd (), 'storage' )
}
Runtime Config Access
Access the mount path in your server code:
export default defineEventHandler (( event ) => {
const config = useRuntimeConfig ( event )
const mount = config . public . fileStorage . mount
console . log ( 'Storage mount:' , mount )
} )
Multiple Storage Locations
While the library only supports one mount point, you can organize multiple storage areas using subdirectories:
const STORAGE_FOLDERS = {
UPLOADS: '/uploads' ,
IMAGES: '/images' ,
DOCUMENTS: '/documents' ,
TEMP: '/temp' ,
PRIVATE: '/private'
} as const
// Use in your handlers
await storeFileLocally ( file , 8 , STORAGE_FOLDERS . IMAGES )
await storeFileLocally ( file , 8 , STORAGE_FOLDERS . DOCUMENTS )
Best Practices
Use environment variables
Always use environment variables for the mount path to support different environments: fileStorage : {
mount : process . env . FILE_STORAGE_MOUNT
}
Create storage directory during setup
Include directory creation in your deployment scripts: mkdir -p /var/www/storage
chown appuser:appuser /var/www/storage
Add .gitignore entry
Don’t commit uploaded files to version control: # Storage directory
storage/
# But keep the directory structure
!storage/.gitkeep
Set up backups
Regularly backup your storage directory: # Example backup script
tar -czf storage-backup- $( date +%Y%m%d ) .tar.gz /var/www/storage
Next Steps
File Upload Start uploading files with your configured storage
File Management Learn how to manage stored files