I have this .sequelizerc file:
const path = require('path');
module.exports = {
config: path.resolve('.', 'src/config/sequelizeCLIConfig.json'),
'migrations-path': path.resolve('.', 'db', 'migrations'),
};
And then I have a .ts file that generates the cli config file named generateSequelizeCLIConfig.ts, which does the following thing:
import path from 'path';
import fs from 'fs';
import config from '../../src/config';
import sequelizeRC from '../../.sequelizerc';
const env = process.env.NODE_ENV || 'development';
const targetFile = path.resolve(sequelizeRC.config);
const sequelizeCLIConfig: Record<string, any> = {};
sequelizeCLIConfig[env] = config.db;
fs.writeFile(targetFile, JSON.stringify(sequelizeCLIConfig, null, 4), err => {
if (err) {
return console.log(err);
}
console.log('The sequelizeCLI config file was saved at ' + targetFile + '!');
});
The plan is, every time I need migration, I run this script first. This script grabs the data from the config folder and generate the src/config/sequelizeCLIConfig.json. And then I run the migration with config data from this .json file.
So the file structure is this:
-.sequelizerc
-db
|-scripts
|-generateSequelizeCLIConfig.ts
-src
|-config
|-index.ts
.sequelizerc
However I got this error when compiling generateSequelizeCLIConfig.ts:
TSError: тип Unable to compile TypeScript:
db/scripts/generateSequelizeCLIConfig.ts(4,25): error TS2307: Cannot find module '../../.sequelizerc'.
So it seems .sequrlizerc is not recognized although I have double checked that this file does exist.
My guess is, .sequelizerc behind the scene is a .js file, not a .ts file, and this gives me some trouble. But I don't know how to verify this, nor how to fix it.
Any suggestions?
Try to add the .sequelizerc to your include in your tsconfig as follows:
{
..prev configs...,
include: [ ...your paths..., ".sequelizerc"],
}
Delete dot. Rename the .sequelizerc to sequelizerc
Related
Since __dirname is undefined in a typescript electron project, I have resorted to
import * as path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
However, this new __dirname gets the directory of the original source file instead of the compiled file. (Paths ends up being /src/something.ts instead of /dist/something.js)
Is there a way to get the path of the compiled file being run? Am I missing some webpack, tsconfig settings?
(Unfortunately, app.getAppPath() also seems to rely on __dirname)
EDIT: I am trying to load an angular app inside electron through the protocol.registerFileProtocol since plain relative or file:// links to not seem to be loaded:
protocol.registerFileProtocol('resource', (request, callback) => {
let url = request.url.substr(11); // Remove the resource://
callback(path.join(app.getAppPath(), '/', url));
// or
callback(path.join(__dirname, '/', url));
});
I am trying to extract .7z files which is password protected.
In a particular folder path there are some .7z files format. First I have to extract all files in the same directory than I have to do another stuff with this files.
const path = require('path')
const fs = require('fs')
import { extractFull } from 'node-7z-forall';
const dirpath = path.join('C:/MyFolder/DATA')
fs.readdir(dirpath, function(err, files) {
const txtFiles = files.filter(el => path.extname(el) === '.7z')
console.log(txtFiles);
extractFull(txtFiles, 'C:/MyFolder/DATA', { p: 'admin123' } /* 7z options/switches */)
.progress(function (files) {
console.log('Some files are extracted: %s', files);
});
})
I am using node-7z-forall module but it is only working when I change the file format to .js to .mjs. in .mjs file format file extract smoothly .but in .js format it is not working.
error:
import { extractFull } from 'node-7z-forall';
^^^^^^
SyntaxError: Cannot use import statement outside a module
How to handle this error. Is it possible to work with in .js format instead of .mjs format?
I am new in nodejs. Please help!
the reason it errors, it that ".js" indicates a commonjs file which uses require() but a ".mjs" file indicates a module which uses the import syntax.
This is also where the error comes from because you try to use import in a non module.
You can prevent the error by simply importing the package using require():
const { extractFull } = require('node-7z-forall');
Im using the dotenv library, but my env variables returns undefined
// app.ts
require('dotenv').config({path: '/.env'});
console.log(process.env.MAIN_DB_PATH) // returns undefined
// .env
MAIN_DB_PATH=./data/database.db
UPLOAD_MULTER_DIR=./module/car/uploads
My folder structure is
So it should works fine :(
To load the .env file in a different directory, you need to provide the absolute path to that file.
__dirname : the absolute path to the directory of the file where you need to load .env file (app.ts in this case)
.. : go 1 level up
Then path.resolve will give you the absolute path to .env file
const path = require('path');
require("dotenv").config({ path: path.resolve(__dirname, '..', '.env') });
console.log(process.env.MAIN_DB_PATH);
You do not need path if the .env file is at the root, but you can define a return value from config method and check if error happend
const result = dotenv.config()
if (result.error) {
throw result.error
}
console.log(result.parsed)
source: https://www.npmjs.com/package/dotenv config paragraph
I try to copy and replace some variable in multiples files. For this, i use esj module replace my vars.
But i don't know if ejs module is correct for my case. I would like just copy "template" initial file and replace variable in file.
My exemple using NodeJS :
const symfonyPluginPath = path.join(
__dirname,
'../plugins/symfony/template'
);
const testPath = path.join(__dirname, '../plugins/test');
shell.rm('-rf', testPath);
shell.mkdir(testPath);
shell.cp('-r', `${symfonyPluginPath}/*`, testPath);
shell.cp('-r', `${symfonyPluginPath}/.*`, testPath);
shell.cd(testPath);
// #ts-ignore
fs.readdir(testPath, (error, files) => {
files.forEach((file) => {
const compiled = ejs.compile(
fs.readFileSync(`${testPath}/${file}`, 'utf8')
);
const test = compiled({ appName: 'test' });
console.log(test);
});
});
This code work for only 1 file, but in forEach i've an error EISDIR: illegal operation on a directory, read.
I don't know if my approch is good and if ejs is the correct module for this.
Anyone can help me ?
Thank you community !
EISDIR stands for "Error, Is Directory". This means that NPM is trying to do something to a file but it is a directory.
Try in this format ---
path.join('x/y/z', '/plugins/test')
I have the following folder structure:
/
/src/
file.ts
one.txt
/resources/
two.txt
and in file.ts I want to read contents of one.txt and two.txt by doing something like the following:
import fs from 'fs';
import path from 'path';
// sync is bad.
fs.readFileSync('one.txt');
fs.readFileSync(path.resolve(__dirname, '../resources/file.txt'));
Everything works just fine when using ts-node.
The problem is that when I run tsc it compiles all files to /dist (which I have told the compiler to do in tsconfig.json by setting outDir to ./dist), but both fs.readFileSync(...) fail, because the *.txt files are not copied to /dist, so fs can not find the files.
Now, my question is: Is there a beautiful way to handle this, and make fs read and writes work as expected both, when using ts-node, and after tsc?
I've managed to handle this in several projects by doing something weird like:
// file.ts
const getResourcesDir = () => {
const dir = path.basename(path.dirname(__dirname));
if (dir === 'dist') {
return path.resolve(__dirname, '../../resources');
} else {
return path.resolve(__dirname, '../resources');
}
}
But this seems just wrong. I believe that there should be a nicer solution, but I can't find it.
dir = path.basename(path.dirname(__dirname));
const certDir = (dir === 'dist' ? './dist/cert': '../cert');
console.log('Cert file', certDir, `${certDir}/server-key.pem`);
console.log('Key file', certDir, `${certDir}/server-key.pem`);