我只想在我的express.js项目中设置环境形式的开发,登台和生产
要设置这些环境,我使用模块config https://www.npmjs.com/package/config
,并在config文件夹中创建了文件development.js
我的代码用于文件开发。
import db from './db';
module.exports = {
host: '0.0.0.0',
port: process.env.PORT || 5000,
secret: process.env.SECRET || 'supersecret',
db: db.development,
base_url: process.env.BASEURL || 'http://192.168.1.153:5000',
fornt_url: process.env.FRONT_BASEURL || 'http://localhost:4200/#',
savePath:"./uploads/"
};
,我还将脚本中的package.json文件更新为
"scripts": {
"test": "node app.js",
"development": "NODE_ENV=development nodemon app.js",
"staging": "NODE_ENV=staging node app.js",
"production": "NODE_ENV=production node app.js"
},
当我运行我的项目时,出现以下错误,请帮助解决此问题
[nodemon] starting `node app.js`
/data/node/tempfu-api/node_modules/config/lib/config.js:963
throw new Error("Cannot parse config file: '" + fullFilename + "': " + e3);
^
Error: Cannot parse config file: '/data/node/tempfu-api/config/development.js': SyntaxError: Unexpected token import
at Config.util.parseFile (/data/node/tempfu-api/node_modules/config/lib/config.js:963:11)
at /data/node/tempfu-api/node_modules/config/lib/config.js:725:28
at Array.forEach (<anonymous>)
at /data/node/tempfu-api/node_modules/config/lib/config.js:721:14
at Array.forEach (<anonymous>)
at Config.util.loadFileConfigs (/data/node/tempfu-api/node_modules/config/lib/config.js:720:13)
at new Config (/data/node/tempfu-api/node_modules/config/lib/config.js:136:27)
at Object.<anonymous> (/data/node/tempfu-api/node_modules/config/lib/config.js:1797:31)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/data/node/tempfu-api/helpers/mysql.js:1:78)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
答案 0 :(得分:0)
我在另一个项目上遇到了同样的问题。通过删除最后一个逗号来解决。
因此,如果您的.json文件中的任何对象均带有尾部逗号,则应将其删除,并将其正确加载。如果对象内部有逗号结尾,也会弹出此错误。
{...},
"sampleScript": {
"test": "node app.js",
"development": "NODE_ENV=development nodemon app.js",
"staging": "NODE_ENV=staging node app.js",
"production": "NODE_ENV=production node app.js" // No trailing comma here at the last line
} // No trailing comma here if the end of the file