Organizing a Javascript project for development and production with the help of npm and grunt

I have been working on structuring the JavaScript files in my project. I opted to use NPM for managing modules and Grunt for concatenating and compressing js and css files for deployment purposes.

Here is the current structure I am using:


-[project root]   
-- [node modules]: contains packages like requirejs, jquery, semantic-ui etc. installed via npm  

--[war]  
---[Dev]   
----[css]: multiple css files from the modules (Question 2:?)  
----[js]: multiple js files from the moduels (Question 2:?)

- Gruntfile.js: used for concatenation and compression    
---[Production] -   
----[css]: directory for compressed and concatenated css files  
----[js]: directory for compressed and concatenated js files  

Question 1: Do you think the above approach to structuring the project is correct? Are there any other recommendations that would help with managing packages, dev, and production files more efficiently?

Question 2: Is there a way for NPM or another tool to automatically move js and css files from the [node modules] folder to the [dev >> css] or [dev >> js] folders? If I do this manually, how can I keep track of the versions? It seems like there might be a better solution out there that I am missing.

Any suggestions, recommendations, or comments would be greatly appreciated.

Thank you!

Answer №1

The question may be too broad for the SO format, but in general, your approach is solid. Instead of directly copying files from node_modules, it's recommended to have your JavaScript files stored under a separate directory like js and then import or require them into your own files.

//foo.js
//Using ES6 style imports
import {Foo as Bar} from "biz";

//Common JS style requires
var Bar = require("biz");

//AMD style requires
require(["biz"], function (Bar) {

If you plan on using modules from node_modules in a browser environment, consider bundling them with tools like Browserify, Webpack, Rollup, or similar bundlers. For automation, utilize task runners such as grunt-browserify along with grunt-watch.

The same principle applies to CSS files: Store your stylesheets under css and if needed, import CSS files from node_modules into your own files. If using preprocessors like SASS or LESS, these will typically handle inlining imports during the build process. For plain CSS files, tools like grunt-css-import can help manage imports.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Increment version number automatically in Node.js package and push changes to git

I'm having trouble figuring out how to automatically increment the version number in my node application's package.json file. I know that npm has a script called version, which takes 3 parameters: minor, major, and patch. However, when I try to u ...

In discord.js version 14, I am attempting to set my bot's status to reflect the total number of servers it is currently a member of

I'm attempting to set my bot's status to reflect the number of servers it is currently in. I attempted the following code: client.on("ready", () => { // let guildsCount = client.guilds.size client.user.setPresence({ activities: [ ...

What are the steps to resolve the issue of "npm ERR! EEXIST: file already exists, rename" occurring with non-existent files?

Welcome to my first question post. (Please be kind if I make mistakes.) I am using node version 5.6.0. For an assignment, I downloaded a JS web app but am encountering an error that is preventing me from working on it: S:\PersonalCloud\jennyly ...

Discovering the overlap between two arrays using Node.js

Question: Simplest code for array intersection in JavaScript In the development of my app using Mongodb and Nodejs, I am working with a 'students' collection that includes an array listing all the courses (course IDs) a specific student has ta ...

"Accessing the remote IP address in GraphQL using an Express Node

Check out this informative tutorial on utilizing GraphQL in NodeJS with express: It may seem simple to retrieve the IP address, but I encountered an issue when attempting to do so locally: Encountered the following error: Cannot read property 'ip& ...

Updating Documents in CouchDB

Can you please confirm if this is the correct method for updating a document in couchDB? To update a document (let's call it fooDoc), I must pass "_rev". First, I need to retrieve that document using the following code (foo.get). Then, in the callbac ...

What is the best way to deliver data from the main Vue instance to components that are being utilized by vue-router?

Currently, I am utilizing vue-router within a single HTML file instead of using separate file components. I am encountering an issue where I am unsure how to pass the data stored in the Vue data object to the component when loading it for a route. Oddly e ...

Strange Firefox behavior with absolutely positioned div and percentage height

One of my projects includes a CSS modal dialog that is structured as follows div { position:fixed; top:50%; left:50%; margin:-26% 0 0 -26%; width:46%; height:46%; padding:3%; } Interestingly, this div appears centered in webkit browsers. Ho ...

Simple guide to decoding JSON in React Native

Currently, I am working on a react native project where I am calling an API to extract data. The extraction process is successful, but now I am facing an issue with taking arguments from the extracted data. Axios({ url: '/Authentification', ...

Discover the Elements that have been incorporated through the use of JavaScript

I am facing an issue with my ASP site where users can add Label elements. The problem is I am not able to track the labels that were added or their IDs. All I know is that they will be inside the Panel called pnl_Added. Once the user has finished adding la ...

JavaScript encountered an error stating "phone is not defined" due to an uncaught ReferenceError

Whenever I click on the Phone Number, it displays an error message saying that the function is not defined. How can I fix this issue? Thank you in advance! Here's the code snippet: <div class="product-label"> <h4><?php echo $p["Fu ...

As the page is resized or zoomed in and out, the div elements start shifting in different directions

When I apply position:absolute to prevent the elements from moving when zooming in and out of the page, they all end up congregating in one place. Can anyone help me with this issue? None of the solutions I've found have worked so far. I'm develo ...

How can I display an ngx spinner after a delay of 1 second?

I am uncertain about the answer I came across on this platform. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const time = 900; const spinnerLogic = () => { if (this.isRequestServed ...

New to NodeJS: Utilizing Requestify to Retrieve Data from Another URL

As a newcomer in the world of NodeJs, I am faced with the task of transitioning my CodeIgniter server APIs to Node.js. Currently, I am utilizing requestify to retrieve data from a web service, and once this is accomplished, I intend to invoke an insert met ...

Configuring Node.js HTTPS to function alongside HAPROXY

My goal is to establish communication between my nodejs app and HAPROXY using HTTPS. The plan is for nodejs to send a message to haproxy via https, and haproxy will then route the message accordingly. Initially, I had success with the request.js library, ...

Is there a way to retrieve the ID from a span and convert it into a string format?

let wordSpan = '<span class="we w_o_r_d_s" id="we" contenteditable="false">we</span>' console.log(wordSpan.id); console.log(wordSpan.attr('id')); console.log(wordSpan.getAttribute('id')); let newDiv = document.c ...

What advantages does utilizing NPM provide compared to directly incorporating the JavaScript library into an MVC project?

When working on an MVC project, I experimented with two different methods of including jQuery: I first downloaded the jQuery files and manually added them to the project, as shown here: https://i.sstatic.net/2TjqX.png I also tried using the bundle sett ...

Modifying the border of images in a Weebly gallery

I'm having trouble changing the gallery image borders on Weebly. The client prefers using Weebly for easy backend editing once the design is in place, so I need to work within these parameters. Usually, I can make most code edits easily...but this iss ...

Webpack returns an undefined error when attempting to add a JavaScript library

I am a newcomer to webpack and I am attempting to incorporate skrollr.js into my webpack setup so that I can use it as needed. However, I am unsure of the correct approach for this. After some research, I have found that I can either use an alias or export ...

Updating the state in React can be achieved by using the `

Upon obtaining a list of search results, each result is equipped with an onclick function. My goal is to exhibit the user-selected results on the screen by adding them to an array upon click: let selectedData = [] function addFunc(resultdata){ consol ...