gpt4 book ai didi

javascript - 在 gruntfile.js 中设置 node_modules 的目录

转载 作者:行者123 更新时间:2023-11-28 05:17:20 25 4
gpt4 key购买 nike

我想在 gruntfile.js 中设置 node_modules 目录的路径。我希望为我的所有 grunt 项目建立一个中央 node_module 目录。

是否可以设置外部node_module目录的路径?

当前设置(有效)

projekt1--gruntfile.js--package.json--node_modulesprojekt2--gruntfile.js--package.json--node_modules

设置我想要的

node_modulesproject1--gruntfile.js--package.jsonproject2--gruntfile.js--package.json

最佳答案

你可以......我一直在使用这个设置,没有任何问题。在您的每个项目 Gruntfile.js 文件中,只需将基础设置为一个目录,如下所示:

给定这样的文件夹结构:

node_modules
project1
app.js
Gruntfile.js
package.json

您的 Gruntfile.js 可能看起来像这样

module.exports = function(grunt) {

// Tell grunt where to find node_modules
// This is the line you're looking for
grunt.file.setBase('../');

// Your normal grunt config
grunt.initConfig({

// Your package.json file is now relative to the parent folder
pkg: grunt.file.readJSON('project1/package.json'),

// I use webpack, your tasks will probably be different
webpack: {
options: {},
build: {

// Paths are now resolved from the upper folder
// since we set the base path to one level up
entry: './project1/app.js',
output: {

// Again, the path must be relative to the parent folder
path: 'project1',
filename: 'app.min.js'
}
}
}

// etc...
});

// Load tasks...

}

setBase('../') 后,您需要确保所有文件路径均已正确解析,因为您的基本路径已更改为上一级。

警告...如果您发布或分发您的包,Grunt 任务将不适用于那些下载它的人,因为他们很可能不会与 node_modules 1 级别相同的设置。即使他们这样做,他们也可能不会将 project1 文件夹命名为相同的

关于javascript - 在 gruntfile.js 中设置 node_modules 的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40867604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com