【问题标题】:Access node_module from different location从不同位置访问 node_module
【发布时间】:2018-01-23 09:18:25
【问题描述】:

所以我正在使用 Grunt + Grunt-Browserify + Vue + Vueify 设置一个项目,但在访问“vue”模块时遇到问题。

这是我的文件夹结构:

app/
   build/
   app.js
   app.vue

grunt/
     node_modules/
     Gruntfile.js
     packaje.json

这是我的 Gruntfile 的内容:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        browserify: {
            vendor: {
                src: ['../app/app.js'],
                dest: '../app/build/app.js',
                options: {
                    watch: true,
                    keepAlive: true,
                    browserifyOptions: {
                        debug: true
                    },
                    transform: ['vueify']
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-browserify');

    grunt.registerTask('default', ['browserify']);
};

这是我的 app.js 的内容:

var Vue = require('vue');
var App = require('./app.vue');

new Vue({
    el: '#app',
    render: function (createElement) {
        return createElement(App);
    }
});

当我运行grunt 时,我得到找不到模块'vue'

如果我像这样设置变量Vue

var Vue = require(__dirname + '/' + 'vue');

我收到此错误Cannot find module 'vue-hot-reload-api'

如果我将 app 文件夹放在 grunt 文件夹中与 node_modules 相同的级别,并且变量 Vue 设置如下: var Vue = require('vue'); 一切正常。

我怎样才能让它工作,以便无论我的应用程序位于何处,我总能找到一个模块

谢谢你,祝你有美好的一天:)

【问题讨论】:

    标签: node.js vue.js gruntjs


    【解决方案1】:

    您的目录名称和路径似乎不正确:

    src: ['../../vue-app/app.js']
    

    应该是

    src: ['../app/app.js']
    

    【讨论】:

    • 抱歉打错了,就像你说的那样:) 我编辑了帖子
    猜你喜欢
    • 2015-05-26
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多