【问题标题】:Syntax Error in Index.jsIndex.js 中的语法错误
【发布时间】:2016-09-05 18:56:31
【问题描述】:
'use strict';

var path = require('path');
var _ = require('lodash');

function requiredProcessEnv(name) {
if (!process.env[name]) {
throw new Error('You must set the ' + name + ' environment variable');
}
return process.env[name];
}

// All configurations will extend these options
// ============================================
var all = {
env: process.env.NODE_ENV,

// Root path of server
root: path.normalize(__dirname + '/../../..'),

// Server port
port: process.env.PORT || 9000,

// Server IP
ip: process.env.IP || '0.0.0.0',

// Should we populate the DB with sample data?
seedDB: false,

// Secret for session, you will want to change this and make it an  environment variable
secrets: {
session: process.env.session || "wav"
},
// Export the config object based on the NODE_ENV
// ==============================================
module.exports = _.merge(
all,
require('./shared'),
require('./' + process.env.NODE_ENV + '.js') || {})};

使用我的项目(使用 angular-fullstack 创建的网络应用程序)运行时,我收到以下错误:

Line 39 col 6 Unexpected token: module.exports
                                        ^
line 39  col 7   Expected ':' and instead saw '.'.
  line 42  col 54  Expected '}' to match '{' from line 15 and instead saw ';'.

另外,这里的代码也给我的项目带来了一些其他问题。我将最后几行更改为:

var config = _.merge(...); 
  console.log(config); 
  module.exports = config;`
And I still get a syntax error: ` line 39  col 7   Expected ':' and instead saw 'config'.
  line 39  col 14  Expected an identifier and instead saw '='.
  line 39  col 16  Expected '}' to match '{' from line 15 and instead saw '_'.
  line 39  col 27  Expected an identifier and instead saw ')'.
  line 39  col 27  Expected an identifier and instead saw ')'.
  line 39  col 28  Expected ')' and instead saw ';'.
  line 40  col 15  'config' is not defined.
  line 41  col 20  'config' is not defined.

【问题讨论】:

    标签: angularjs node.js postgresql express web-application-project


    【解决方案1】:

    'module.export' 在 'all' 对象内,将其移到外面应该可以:

    'use strict';
    
    var path = require('path');
    var _ = require('lodash');
    
    function requiredProcessEnv(name) {
        if (!process.env[name]) {
            throw new Error('You must set the ' + name + ' environment variable');
        }
        return process.env[name];
    }
    
    // All configurations will extend these options
    // ============================================
    var all = {
        env: process.env.NODE_ENV,
    
    // Root path of server
        root: path.normalize(__dirname + '/../../..'),
    
    // Server port
        port: process.env.PORT || 9000,
    
    // Server IP
        ip: process.env.IP || '0.0.0.0',
    
    // Should we populate the DB with sample data?
        seedDB: false,
    
    // Secret for session, you will want to change this and make it an  environment variable
        secrets: {
            session: process.env.session || "wav"
        }
    };
    
    // Export the config object based on the NODE_ENV
    // ==============================================
    module.exports = _.merge(
        all,
        require('./shared'),
        require('./' + process.env.NODE_ENV + '.js') || {}
    );
    

    【讨论】:

    • 谢谢你,我注意到了这么小的事情。我很感激
    【解决方案2】:

    请学会正确缩进您的代码。然后,您的错误应该一目了然。

    Coding Style Guide for node.js apps?

    https://github.com/felixge/node-style-guide

    【讨论】:

    • np,缩进它,如果你再次失败,再次发布,看看我们是否可以提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2015-03-26
    相关资源
    最近更新 更多