在我的index.js文件中,我将const config = require('config');
写为第一行。
我的项目文件夹中有一个名为config.js
的文件但我一直让我的控制台告诉我Cannot find module 'config'
我的配置文件基本上是这样的:
module.exports = {
'secretKey': 'mySecretCode12232',
'mongoUrl' : 'mongodb://localhost:27017/test'
};
这没有任何意义,它应该有效。
答案 0 :(得分:0)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS_HOME" value="${LOG_PATH}"/>
<!-- standard encoder pattern-->
<property name="LOG_PATTERN" value="%d{MM/dd/yy HH:mm:ss.SSS} %-5p %t %C{0}:%L - %m%n" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${LOG_PATTERN}</Pattern>
</layout>
</appender>
<property name="EMPLOYEE_LOG_FILE" value="${LOGS_HOME}/employee.log"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${EMPLOYEE_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${EMPLOYEE_LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>14</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<property name="EMPLOYEE_SALARY_LOG_FILE" value="${LOGS_HOME}/emp_sal.log"/>
<appender name="SalaryLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${EMPLOYEE_SALARY_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${EMPLOYEE_SALARY_LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>14</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<property name="EMPLOYEE_DEPT_LOG_FILE" value="${LOGS_HOME}/emp_sal.log"/>
<appender name="DeptLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${EMPLOYEE_DEPT_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${EMPLOYEE_DEPT_LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>14</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="com.nbt.apps.employeeInfo" level="info" additivity="false">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.garmin.gh.apps.wellnessmonitor.service.EmployeeService" level="info" additivity="false">
<appender-ref ref="DeptLogAppender" />
<appender-ref ref="SalaryLogAppender"/>
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
How to log from one class to two different log files based on methods?
我也有自己的函数,它从定义的子目录加载atomaticaly,它节省了很多时间。
答案 1 :(得分:0)
如果您未在require语句中提供任何路径选择器(例如require('./config'))
),您的代码将搜索名为config
的程序包,并因无法找到此特定程序包而失败,如果需要将假设它是提供的包名称(并将开始搜索,例如在您的node_modules
等 - 搜索路径,这不是一个简单的主题:))。
如果您想要从另一个文件中获取该模块,您必须提供一个正确的路径,因此假设您的config.js
与您的其他文件位于同一目录中,正确的声明将是:
const config = require('./config'); // Extension can be omitted